请帮我理解这段代码的含义.我第一次看到这种用法
typedef enum {
E_1_DEFAULT = 0,
E_1_1,
E_1_2,
E_1_3,
E_1_4,
E_1_5,
E_1_255 = 255 //needs at least 8Bit
} APPLICATION_ENUM_1;
typedef enum {
E_2_DEFAULT = 0,
E_2_1,
E_2_2,
E_2_3 //needs at least 2Bit
} APPLICATION_ENUM_2;
typedef enum {
E_3_DEFAULT = 0,
E_3_1,
E_3_2,
E_3_3,
E_3_4,
E_3_5,
E_3_666 = 666 //needs at least 10Bit
} APPLICATION_ENUM_3;
typedef struct {
APPLICATION_ENUM_3 var3:10; // 10Bit
APPLICATION_ENUM_1 var1:8; // 18Bit
APPLICATION_ENUM_2 var2:2; // 20Bit
uint8 unnused_1:4; // fill up the last whole byte -> 24Bit = 3byte
} APPLICATION_RAM;;
Run Code Online (Sandbox Code Playgroud)
根据C++标准
枚举的基础类型是一个整数类型,可以表示枚举中定义的所有枚举器值.
现在让我们看一下第一个定义
typedef enum {
E_1_DEFAULT = 0,
E_1_1,
E_1_2,
E_1_3,
E_1_4,
E_1_5,
E_1_255 = 255 //needs at least 8Bit
} APPLICATION_ENUM_1;
Run Code Online (Sandbox Code Playgroud)
定义E_1_255 = 255确保所有值0 to 255都可以由此枚举类型表示,并且您需要至少8位来表示来自的所有值0 to 255
typedef struct {
APPLICATION_ENUM_3 var3:10; // 10Bit
APPLICATION_ENUM_1 var1:8; // 18Bit
APPLICATION_ENUM_2 var2:2; // 20Bit
uint8 unnused_1:4; // 24Bit = 3byte
} APPLICATION_RAM;
Run Code Online (Sandbox Code Playgroud)
上面的结构使用了很少使用的位域构造.基本上声明APPLICATION_RAM为具有的结构
var3 成员是10位var1 8位成员var2 成员是2位unnused_1 4位成员