struct union and endian
2016-09-27 14:12
190 查看
#pragma pack(push, 1)
typedef union {
struct {
# ifdef CS_BIG_ENDIAN
cs_uint16 rsrvd1 :4;
cs_uint16 output_src_sel :3;
cs_uint16 invert_output :1;
cs_uint16 invert_input :1;
cs_uint16 iovddsel_1v2 :1;
cs_uint16 outen_ovr :1;
cs_uint16 outen :1;
cs_uint16 rsrvd2 :2;
cs_uint16 pullup_1k :1;
cs_uint16 pullup_10k :1;
# else /* CS_LITTLE_ENDIAN */
cs_uint16 pullup_10k :1;
cs_uint16 pullup_1k :1;
cs_uint16 rsrvd2 :2;
cs_uint16 outen :1;
cs_uint16 outen_ovr :1;
cs_uint16 iovddsel_1v2 :1;
cs_uint16 invert_input :1;
cs_uint16 invert_output :1;
cs_uint16 output_src_sel :3;
cs_uint16 rsrvd1 :4;
# endif
} bf ;
cs_uint16 wrd ;
} CS4224_GPIO_GPIO1_t;
#pragma pack(pop)
After we execute it in the little endian platform, it will print "0x11";
we can know that the "pullup_10k" field is the first bit of the output data.
typedef union {
struct {
# ifdef CS_BIG_ENDIAN
cs_uint16 rsrvd1 :4;
cs_uint16 output_src_sel :3;
cs_uint16 invert_output :1;
cs_uint16 invert_input :1;
cs_uint16 iovddsel_1v2 :1;
cs_uint16 outen_ovr :1;
cs_uint16 outen :1;
cs_uint16 rsrvd2 :2;
cs_uint16 pullup_1k :1;
cs_uint16 pullup_10k :1;
# else /* CS_LITTLE_ENDIAN */
cs_uint16 pullup_10k :1;
cs_uint16 pullup_1k :1;
cs_uint16 rsrvd2 :2;
cs_uint16 outen :1;
cs_uint16 outen_ovr :1;
cs_uint16 iovddsel_1v2 :1;
cs_uint16 invert_input :1;
cs_uint16 invert_output :1;
cs_uint16 output_src_sel :3;
cs_uint16 rsrvd1 :4;
# endif
} bf ;
cs_uint16 wrd ;
} CS4224_GPIO_GPIO1_t;
#pragma pack(pop)
int main() { CS4224_GPIO_GPIO1_t gpio_cfg; gpio_cfg.bf.output_src_sel = 0; gpio_cfg.bf.invert_output = 0; gpio_cfg.bf.invert_input = 0; gpio_cfg.bf.iovddsel_1v2 = 0; gpio_cfg.bf.outen_ovr = 0; gpio_cfg.bf.outen = 1; gpio_cfg.bf.pullup_1k = 0; gpio_cfg.bf.pullup_10k = 1; printf("0x%x\n", gpio_cfg.wrd); return 0; }
After we execute it in the little endian platform, it will print "0x11";
we can know that the "pullup_10k" field is the first bit of the output data.
相关文章推荐
- 关于 Union,Struct and Class的大小计算问题的思考(转)&union和struct类型的大小计算(转)
- 关于Union,Struct and Class的大小计算问题
- struct and union
- struct、union、enum and sizeof
- struct and union example
- struct and union 的区别
- 结构体struct and 联合体 union
- union and struct
- struct、union、enum and sizeof
- Thinking in C++: C++中关于union、struct、class的区别
- struct union enum的区别
- C# Class and Struct 区别
- 利用union类型测试机器是大端还是小端(Big_endian or Little_endian)
- typedef _packed struct _packed关键字的意思是在struct和union结构中不添加填充字节
- 结构体struct 和 联合union 内存分配
- struct与union内存对齐方式
- struct与union字节对齐问题
- struct、union
- Big endian and Little endian
- struct union enum