- 论坛徽章:
- 0
|
用这个程序测试:
#include <stdio.h>
struct test
{
unsigned short a : 4;
unsigned short b : 5;
unsigned short c : 7;
};
void test_struct()
{
struct test tt;
unsigned short * ptr =(unsigned short *)&tt;
int i;
printf("size of struct = :%d \n", sizeof(struct test));
for(ptr[0]=1, i=0; i<8*sizeof(struct test); i++, ptr[0]<<=1)
{
printf("struct = %04x, a = %02x, b = %02x, c = %02x \n", ptr[0],tt.a,tt.b,tt.c);
printf("low addr =: 0x%02x \n", ((unsigned char*)&tt)[0]);
printf("high addr =: 0x%02x \n", ((unsigned char*)&tt)[1]);
}
}
void main()
{
test_struct();
}
结论:
结论:
1. 在struct中各个bit是依次排的,可以跨越byte
2. 在x86机器(little-endian)上,字节之间的顺序是高高低低
3. 如果bit总数超过一个unsigned short,则在分配一个unsigned short
low addr <---> high addr
high bit <---> low bit
0123456701234567 (2 bytes)
aaaabbbbbccccccc |
|