LongBitsToDouble Called With A Narrow Argument

ID

java.longbitstodouble_narrow_argument

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Code Smell

Language

Java

Tags

code-style, reliability

Description

Reports Double.longBitsToDouble(..) calls whose single argument is provably narrower than long: an int or char literal, or a name declared int, short, byte or char.

Rationale

Double.longBitsToDouble reinterprets the 64 bits of its argument as an IEEE-754 double. A narrower argument is first widened to long the ordinary Java way, sign-extending the value — which changes the bit pattern the method actually reads, most visibly by corrupting the sign and exponent bits:

int i = 0x80003800;
Double.longBitsToDouble(i);   // Bug: widened to a long with a different bit pattern, returns NaN

The call compiles without a warning, since int widens to long implicitly, so the mistake is silent: the method returns a value, just not the one the bit pattern was meant to produce.

Remediation

Give the method a value that is already long, either by declaring the source value long or by reconstructing the bit pattern with a long literal:

long i = 0x80003800L;
Double.longBitsToDouble(i);   // Compliant

© 2026 Xygeni. All rights reserved