...
Noncompliant Code Example
| Wiki Markup |
|---|
This noncompliant code example shows integer promotion when the bit-wise OR operator is used. The byte array element is sign extended to 32 bits before it is used as an operand. If it |
containd contained the value {{0xff}}, now it would contain {{0xffffffff}} \[[Findbugs 08|AA. Java References#Findbugs 08]\]. |
| Code Block |
|---|
|
int result = 0;
for(int i = 0; i < 4; i++)
result = ((result << 8) | b[i]);
|
...
| Wiki Markup |
|---|
\[[JLS 05|AA. Java References#JLS 05]\] 4.2.2 "Integer Operations" and 5.6 "Numeric Promotions"
\[[Findbugs 08|AA. Java References#Findbugs 08]\] "BIT: Bitwise OR of signed byte value" |
...
EXP07-J. Do not diminish the benefits of constants by assuming their values in expressions 04. Expressions (EXP) EXP09-J. Use parentheses for precedence of operation