Results 1 to 4 of 4
  1. #1
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Rep Power

    Question Float and double to binary

    How do I perform float and doubles to binary. For float I have tried
    Java Code:
    But the answer doesnt seem to be right. When I input a value '1' into the above function, I get an output
    Java Code:
    which is definitely wrong.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    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. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Rep Power


    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. #4
    rosh72851 is offline Member
    Join Date
    Sep 2008
    Rep Power


    thats what I wanted to know. How to handle the mantissa?

    thanx again

Similar Threads

  1. Put float value to double
    By chamalsl in forum New To Java
    Replies: 3
    Last Post: 09-20-2008, 05:52 AM
  2. String to Float
    By durahman in forum New To Java
    Replies: 2
    Last Post: 02-12-2008, 01:17 AM
  3. Float to String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-29-2007, 06:08 PM
  4. Replies: 2
    Last Post: 11-16-2007, 02:24 PM
  5. Class float
    By Peter in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 02:17 AM

Posting Permissions

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