Binary to 32 bit floating point

This is a discussion on Binary to 32 bit floating point within the C++ Programming forums, part of the General Programming Boards category; Trying to get to grips with the idea of floating point. Just looking to convert a binary to float. The ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    20

    Binary to 32 bit floating point

    Trying to get to grips with the idea of floating point.
    Just looking to convert a binary to float.

    The last 8 bits are the exponent.. right?
    The most significant bit is on the right?
    The first bit is the sign + - ?

    If someone could give me an example 32 bit conversion it would spell things out for me.

    like this binary for example
    11110000 11001100 10101010 00001111

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    20
    0 100 0010 0101 0101 0110 0110 0010 1010

    Okay worked it out a bit more


    0 100 0010 0101 0101 0110 0110 0010 1010

    The first bit on the left is the sign

    0 100 0010 0101 0101 0110 0110 0010 1010

    The next 7 are the exponent

    0 100 0010 0101 0101 0110 0110 0010 1010

    and this is the mantissa

    so that means that its a positive number ...with an exponent of 5?? .. not sure how i get five (using java applet to work out the mechanics) ..do I minus 127?

    The mantisse is 1.6671803.. which works out as 53.349769592285156 ..not sure how I get here though

    The applet I am using is here FloatPointCalculator for reference

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    IEEE floating point 32 standard is:

    1 bit for sign
    8 bits for exponent, not 7
    23 bits for mantissa, not 24

    01000010 01010101 01100110 00101010

    Sign Bit
    Exponent Bits
    Mantissa Bits
    Devoted my life to programming...

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Forgot to mention:

    float = (signBit ? -1 : 1)*(2^trueExponent)*(1.mantissa)
    Here ^ mean at the power of.

    For example 01000010 01010101 01100110 00101010:

    Code:
    signBit = 0
    biasedExponent = 136 <==> trueExponent = 9
    mantissa = 5596714
    
    yourFloat = 1 * 512 * 1.5596714 = 798.5517568
    I wish the example is corrent, i had no time to test it.
    Devoted my life to programming...

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Just as advice, instead of thinking of "left" and "right" you should think of "most significant" and "least significant." Left and right have nothing to do with anything, other than the way we normally write down binary quantities.

    If you turn your computer upside down the meaning of the bits doesn't change even though you've now exchanged left and right.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    20
    knowing its 8 instead of 7 helps a lot sipher.. thats solved it methinks
    that calculation makes a lot more sense now

    You are quite correct brewbuck, I am familiar already with this idea for integers.. just wasn't sure if it was reverse ordering for floats.. I must say integers are easy in comparison and up until last week I thought floats were similar

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    My example was incorrect.

    You must first make the multiplication and then convert it from binary to decimal.

    Sorry...

    Here is the correction:

    Code:
    signBit = 0
    exponent = biasedExponent = 136 <==> trueExponent = 9
    mantissa = 01010101 01100110 00101010
    
    yourFloat = 1 * (2^9)* 1.010101010110011000101010
    = 1010101010.110011000101010 in binary
    = 682.26154 in decimal
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For the numerical recipes in C types!
    By Smattacus in forum C Programming
    Replies: 5
    Last Post: 10-28-2008, 07:57 PM
  2. Testing Floating Point Return Values
    By jason_m in forum C Programming
    Replies: 5
    Last Post: 08-15-2008, 01:37 PM
  3. 32 bit or 64 bit allignment ?! gcc options??
    By mynickmynick in forum C Programming
    Replies: 3
    Last Post: 07-29-2008, 02:43 AM
  4. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 11:07 AM
  5. Floating point numbers in a binary file
    By frenchfry164 in forum C++ Programming
    Replies: 6
    Last Post: 07-31-2003, 10:04 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21