Skip to main content

IAR Embedded Workbench for RH850 3.20.x

MISRAC2004-10.5

In this section:
Synopsis

(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.

Enabled by default

Yes

Severity/Certainty

Low/Medium

lowmedium.png
Full description

Detected a bitwise operation on unsigned char or unsigned short, that are not immediately cast to this type to ensure consistent truncation. This check is identical to MISRAC++2008-5-0-10.

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.

MISRA C++ 2008 5-0-10

(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.

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 = ((uint8_t)(~port)) >> 4;
	result_16 = ((uint16_t)(~(uint16_t)port)) >> 4;
}