5.6 Shift operators

5.6.1 Do not use bitwise operators with signed operands

Use of signed operands with bitwise operators is in some cases subject to undefined or implementation defined behavior. Therefore, bitwise operators should only be used with operands of unsigned integral types.

#include <cstdint>
               
void foo (int32_t i)
{
int32_t r = i << -1;    // @@- Non-Compliant: undefined behavior -@@
r = -1 >> 1;            // @@- Non-Compliant: implementation defined -@@
r = ~0;                 // @@- Non-Compliant: implementation defined -@@
     
uint32_t u = (-1) & 2u; // @@- Non-Compliant: implementation defined -@@
u = (-1) | 1u;          // @@- Non-Compliant: implementation defined -@@
u = (-1) ^ 1u;          // @@- Non-Compliant: implementation defined -@@
}

References

  • HIC++ v3.3 – 10.11

Click here for references