I’m trying to understand how floating point works. When I run this code :
float number = 3.0f;
int bits = Float.floatToIntBits(number);
System.out.println(Integer.toBinaryString(bits));
I get :
1000000010000000000000000000000
But the correct binary format for 3 should be (according to this converter):
01000000010000000000000000000000
Why isn’t the sign bit (0) displayed by println here?
EDIT : initially, I was usingLong.toBinaryString
instead of Integer.toBinaryString
I would get this kind of result :
float number = -3.0f;
int bits = Float.floatToIntBits(number);
System.out.println(Long.toBinaryString(bits));
output :
1111111111111111111111111111111111000000010000000000000000000000
but using Integer.toBinaryString
return the correct binary format for -3.0f
:
11000000010000000000000000000000