輸入一個字節(jié)內(nèi)的數(shù)(0~255)和移動位數(shù)穆端。輸出移位結(jié)果(要求循環(huán)移位)。
? 提示:系統(tǒng)自帶的移位都是非循環(huán)的
intnum =0;
intmoveStep =0;
printf("請輸入0-255的數(shù)和移動位數(shù):");
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("請輸入一個字節(jié)數(shù)和移動位數(shù):");
? scanf("%d%d", &a, &n);//輸入移動的位數(shù)
?? while (n--) {? //每次循環(huán)移動一位
????? if ((a & 128) == 128) {
????????? a = ? (a << 1) + 1;
??? ? ? }else{
????????? a = ? a << 1;
???? }
?? }
?? printf("a = %d", a);