@2018-10-31
负数的左右移位(变量自移位)
验证代码
1 #include2 #include 3 4 /** 5 * @ i <<= 1 & i >>= 1 6 * i为正,右移高位补0,左移低位补0 7 * i为负,右移高位补1,左移低位补0 8 */ 9 10 11 12 void leftRightShift(int number, int bit)13 {14 char buf[32];15 int temp = number;16 17 printf("%d\n", number);18 itoa(number, buf, 2);19 printf("%s\n", buf);20 21 number >>= bit;22 printf("%d\n", number);23 itoa(number, buf, 2);24 printf("%s\n", buf);25 26 number = temp;27 number <<= bit;28 printf("%d\n", number);29 itoa(number, buf, 2);30 printf("%s\n", buf);31 }32 33 34 int main(void)35 {36 int test1 = 1024;37 int test2 = -1024;38 39 40 leftRightShift(test1, 1);41 leftRightShift(test2, 1);42 43 }