MISRAC++2008-5-0-10
Synopsis
(Required) If the bitwise operators ~ and << are applied to an operand with an underlying type of unsigned char or unsigned short, the result shall be immediately cast to the underlying type of the operand.
Enabled by default
Yes
Severity/Certainty
Low/Medium

Full description
A bitwise operation on unsigned char or unsigned short was found, that was not immediately cast to this type to ensure consistent truncation. This check is identical to MISRAC2004-10.5.
Coding standards
- MISRA C:2004 10.5
(Required) If the bitwise operators ~ and << are applied to an operand of underlying type unsigned char or unsigned short, the result shall be immediately cast to the underlying type of the operand.
Code examples
The following code example fails the check and will give a warning:
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
void example(void) {
uint8_t port = 0x5aU;
uint8_t result_8;
uint16_t result_16;
uint16_t mode;
result_8 = (~port) >> 4;
}
The following code example passes the check and will not give a warning about this issue:
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
void example(void) {
uint8_t port = 0x5aU;
uint8_t result_8;
uint16_t result_16;
uint16_t mode;
result_8 = ( static_cast< uint8_t > (~port) ) >> 4; // Compliant
result_16 = ( static_cast < uint16_t > ( static_cast< uint16_t > ( port ) << 4 ) & mode ) >> 6; // Compliant
}