Why is this an issue?
Some types are not very well suited for use in a bit-field, because their behavior is implementation-defined. When defining a bit-field, you should
stick to the following safe and portable types:
- In C:
unsigned int or
- In C++ before C++14: all enumerated types, as well as
unsigned long long
- In C++ starting at C++14: all enumerated and integral types
Noncompliant code example
// Assuming we are in C
int b:3; // Noncompliant - may have the range of values 0..7 or -4..3
unsigned int b:3;
- MISRA C:2004, 6.4 - Bit fields shall only be defined to be of type unsigned int or signed int.
- MISRA C++:2008, 9-6-2 - Bit-fields shall be either bool type or an explicitly unsigned or signed integral type.
- MISRA C:2012, 6.1 - Bit-fields shall only be declared with an appropriate type
- CERT, INT12-C. - Do not make assumptions about the type of a plain int bit-field
when used in an expression