1
|
写出下列程序在X86上的运行结果
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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