测试1:
#include <stdio.h> #include <string.h> int main() { union unCMD{// First Command Byte (CMD) as a bit-fieldunsigned short usValue;struct{// The eight bits, starting with bit0.unsigned char one : 1;unsigned char two: 1;unsigned char three: 1;unsigned char four: 1;unsigned char five : 1;unsigned char six: 1;unsigned char seven : 1;unsigned char eight : 1;unsigned char nine;} field;};union unCMD command1; command1.usValue = 0x1122; printf("%xrn",command1.usValue); printf("one :%d two:%d three:%d four:%d five:%d six:%d seven:%d eight:%d nine:%xn",command1.field.one,command1.field.two,command1.field.three,command1.field.four,command1.field.five,command1.field.six,command1.field.seven,command1.field.eight,command1.field.nine); return 1; }执行结果:
1122one :0 two:1 three:0 four:0 five:0 six:1 seven:0 eight:0 nine:11
分析 将 0x1122 的高8位 赋值给 nine;
将低八位 按位从低到高赋给 one -> eight (0x22 二进制 0010 0010)
测试2:
将位域类型修改为 short:
#include <stdio.h> #include <string.h> int main() { union unCMD{// First Command Byte (CMD) as a bit-fieldunsigned short usValue;struct{// The eight bits, starting with bit0.unsigned short one : 1;unsigned short two: 1;unsigned short three: 1;unsigned short four: 1;unsigned short five : 1;unsigned short six: 1;unsigned short seven : 1;unsigned short eight : 1;unsigned char nine;} field;};union unCMD command1; command1.usValue = 0x1113; printf("%xrn",command1.usValue); printf("one :%d two:%d three:%d four:%d five:%d six:%d seven:%d eight:%d nine:%xn",command1.field.one,command1.field.two,command1.field.three,command1.field.four,command1.field.five,command1.field.six,command1.field.seven,command1.field.eight,command1.field.nine); return 1; }
执行结果:
1113one :1 two:1 three:0 four:0 five:1 six:0 seven:0 eight:0 nine:0nine的值为0 ; 只是将0x13赋值给 one->eight;
one->eight 各占一个bit 为什么 nine没有赋值成功?
union中各个变量是共用内存空间。上例中,usValue和结构体共用内存空间,
如果定义的 变量还是char 位域和 char ,那么两个char的长度刚好是一个short.
如果定义的 变量还是short 位域和 char ,那么内存空间就变成了 24bit 虽然只占用了short的前8个 ,但是内存中有8个bit是 有被赋值但是不可用, 后面的char 也就不会赋值
测试3:
#include <stdio.h> #include <string.h> int main() { union unCMD{// First Command Byte (CMD) as a bit-fieldunsigned short usValue;struct{// The eight bits, starting with bit0.unsigned short one : 1;unsigned short two: 1;unsigned short three: 1;unsigned short four: 1;unsigned short five : 1;unsigned short six: 1;unsigned short seven : 1;unsigned short eight : 1;unsigned short one1 : 1;unsigned short two1: 1;unsigned short three1: 1;unsigned short four1: 1;unsigned short five1 : 1;unsigned short six1: 1;unsigned short seven1 : 1;unsigned short eight1 : 1;unsigned char nine;} field;};union unCMD command1; command1.usValue = 0xFF13; printf("%xrn",command1.usValue); printf("one1 :%d two1:%d three1:%d four1:%d five1:%d six1:%d seven1:%d eight1:%d nine:%xn",command1.field.one1,command1.field.two1,command1.field.three1,command1.field.four1,command1.field.five1,command1.field.six1,command1.field.seven1,command1.field.eight1,command1.field.nine); return 1; }执行结果:
ff13
one1 :1 two1:1 three1:1 four1:1 five1:1 six1:1 seven1:1 eight1:1 nine:0
one:1 two:0 three:0 four:0 five:1 six:1 seven:0 eight:0
测试2和测试3的效果是等同的.