c位域操作
#include <stdio.h> int main() { struct fields { unsigned short a:7; unsigned short b:5; unsigned char c:4; }fs; fs.a = 0x8F; fs.b = 0x1A; fs.c = 0x19; printf("%#x, %#x, %#x \n",fs.a, fs.b, fs.c); } 用 gcc -g -o bitfield bitfield.c 编译并运行,结果为 [root@localhost]# ./bitfield 0xf, 0x1a, 0x9
我的理解:a占用0x8F的4位,b占用0x1A的5位,c占用0x19的4位。