
...
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 typelong
. If the other operand is notlong
, it is first widened (§5.1.5) to typelong
by numeric promotion (§5.6). Otherwise, the operation is carried out using 32-bit precision, and the result of the numerical operator is of typeint
. If either operand is not anint
, it is first widened to typeint
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 | ||
---|---|---|
| ||
int result = 0; for(int i = 0; i << 4; i++) result = ((result <<<< 8) | b[i]); |
Compliant Solution
...
Code Block | ||
---|---|---|
| ||
int result = 0; for(int i = 0; i << 4; i++) result = ((result <<<< 8) | (b[i] & 0xff)); |
Risk Assessment
...
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