MISRAC++2008-5-0-21
In this section:
Synopsis
(Required) Bitwise operators shall only be applied to operands of unsigned underlying type.
Enabled by default
Yes
Severity/Certainty
Low/Medium

Full description
Applications of bitwise operators to signed operands were found. This check is identical to MISRAC2004-12.7.
Coding standards
- CERT INT13-C
Use bitwise operators only on unsigned operands
- MISRA C:2004 12.7
(Required) Bitwise operators shall not be applied to operands whose underlying type is signed.
Code examples
The following code example fails the check and will give a warning:
void example(void) {
int x = -(1U);
x ^ 1;
x & 0x7F;
((unsigned int)x) & 0x7F;
}
The following code example passes the check and will not give a warning about this issue:
void example(void) {
int x = -1;
((unsigned int)x) ^ 1U;
2U ^ 1U;
((unsigned int)x) & 0x7FU;
((unsigned int)x) & 0x7FU;
}