有时候为了方便读取某个寄存器的某些位段,可以将其定义为联合体。将位段作为结构体,与寄存器进行联合。
void test7(void)
{
typedef union
{
unsigned int allBits;
struct
{
unsigned a :4;
unsigned b :4;
unsigned c :4;
unsigned d :4;
};
}Sta;
Sta ss;
ss.allBits=0x1234;
printf("0x%x\n",ss.allBits);
//主意a是低4位,b是高4位
ss.a=0x1;
ss.b=0x2;
ss.c=0x03;
ss.d=0x04;
printf("0x%x\n",ss.allBits);
}
|