假设我有
typedef struct {
unsigned short bar : 1;
} foo_bf;
typedef union {
unsigned short val;
foo_bf bf;
} foo_t;
如何从类型(例如 uint16_t)正确地为该位域赋值?
uint16_t myValue = 1;
foo_t foo;
foo.bf.bar = myValue
运行 PC-Lint,这会变成 MISRA 错误: 表达式分配给更窄或不同的基本类型。
我试图限制使用的位数,但没有成功。
foo.bf.bar = (myValue 0x1U)
如果我必须使用 uint16_t 值作为原点,是否有机会使其符合 MISRA 标准?