doubleToLongBits Reveals IEEE 754 Bits

doubleToLongBits exposes the IEEE 754 layout: plus and minus zero differ only in the sign bit, every NaN canonicalizes, and the raw form keeps payloads.

JavaData Integrity

Code

snippet
public class IeeeBits {

    public static void main(String[] args) {
        long bits = Double.doubleToLongBits(1.0);
        System.out.println("bits(1.0)   = " + Long.toHexString(bits)); // 3ff0000000000000
        System.out.println("sign bit    = " + (bits >>> 63));

        // 0.0 与 -0.0 用 == 相等,位模式却不同:只差一个符号位
        System.out.println("bits(+0.0)  = " + Long.toHexString(Double.doubleToLongBits(0.0)));
        System.out.println("bits(-0.0)  = " + Long.toHexString(Double.doubleToLongBits(-0.0)));

        // doubleToLongBits 把全部 NaN 归一成规范位型;raw 保留原始载荷
        System.out.println("canon NaN   = " + Long.toHexString(Double.doubleToLongBits(Double.NaN)));
        double payloadNan = Double.longBitsToDouble(0x7ff8000000000001L);
        System.out.println("raw  NaN    = " + Long.toHexString(Double.doubleToRawLongBits(payloadNan)));

        // 逆变换:位型还原为 double
        System.out.println("back to d   = " + Double.longBitsToDouble(0x3ff0000000000000L));
    }
}

What this snippet does

The snippet is organized around the class IeeeBits and the function main.

What to watch for

  • The imports (if any) reach only standard-library modules.

Applying it

Entry signature: public static void main(String[] args)

Output lines are prefixed (bits(1.0) =, sign bit =, bits(+0.0) =), so the effect of every step stays visible.

Statements that matter

double payloadNan = Double.longBitsToDouble(0x7ff8000000000001L);
long bits = Double.doubleToLongBits(1.0);

Interactive Tools for This Topic

FAQ

What does doubleToLongBits Reveals IEEE 754 Bits print when run?
Run it as a complete program — the output lines carry labels such as `bits(1.0) =`, `sign bit =`, `bits(+0.0) =`.