
bit operations
I want to rotate right a number by n bits. I don't have the width of the word of the machine and i wan't to solve my problem without finding out the width.(one can always find out the width of a word by counting the number of 1 bits in ~0). Ne ideas ?

You can't rotate a number in C without knowing the width. You can shift, but not rotate.

Since ~0 is all bits set, shifting it right by one and adding one will be the high bit. So you could start with something like this.
Code:
#include <stdio.h>
unsigned int rotate(unsigned int value)
{
unsigned int lsb = value & 1;
value >>= 1;
if ( lsb )
{
value = (~0U >> 1) + 1;
}
return value;
}
int main()
{
unsigned int num = 0xFFFF;
printf("rotate(0x%X) = 0x%X\n", num, rotate(num));
return 0;
}
/* my output
rotate(0xFFFF) = 0x80007FFF
*/
If you can get the high bit, you could get the n highest bits with a little more work.

thanks a lot dave
gaurav