Dennishofken
Dennishofken

Reputation: 317

What does Float.floatToRawIntBits(); return in java?

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:

Upvotes: 0

Views: 636

Answers (2)

cvdr
cvdr

Reputation: 1119

This method return a representation of the specified floating-point value, not the binary value.

Upvotes: 2

Schidu Luca
Schidu Luca

Reputation: 3947

You need one more conversion:

int bits = Float.floatToIntBits(0.125f);
System.out.println(Integer.toBinaryString(bits));

Upvotes: 6

Related Questions