javatypesdoublebit-representation

Value of 1024 has one bit more in binary representation that value of 1


The output of following code:

System.out.println( Long.toBinaryString( Double.doubleToRawLongBits( 1 ) ) );
System.out.println( Long.toBinaryString( Double.doubleToRawLongBits( 1024 ) ) );

Is:

11111111110000000000000000000000000000000000000000000000000000
100000010010000000000000000000000000000000000000000000000000000

Why this code prints one bit more for value of 1024?


Solution

  • Why this code prints one bit more for value of 1024?

    This is because leading 000000's are dropped by Long.toBinaryString. A double is always 64-bit, but it can have up to 63 leading zeros.

    e.g. 000000000000000000000000000000000000000000000000000000000000000000000001 is printed as 1