2-8 编写一个函数 rightrot(x, n),该函数返回将 x 循环右移(即从最右端 移出的位将从最左端移入)n(二进制)位后所得到的值
Archlinux
GCC 13.1.1 20230429
2023-07-23 19:59:05 星期日
点击查看代码
#include<stdio.h>
#include<stdint.h>
int rightrot( unsigned int x, int n )
{
uint8_t tmp;
while( n > 0 )
{
tmp = (x & 1) << 7; // 0000 000x -> x0000 0000
x = x >> 1; // xxxx xxxs -> 0xxx xxxx
tmp = x | tmp;
n--;
}
return tmp;
}
int main()
{
int x = 129; // 1000 0001 -> 1100 0000(192)
int c = rightrot( x, 1 );
printf("%d\n", c);
return 0;
}
运行截图:

输出正确。
小白刚学习C语言,代码质量不高,欢迎评论。