代码:
#include<stdio.h>
int main(){
unsigned int a , b , c ;
unsigned short a1 , b1 , c1 ;
a = 0xFFFFFFFF;
b = (a << 16) >> 16;
c = (a >> 16) << 16;
a1 = 0xFFFF;
b1 = (a1 << 128) >> 128; //这里的数字无论多大,只要相等,结果就是:0xffff
c1 = (a1 >> 8) << 8;
printf("b = 0x%8.8x\n" , b);
printf("c = 0x%8.8x\n" , c);
printf("b1 = 0x%4.4x\n" , b1);
printf("c1 = 0x%4.4x\n" , c1);
return 0;
}
输出:b = 0x0000ffff
c = 0xffff0000
b1 = 0xffff
c1 = 0xff00
为什么short先左移再直接右移就不会发生变化,而int就会将多余的变为0.