将 1 位宽的位域设置为 2 是否意味着位域已设置或未设置?

Does setting a 1-bit wide bitfield to 2 mean the bitfield is set or unset?

所以我有一个像这样的位域:

unsigned int foobar:1;

然后我用这段代码设置它

uint32_t code = loadCode();
structure.foobar = code & 2;

那么,如果 code 设置为 2,这是否意味着 foobar 设置为 1、0 或未定义?我使用的确切标准实际上是 C++11,而不是纯 C。

[expr.ass]/6:

When the left operand of an assignment operator is a bit-field that cannot represent the value of the expression, the resulting value of the bit-field is implementation-defined.

同样,对于initialization

When initializing a bit-field with a value that it cannot represent, the resulting value of the bit-field is implementation-defined.

这是DR 1816添加的。作为修复标准bug的缺陷报告,具有事实上的追溯性。