How to convert a single-precision binary float to decimal

A single-precision binary float is represented using 32-bits. These bits are divided into three sections to represent the sign, exponent, and fractional parts of a decimal number (according to the IEEE 754 standard).

Bit numbering convention
Bit numbering convention

The fractional part is first converted into a decimal by summing all the 2n2^{-n}s (where nn is the position of a bit in the fractional part that has a 11). For example, if the 23-bit fractional part has a 11 at bit number 2 and 3:

There is a 1 at bit number 2 and 3
There is a 1 at bit number 2 and 3

Its decimal equivalent would be:

22+23=1/4+1/8=0.3752^{-2} + 2^{-3} = 1/4 + 1/8 = 0.375

Formula

Applying the following formula will convert the whole 32-bit binary number into a decimal,

(1)s×(1+(-1)^s\times(1+ Fraction)×2exp127)\times2^{exp-127}

,where ss is the sign-bit, Fraction is the value that has been computed above as an example, and expexp is the numerical value, in base 10, of the 8-bits reserved for the exponent.

Example

svg viewer

s=1s=1

Fraction =21=0.5=2^{-1}=0.5

exp=20+21+27=1+2+128=131exp=2^0+2^1+2^7=1+2+128=131

Substitute the values in the formula to obtain the final answer:

Answer =(1)1×(1+0.5)×2131127=1.5×24=24.0=(-1)^1\times(1+0.5)\times2^{131-127}=-1.5\times2^4=-24.0

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved