Skip to main content

IAR Embedded Workbench for Arm 9.70.x

MISRAC2004-12.7

In this section:
Synopsis

(Required) Bitwise operators shall not be applied to operands whose underlying type is signed.

Enabled by default

Yes

Severity/Certainty

Low/Medium

lowmedium.png
Full description

Applications of bitwise operators to signed operands were found. This check is identical to MISRAC++2008-5-0-21.

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.

MISRA C++ 2008 5-0-21

(Required) Bitwise operators shall only be applied to operands of unsigned underlying type.

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;
}