Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This noncompliant code example demonstrates how performing bitwise operations on integer types smaller than int may have unexpected results.

Code Block
bgColor#FFcccc
langc
uint8_t port = 0x5a;
uint8_t result_8 = ( ~port ) >> 4;

...

In this compliant solution, the bitwise complement of port is converted back to 8 bits. Consequently, result_8 is assigned the expected value of 0x0aU.

Code Block
bgColor#ccccff
langc
uint8_t port = 0x5a;
uint8_t result_8 = (uint8_t) (~port) >> 4;

...