这个是the C programmg language上面的一个题目:
int _ror2(int x,int n)
{
return ( (~(~0 << n) & x) << (sizeof(int) * 8-n) ) | (x >>n);
}
看起来有点恐怖 实际上是一点一点精简的
int _ror(int x,int n)
{ /*old*/
int temp,longth;
longth = sizeof(int) * 8 - 1;
while(n--){
temp = x & 1;
x >>= 1;
temp <<= longth;
x |= temp;
}
return x;
}
int _ror_2(int x,int n)
{ /* ror2 */
int temp,longth;
longth = sizeof(int) * 8;
temp = ~(~0 << n);
temp &= x;
longth -= n;
temp <<= longth;
x >>= n;
return temp| x;
}