I am generating sequence of numbers of type double like 0.9913963644564902 0.0341175990344773 0.13181105852355268 0.45773616980747556 and I have to convert it to binary representation in java.
Asked
Active
Viewed 6,624 times
2
-
1What do you mean by "convert to binary"? What output are you expecting for this example? – Oliver Charlesworth Dec 22 '13 at 13:57
-
You probably want to use a DataOutputStream. Read the javadoc. – JB Nizet Dec 22 '13 at 13:59
-
The author most probably wants the actual floating point number in binary format (infinite precision). I do not think he wants to have all fields of the IEEE 8 byte double as a binary integer. – Tadas S Dec 22 '13 at 14:07
-
@afk5min: But that's not something he/she can have, as most finite decimal expansions have infinite binary expansions. – Oliver Charlesworth Dec 22 '13 at 14:13
1 Answers
2
This will help you. I have tried out using one of your input double value - 0.9913963644564902
public static void main(String[] args){
double d = 0.9913963644564902;
System.out.println("0b"+Long.toBinaryString(Double.doubleToRawLongBits(d)));
}
The output would be
run:
0b11111111101111101110011000010011011110010101101101100001110011
BUILD SUCCESSFUL (total time: 0 seconds)

Keerthivasan
- 12,760
- 2
- 32
- 53
-
Why would you think the author would want a binary INTEGER as a result? – Tadas S Dec 22 '13 at 14:09
-
1
-
Yes, for -0.9913963644564902, output would be *run: 0b1011111111101111101110011000010011011110010101101101100001110011 BUILD SUCCESSFUL (total time: 0 seconds)* – Keerthivasan Dec 22 '13 at 14:12
-
-