写出下列程序在X86 上的运行结果:
struct mybitfields {
unsigned short a:4;
unsigned short b:5;
unsigned short c:7;
}test;
void main(){
int i;
test.a=2;
test.b=3;
test.c=0;
i=*((short*)&test);
printf("%d\n",i);
system("pause");
}
输出结果:50
分析:
a:4表示a占4位
b:5表示b占5位
c:7表示c占7位
内存分配方式是从低位到高位(a在低位,c在高位)
当有赋值:test.a=2(0010) test.b=3(00011) test.c=0(0000000)
高 ---------- 32 位--------> 低
00000000 00000000 00000000 00110010
00000000 00000000 |+++++++|====--|--
--------------------—|c------|b------|a-
---------------------|0------|3------|-2--
X86 上市16位CPU,对应的short占两个字节。
拼成00000000 00110010 这个二进制值(short)为十进制50