c - K&R 2-8 rightrotate (length of integer in bits) -
write function rightrot(x,n) returns value of integer x rotated right n operations
i can't see how can bits , put them in right position without requiring length of x(the amount of bits).
am thinking wrong or need length of x somehow? if so, how go getting length of x?
the code below wrong probaply way, put length_x in demonstrate problem.
i hope can point me in right direction.
#include <stdio.h> unsigned rightrot(unsigned x, int n); main() { unsigned x = 75; int p, n, y; p = 5; n = 3; y = 45; printf("%u\n",rightrot(x,n)); } unsigned rightrot(unsigned x, int n) { oldn = (x & ~(~0 << n)) << length_x; x = x >> n | oldn; return x; }
try:
unsigned int rrot(unsigned int x, int n) { int len; (len=31;len>=0 && !((1 << len) & x);len--); len++; return ((x >> n) | (x << (len - n)) & ((1 << len) - 1); }
the loop should find length in bits of x
. iterates along bits of n, checking if bit high; if so, length of number.
note: no checks made len
greater n
.
Comments
Post a Comment