MISRA C++:2023 Rule 12.2.3
A named bit-field with signed integer type shall not have a length of one bit
Since R2024b
Description
Rule Definition
A named bit-field with signed integer type shall not have a length of one bit.
Rationale
A bit-field with a signed integer type and a bit-width of one bit can behave in an
unexpected way, can have a value that the developer does not expect, and is likely to be an
error. For example, signed integer types of a fixed width such as
std::int16_t
have a two's complement representation. This
representation uses the most significant bit to represent the sign of the value, which
leaves not bit to represent the magnitude of the value. In this case, the bit-field can
represent only 0 or -1.
The rule does not apply to anonymous signed bit-fields, regardless of their length, because those bit-fields are inaccessible.
Polyspace Implementation
The coding rule checker reports a violation when you declare a named bit-field with a signed integer type and a length of one bit.
Troubleshooting
If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Classes |
Category: Required |
Version History
Introduced in R2024b