Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert negative double value to binary value in java?

Tags:

java

The following is the double value

-1.2316741412499997E8

The following is the method i am using :

Long.toBinaryString(Double.doubleToRawLongBits(-1.2316741412499997E8))
output: 1100000110011101010111011000101011011000011111111111111111111110

However when i tried doing it with value 2 i get the following output as shown below which is wrong.

Long.toBinaryString(Double.doubleToRawLongBits(2.0))
output: 100000000000000000000000000000000000000000000000000000000000000

Will someone tell me how do i convert a negative double value to binary.Thanks in advance.

like image 954
Joyson Avatar asked Mar 27 '26 15:03

Joyson


1 Answers

I'm not sure what result you would like, but the result for 2.0 is absolutely correct.

Note that the number it gives is not zero padded, so it may seem to have the sign bit set, but the bit set is the highest bit of the exponent.

like image 141
Joachim Isaksson Avatar answered Mar 29 '26 05:03

Joachim Isaksson