Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Show that the problem can be fixed by switching to unsigned integers, rather than giving up bitwise operators

...

Code Block
bgColor#ccccff
langc
int value;

if (scanf("%d", &value) == 1) {
  if (value % 2 != 0) {
    /* Take action if value is odd */
  }
}

Compliant Solution

Using bitwise operators is safe on unsigned integers:

Code Block
bgColor#ccccff
languagec
unsigned int value;

if (scanf("%d", &value) == 1) {
  if (value & 0x1 != 0) {
    /* Take action if value is odd */
  }
}

Risk Assessment

Incorrect assumptions about integer representation can lead to execution of unintended code branches and other unexpected behavior.

...