Joyson
Joyson

Reputation: 1653

How to convert negative double value to binary value in 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.

Upvotes: 2

Views: 1519

Answers (3)

chiccodoro
chiccodoro

Reputation: 14716

Floating point values (such as double) are represented completely different from integer values. From integer values you are used to them being represented as a zero-padded (for positive numbers) binary number which represents the same number in base 2. This does not hold true for floating point values.

By definition floating point values have a floating decimal point, so the binary number has to encode the information on where the decimal point is. This is done by reshaping the number to the following:

(+/-) x * 2^y   whereas 0 <= x < 2 (= 10 base 2)

(+/-) is the sign which takes one bit. x is the mantissa, y the exponent. So the binary representation has to encode three pieces in the end.

Since 0 <= x < 2, x always starts with 1.something, so the 1. need not be in the representation. Only the part after the decimal point is saved, which is considered the significand.

In your example, 2.0 is reshaped to:

+ 1.0 * 2 ^ 1

Sign: + Significand: 0 Exponent: 1

This happens to be represented as

0 10000000000 0000000000000000000000000000000000000000000000000000

For negative numbers the sign is - which is represented as a leading 1:

1 10000000000 0000000000000000000000000000000000000000000000000000

Note that your original posts lists a number that is missing the leading 0 which is mathematically correct but misleading when you try to capture the binary representation:

  10000000000 0000000000000000000000000000000000000000000000000000 =
0 10000000000 0000000000000000000000000000000000000000000000000000

There are some more details to understand and keep in mind. For example the exponent can be negative, too. You can find a more elaborate explanation in the Floating Point Wikipedia article. binaryconvert.com as referenced by Joachim can also help you understand the representation.

Upvotes: 4

Rosdi Kasim
Rosdi Kasim

Reputation: 25986

Is this what you want?

System.out.println(Long.toBinaryString(Double.valueOf(2.0).longValue()));
System.out.println(Long.toBinaryString(Double.valueOf(-2.0).longValue()));

Upvotes: 0

Joachim Isaksson
Joachim Isaksson

Reputation: 180927

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.

Upvotes: 5

Related Questions