Reputation: 317
I am trying to get Java to return a binary representation of a float value. Let's say "0.125".
As far as I'm concerned 0.125 in binary32 equals: 00111110 00000000 00000000 00000000
This is also what I expect Java to return when running the following code:
int bits = Float.floatToIntBits(0.125f);
System.out.println(bits);
However this code returns this value: 1040187392
My two questions are:
How do I get java to return the requested binary representation?
What is the meaning of the number that it returned?
Upvotes: 0
Views: 636
Reputation: 1119
This method return a representation of the specified floating-point value, not the binary value.
Upvotes: 2
Reputation: 3947
You need one more conversion:
int bits = Float.floatToIntBits(0.125f);
System.out.println(Integer.toBinaryString(bits));
Upvotes: 6