使用位域来定义位变量,执行位操作,多用于flag变量,YES or NO 的标记

struct bit_t {
        unsigned char   b0:1;                   /* bit0 */
        unsigned char   b1:1;                   /* bit1 */
        unsigned char   b2:1;                   /* bit2 */
        unsigned char   b3:1;                   /* bit3 */
        unsigned char   b4:1;                   /* bit4 */       

        unsigned char   b5:1;                   /* bit5 */
        unsigned char   b6:1;                   /* bit6 */
        unsigned char   b7:1;                   /* bit7 */
};

typedef   union   byte_u  {
       struct bit_t bit;
       char byte;
};

联合变量定义 

union byte_u key2;

位域使用
 #define volEncodeCurrentState0   key2.bit.b0
 #define volEncodeCurrentState1   key2.bit.b1
 #define volEncodeOldState0          key2.bit.b2
 #define volEncodeOldState1          key2.bit.b3
 #define volEncodeIncFlag               key2.bit.b4
 #define volEncodeDecFlag             key2.bit.b5

你可能感兴趣的:(C,基础)