struct Type {
uint8_t var : 3;
};
int main()
{
struct Type bar;
bar.var = 1;
uint8_t baz = bar.var << 5;
}
根据标准,左移位大于左操作数类型的宽度是未定义的行为:
6.5.7 Bitwise shift operators/3对每个操作数执行整数提升.结果的类型是升级的左操作数的类型.If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.
但是位字段呢?这里不是至少有八个比特吗?