How can I convert 32-bit binary number to floating point number?

17,257

Solution 1

Assuming you are a looking for a single floating point here is the format:

First digit is sign, next 8 are the exponent and finally, the last 23 are the significand.

For your number (00111111010000000000000000000000)

Sign: 0 is +
Exponent: 01111110 is -1 (126-127)
Significand: 10000000000000000000000 is 1.5 (The 'invisible' first bit gives you 1, then the second bit (only one set) is 0.5, the next one would have been 0.25, then 0.125 and so on)

You then calculate the value as such:

sign * 2^exp * Significand

1 * 2^-1 * 1.5
1 * 0.5 * 1.5
0.75

You floating point number is equal to 0.75.

Solution 2

If you do some looking around at wikipedia and other places, what you will learn is that the floating point standards (there are different ones, IEEE754 being the most commonly known because it is used on desktops, etc (dsp's will have their own sometimes which are faster to compute)) will "normalize" the number. Floating point is all about where the decimal point is, a non-floating point integer has an implied decimal point to the right of the number

00111111010000000000000000000000. <--- here

other than all zeros to normalize lets assume we are using a format that wants the number to be 1.xxxx in binary so we need to move our decimal point

001.11111010000000000000000000000

the decimal place was moved just after the most significant one (so that only one 1 is to the left of the decimal point).

if you remember from math in high school 123 can be written as 1.23 * 10^2 (ten to the power 2). 123 is also 123 * 10^0. Each place that you move the decimal left in a base 10 number increases the power of 10 by one. Each place to the right decreases. No different in binary which is base 2, each place you move the decimal left you increase the power of 2 (binary is base 2) each place right, you decrease the power of 2.

So to be accurate our number is now

1.1111101 * 2^30

Now the details of the floating point format come into play. Because the 1 to the left of the decimal point (Except for the exact number zero and some other exceptions) is assumed to be there, it is sometimes not in the final binary representation for that floating point number, it is a waste of space to put a bit we know is always one when we could instead have one more bit for mantissa. The mantissas are often wysiwyg in the binary, the exponent is the one that leaves you scratching your head, esp with IEEE754. An exponent of 128 for example in the binary format for the number might represent 2 to the power 0 a 129 might be 2 to the power 1 and so on. this may seem strange but it is nothing more than a twos complement field for some of these formats, others have other ways do do things. Single, double, extended have different numbers of bits for the exponent and you need to figure out what you add to the real exponent (30 in this case) to get the bit pattern that goes in the exponent field of the floating point number.

another example

0000000000000000000000000000000000000101

would be

1.01 * 2^2

if you start with a 32 bit integer (not float) and want to end up with a 32 bit floating point number you obviously cannot retain all the detail of that number, since the floating point number needs a sign bit and an exponent as well as the mantissa, the mantissa is less than 32 bits so you throw away the least significant bits

1000000000000000000000000000001  

will get chopped off at some point:

1.000000000000000 * 2^something
Share:
17,257
Supertecnoboff
Author by

Supertecnoboff

I’m Daniel Sadjadian - Car enthusiast, computer programmer &amp; entrepreneur running my own business. I live life to the max and try my best to get the most out of each day. Every moment counts :)

Updated on June 05, 2022

Comments

  • Supertecnoboff
    Supertecnoboff about 2 years

    I have this 32-bit binary number (00111111010000000000000000000000) and I want to know how I can go about converting it to a floating point number.

    Without using programming or a converter, how can I manually convert this 32-bit binary number into a floating point number?

    Thanks,

    Dan