输入一个字节内的数(0~255)和移动位数。输出移位结果(要求循环移位)。
提示:系统自带的移位都是非循环的
intnum =0;
intmoveStep =0;
printf("请输入0-255的数和移动位数:");
scanf("%d %d", &num, &moveStep);
moveStep = moveStep %8;
//截掉前27位
unsignedcharcharNum = num;
//低位往高位挪
unsignedcharleftMoveNum = charNum << moveStep;
//高位往低位挪
unsignedcharrightMoveNum = charNum >> (8- moveStep);
//合到一起
unsignedcharresult = leftMoveNum | rightMoveNum;
printf("result = %d",result);
方法二:
int n;
unsigned char a = 0;
printf("请输入一个字节数和移动位数:");
scanf("%d%d", &a, &n);//输入移动的位数
while (n--) { //每次循环移动一位
if ((a & 128) == 128) {
a = (a << 1) + 1;
}else{
a = a << 1;
}
}
printf("a = %d", a);