Try:
unsigned int rrot(unsigned int x, int n) {
int len;
for (len=31;len>=0 && !((1 << len) & x);len--);
len++;
return ((x >> n) | (x << (len - n)) & ((1 << len) - 1);
}
The for loop should find the length in bits of x
. It iterates along the bits of n, checking if the bit is high; if so, that is the length of the number.
Note: no checks are made that len
is greater than n
.