位域,段域,联合体,结构体操作寄存器

#include <stdio.h>

typedef int Uint16;
struct  SCICCR_BITS {        // bit    description
   Uint16 SCICHAR:3;         // 2:0    Character length control        
   Uint16 ADDRIDLE_MODE:1;   // 3      ADDR/IDLE Mode control
   Uint16 LOOPBKENA:1;       // 4      Loop Back enable
   Uint16 PARITYENA:1;       // 5      Parity enable   
   Uint16 PARITY:1;          // 6      Even or Odd Parity
   Uint16 STOPBITS:1;        // 7      Number of Stop Bits
   Uint16 rsvd1:8;           // 15:8   reserved
}; 

union SCICCR_REG {
   Uint16              all;
   struct SCICCR_BITS  bit;
};

extern int a =0 ;//定义一个全局变量a 并给初值。
extern int b;   //声明一个变量,在别处定义了
int main(int argc,char *argv[])
{
    union SCICCR_REG SCIICR;
    a=1;
    printf("a=%d\n",a);
    SCIICR.all=0xfe;
    printf("SCIICR=%x\n",SCIICR);
    SCIICR.bit.SCICHAR=0b000;
    printf("SCIICR=%x\n",SCIICR);
    return 0;
}


输出结果:

 

a=1
SCIICR=fe
SCIICR=f8


/usercode/file.cpp:19:12: warning: ‘a’ initialized and declared ‘extern’ [enabled by default]
 extern int a =0 ;//定义一个全局变量a 并给初值。
            ^
/usercode/file.cpp: In function ‘int main(int, char**)’:
/usercode/file.cpp:27:32: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘SCICCR_REG’ [-Wformat=]
     printf("SCIICR=%x\n",SCIICR);
                                ^
/usercode/file.cpp:29:32: warning: format ‘%x’ expects argument of type ‘unsigned int’, but argument 2 has type ‘SCICCR_REG’ [-Wformat=]
     printf("SCIICR=%x\n",SCIICR);
                                ^
分析:union SCICCR_REG {  

                                                Uint16              all;  

                                                 struct SCICCR_BITS  bit;

                                              };

实现了想进行整体操作时就进行整体操作,想进行位操作时就进行位操作。

注意操作时,位域在内存中的高低排序,先定义的位域在内存地址低处。

 

 

 

 

 

 

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值