寫出下列程式在X86上的運作結果
struct mybitfields
{
unsigned short a : 4;
unsigned short b : 5;
unsigned short c : 7;
} test
void main( void )
{
int i;
test.a = 2;
test.b = 3;
test.c = 0;
i = *(( short *)&test);
printf ( "%d\n" , i);
}
在結構體中,冒号想防禦配置設定了空間,定義了a為4個位元組,b 五個,C六個
在小端存儲模式下,
test 15 14 13 12 11 10 9 |8 7 6 5 4 |3 2 1 0
test.a | |0 0 1 0
test.b |0 0 0 1 1 |
test.c 0 0 0 0 0 0 0 | |
i = *((short *)&test);從首位址開始,取出兩個位元組的資料,即0x0032, 在轉換為d%(也就是int型),為50