编写一个移位函数,使移位函数既能循环左移又能循环右移。参数n大于0时便是左移,参数n小于0时便是右移
nsigned move(unsigned value, int n)
{
unsigned z;
if (n > 0) {
// 循环左移
z = (value >> (32 - n)) |(value << n);
}else
{
// 循环右移
n = -n;
z = (value << (32 - n)) |(value >> n);
}
return z;
}
int main(int argc, const char * argv[])
{
unsigned a;
int n;
printf("请输入一个八进制输:\n");
scanf("%d", &a);
printf("请输入要移位的位数:\n");
scanf("%d", &n);
printf("移位后的结果是:%o\n", move(a, n));
return 0;
}
|
|