Details
The reference for bit fields at cppreference presents the following example:
#include <iostream> struct S { // three-bit unsigned field, // allowed values are 0...7 unsigned int b : 3; }; int main() { S s = {7}; ++s.b; // unsigned overflow (guaranteed wrap-around) std::cout << s.b << '\n'; // output: 0 }
Emphasis on the guaranteed wrap-around comment.
However, WG21 CWG Issue 1816 describe some possible issues with unclear specification of bit field values, and [expr.post.incr]/1 in the latest standard draft states:
The value of a postfix ++ expression is the value of its operand. ...
If the operand is a bit-field that cannot represent the incremented value, the resulting value of the bit-field is implementation-defined.
I'm unsure, however, if this applies also for wrap-around of unsigned bit fields.
Question
- Is overflow of an unsigned bit field guaranteed to wrap-around?