# Thread: Float and double to binary

1. Member
Join Date
Sep 2008
Posts
58
Rep Power
0

## Float and double to binary

How do I perform float and doubles to binary. For float I have tried
Java 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
Java Code:
`111111100000000000000000000000`
which is definitely wrong.

2. 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:
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.

3. 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
Java 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);
}
}```

4. Member
Join Date
Sep 2008
Posts
58
Rep Power
0
thanx...
thats what I wanted to know. How to handle the mantissa?

thanx again

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•