# Float and double to binary

• 09-24-2008, 01:34 AM
rosh72851
Float and double to binary
How do I perform float and doubles to binary. For float I have tried
Code:

`Integer.toBinaryString(Float.floatToRawIntBits(value));`
But the answer doesnt seem to be right. When I input a value '1' into the above function, I get an output
Code:

`111111100000000000000000000000`
which is definitely wrong.
• 09-24-2008, 03:23 AM
Norm
was value an int? Does java convert it to an int to float to match the parms for that method?
The API doc for that method says:
Quote:

Bit 31 (the bit that is selected by the mask 0x80000000) represents the sign of the floating-point number. Bits 30-23 (the bits that are selected by the mask 0x7f800000) represent the exponent. Bits 22-0 (the bits that are selected by the mask 0x007fffff) represent the significand (sometimes called the mantissa) of the floating-point number.
• 09-24-2008, 05:01 AM
hardwired
To pick apart/interpret the value returned from the Float.floatToIntBits method see its description in the Method Detail section of the Float class api.
Also, a couple of resouce links:
IEEE 754-1985
IEEE 754 Converter
Code:

```public class Test {     public static void main(String[] args) {         float f = -118.625f;         int intBits = Float.floatToIntBits(f);         int rawIntBits = Float.floatToRawIntBits(f);         System.out.printf("f = %f  intBits = %d  " +                           "rawIntBits = %d%n", f, intBits, rawIntBits);         float toFloat = Float.intBitsToFloat(intBits);         System.out.printf("toFloat = %f%n", toFloat);         int sign    = intBits & 0x80000000;         int exponent = intBits & 0x7f800000;         int mantissa = intBits & 0x007fffff;         System.out.printf("sign = %d  exponent = %d  mantissa = %d%n",                           sign, exponent, mantissa);         String binarySign = Integer.toBinaryString(sign);         String binaryExponent = Integer.toBinaryString(exponent);         String binaryMantissa = Integer.toBinaryString(mantissa);         System.out.printf("binarySign    = %s%nbinaryExponent = %s%n" +                           "binaryMantissa = %s%n", binarySign,                           binaryExponent, binaryMantissa);     } }```
• 09-24-2008, 06:29 AM
rosh72851
thanx...
thats what I wanted to know. How to handle the mantissa?

thanx again