Say I have a bitfield like this:
struct SomeStruct {
uint32_t first : 12;
uint32_t second : 2;
uint32_t third : 18;
};
SomeStruct obj;
I want to assign
obj.second = 3; // Actually, the maximum allowed value
What is the portable way to achieve this? I also don't want to use known bit field width explicitly.
My code used
obj.second = std::numeric_limits<uint32_t>::max()
but clang with -Wbitfield-constant-conversion
gives a warning about it and cpp reference states:
The following properties of bit fields are implementation-defined The value that results from assigning or initializing a bit field with a value out of range, or from incrementing a bit field past its range.
So is assigning -1
or numeric_limits<uint32_t>::max()
actually portable or is there any other way?