我正在定义一组结构来处理一些寄存器,当我定义结构时,我发现定义简单字段的联合和结构的联合之间存在差异。我不确定为什么会出现这种差异:
#include <iostream>
using namespace std;
typedef union
{
uint16_t all_bits;
struct
{
uint16_t a:4, b:4, c:4, d:3, e:1;
};
}
Example1_t;
typedef union
{
uint16_t all_bits;
uint16_t a:4, b:4, c:4, d:3, e:1;
}
Example2_t;
int
main ()
{
Example1_t example1;
Example2_t example2;
example1.all_bits = 0x8BCD;
example2.all_bits = 0x8BCD;
cout << "a " << std::hex << example1.a << " " << example2.a << std::endl;
cout << "b " << std::hex << example1.b << " " << example2.b << std::endl;
cout …Run Code Online (Sandbox Code Playgroud)