Bit-fields can be used to allow flags or other integer values with small ranges to be packed together to save storage space.
It is implementation-defined whether the specifier int
designates the same type as signed int
or the same type as unsigned int
for bit fields. According to the C standard [ISO/IEC 9899:2011], C integer promotions also require that "if an int
can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int
; otherwise, it is converted to an unsigned int
."
This issue is similar to the signedness of plain char
, discussed in INT07-C. Use only explicitly signed or unsigned char type for numeric values. A plain int
bit-field that is treated as unsigned will promote to int
as long as its field width is less than that of int
because int
can hold all values of the original type. This is the same behavior as that of a plain char
treated as unsigned. However, a plain int
bit-field treated as unsigned will promote to unsigned int
if its field width is the same as that of int
. This difference makes a plain int
bit-field even trickier than a plain char
.
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
| 73 S | Fully implemented | |||||||
Compass/ROSE |
|
|
| ||||||
| bitftype | Fully implemented | |||||||
PRQA QA·CQA-C |
| Fully implemented |
...
ISO/IEC 9899:2011 Section 6.3.1.1, "Boolean, characters, and integers," and Section 6.7.2, "Type specifiers"
ISO/IEC TR 24772 "STR Bit representations"
MISRA Rule 12.7
Bibliography
...