A value of 1024 has another bit in binary representation, a value of 1

The output of the following code:

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

There is:

 11111111110000000000000000000000000000000000000000000000000000 100000010010000000000000000000000000000000000000000000000000000 

Why does this code print another bit for the value 1024?

+4
source share
2 answers

Why does this code print another bit for the value 1024?

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

eg. 000000000000000000000000000000000000000000000000000000000000000000000000000000000001 printed as 1

+4
source

Try the following:

 System.out.println(Long.toBinaryString(1)); 

Output:

 1 

which indicates that Long.toBinaryString() discards leading zeros.

+3
source

Source: https://habr.com/ru/post/1480440/


All Articles