S
slashdotcommacolon
Hello, is this the correct solution to Exercise 2-7 of K&R, it seems to
work but using sizeof(x)*8-n to shift the saved bits that would get
lost to the end seems like cheating:
unsigned int rightrot (unsigned int x, int n)
{
return ~(~(x >> n) ^ ((x & ~(~0 << n))<<((sizeof (x)*8)-n)));
}
Any advice appreciated.
p.s. "Write a function rightrot(x,n) that returns the value of the
integer x rotated to the right by n bit positions".
work but using sizeof(x)*8-n to shift the saved bits that would get
lost to the end seems like cheating:
unsigned int rightrot (unsigned int x, int n)
{
return ~(~(x >> n) ^ ((x & ~(~0 << n))<<((sizeof (x)*8)-n)));
}
Any advice appreciated.
p.s. "Write a function rightrot(x,n) that returns the value of the
integer x rotated to the right by n bit positions".