无符号长整型数x的循环右移
无符号长整型数x,循环右移位数n的取值范围是:。比如,x=0x12345678,n=4,则循环右移4位后,x=0x81234567。
程序代码
typedef unsigned long int u32;
u32 rotate_right(u32 x, int n);void main()
{static u32 rox,x=0x12345678;static int n;n=20;rox=rotate_right(x, n);while(1);
}
u32 rotate_right(u32 x, int n)
{u32 temp1,temp2,temp;temp1=temp2=x;while(n) //实现1位循环右移的基础上,实现n位循环右移{temp1<<=31;temp2>>=1;temp=temp1|temp2;temp1=temp;temp2=temp;n--;}return temp;
}
运行结果