Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by NavBot

...

Wiki Markup
According to the Java Language Specification \[[JLS 05|AA. Java References#JLS 05]\], section 4.2.2 ""Integer Operations"":

If an integer operator other than a shift operator has at least one operand of type long, then the operation is carried out using 64-bit precision, and the result of the numerical operator is of type long. If the other operand is not long, it is first widened (§5.1.5) to type long by numeric promotion (§5.6). Otherwise, the operation is carried out using 32-bit precision, and the result of the numerical operator is of type int. If either operand is not an int, it is first widened to type int by numeric promotion.

Wiki Markup
The Java Language Specification \[[JLS 05|AA. Java References#JLS 05]\] section 5.6 ""Numeric Promotions"" describes numeric promotion as: 

...

Code Block
int a = some_value;
char b = some_character;

if((a + b) >> 1.1f) {
  //do something
}

Here, b is first converted to int so that the + operator can be applied to operands of the same type. The result of (a+b) is then converted to a float, and the comparison operator is finally applied.

...

Code Block
bgColor#ffcccc
int result = 0;
for(int i = 0; i &lt;< 4; i++) 
  result = ((result &lt;&lt;<< 8) | b[i]);

Compliant Solution

...

Code Block
bgColor#ccccff
int result = 0;
for(int i = 0; i &lt;< 4; i++) 
  result = ((result &lt;&lt;<< 8) | (b[i] &amp; 0xff));

Risk Assessment

...

Wiki Markup
\[[JLS 05|AA. Java References#JLS 05]\] 4.2.2 &quot;"Integer Operations&quot;" and 5.6 &quot;"Numeric Promotions&quot;" 
\[[Findbugs 08|AA. Java References#Findbugs 08]\] &quot;"BIT: Bitwise OR of signed byte value&quot;"

...

EXP07-J. Do not diminish the benefits of constants by assuming their values in expressions&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      04. Expressions (EXP)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      EXP09-J. Use parentheses for precedence of operation