代碼如下:
#include <stdio.h>
int main()
{
short a=-1;
unsigned int b=a;
int c=a;
printf("%x\n", b);
printf("%d\n", c);
a=1;
b=a;
c=a;
printf("%x\n", b);
printf("%d\n", c);
return 0;
}
輸出為:
ffffffff
-1
1
1
說明,把short型數據賦值給int或者unsigned int型數據時,如果short型數據是負的,則擴展的比特位全是1,;如果short型數據是正的,則擴展的比特位全是0.
代碼如下:
#include <stdio.h>
int main()
{
unsigned short a=-1;
unsigned int b=a;
int c=a;
printf("%x\n", b);
printf("%d\n", c);
a=1;
b=a;
c=a;
printf("%x\n", b);
printf("%d\n", c);
return 0;
}
輸出為:
ffff
65535
1
1
說明,把unsigned short型數據賦值給int或者unsigned int型數據時,則擴展的比特位全是0