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?
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
Try this:
System.out.println(Long.toBinaryString(1));
The output is:
1
which indicates that Long.toBinaryString()
discards the leading zeros.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With