Versions Compared

Key

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

...

The intent of the expression in this noncompliant code example is to add the variable OFFSET with the result of the bitwise and between x and MASK.

Code Block
bgColor#FFCCCC

public static final int MASK = 1337;
public static final int OFFSET = -1337;

public static int computeCode(int x) {
return x & MASK + OFFSET;
}

Because of operator precedence rules, the expression is parsed as

Code Block
bgColor#FFCCCC
x & (MASK + OFFSET)

Which gets evaluated as

Code Block
bgColor#FFCCCC
x & (1337 - 1337)

Resulting in 0.

Compliant Solution

In this compliant solution, parentheses are used to ensure the expression evaluates as expected.

...