31 lines
1.4 KiB
Plaintext
31 lines
1.4 KiB
Plaintext
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: ``++signed short++``, ``++unsigned short++``, ``++signed char++``, ``++unsigned char++``, ``++signed int++``, ``++unsigned int++`` or ``++_Bool++``
|
||
* In {cpp} before {cpp}14: all enumerated types, as well as ``++signed short++``, ``++unsigned short++``, ``++signed char++``, ``++unsigned char++``, ``++signed int++``, ``++unsigned int++``, ``++signed long++``, ``++unsigned long++``, ``++signed long long++``, ``++unsigned long long++````++ or bool++``
|
||
* In {cpp} starting at {cpp}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
|
||
----
|
||
|
||
|
||
== Compliant Solution
|
||
|
||
----
|
||
unsigned int b:3;
|
||
----
|
||
|
||
|
||
== See
|
||
|
||
* MISRA C:2004, 6.4 - Bit fields shall only be defined to be of type _unsigned int_ or _signed int_.
|
||
* MISRA {cpp}: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
|
||
* https://wiki.sei.cmu.edu/confluence/x/VNYxBQ[CERT, INT12-C.] - Do not make assumptions about the type of a plain int bit-field when used in an expression
|
||
|