Main Content

MISRA C++:2008 Rule 5-0-20

Non-constant operands to a binary bitwise operator shall have the same underlying type

Description

Rule Definition

Non-constant operands to a binary bitwise operator shall have the same underlying type.

Rationale

In a binary bitwise operation, if you use operands that have different underlying types, then it is not clear which type you considered when designing the operation. For instance, in this code:

uint8_t mask = ~(0x10);
uint16_t value;
value ^= mask; // Non-compliant
It is not clear whether you are designing an 8-bit mask or a 16-bit mask. To avoid such confusion, use the same underlying type for nonconstant operands of binary bitwise operators.

Polyspace Implementation

If the nonconstant operands of a binary bitwise operator have different underlying types, Polyspace® raises a violation of this rule.

Troubleshooting

If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.

Examples

expand all

#include<cstdint>

void foo()
{
    uint8_t a, b;
    uint16_t c;
    //...
    uint8_t d = a ^ b; //Compliant
    uint16_t e = a ^ c; //Noncompliant

}

In this example, Polyspace flags the bitwise operation a^c because a and c are of mismatched underlying data types.

Check Information

Group: Expressions
Category: Required

Version History

Introduced in R2013b