Convert binary floating-point real numbers into denary and vice versa

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science 9618 – Floating‑point Numbers

13.3 Floating‑point Numbers – Representation and Manipulation

Learning Objective

Convert binary floating‑point real numbers into denary (decimal) and convert denary real numbers into binary floating‑point form.

Why Floating‑point?

Integer representations cannot store fractions or very large/small numbers efficiently. Floating‑point notation provides a way to represent a wide range of real numbers using a fixed number of bits.

IEEE 754 Single‑Precision Format (32 bits)

The most common format examined at A‑Level is the 32‑bit single‑precision representation, which consists of three fields:

FieldBitsPurpose
Sign (s)10 for positive, 1 for negative
Exponent (e)8Stores the exponent with a bias of 127
Mantissa (fraction) (f)23Stores the fractional part of the significand (the leading 1 is implicit)

The value represented is given by the formula

\$V = (-1)^{s}\times 1.f \times 2^{\,e-127}\$

where \$1.f\$ denotes the binary number formed by an implicit leading 1 followed by the bits of the mantissa.

Step‑by‑Step Conversion: Binary → Decimal

  1. Identify the three fields (sign, exponent, mantissa) from the 32‑bit pattern.
  2. Convert the exponent field from binary to decimal, then subtract the bias (127) to obtain the actual exponent \$E\$.
  3. Form the binary significand by prefixing the mantissa with an implicit leading 1: \$1.f\$.
  4. Convert \$1.f\$ to a decimal fraction.
  5. Apply the formula \$V = (-1)^{s}\times (1.f) \times 2^{E}\$.

Example 1 – Binary to Decimal

Given the 32‑bit pattern:

0 10000001 10100000000000000000000

Perform the conversion:

  • Sign bit \$s\$: 0 → positive.
  • Exponent bits \$e\$: 10000001₂ = 129₁₀.
    Actual exponent \$E = 129 - 127 = 2\$.
  • Mantissa \$f\$: 101000… (23 bits).
    Implicit leading 1 gives \$1.f = 1.101_{2}\$.
  • Convert \$1.101_{2}\$ to decimal: \$1 + \frac{1}{2} + \frac{0}{4} + \frac{1}{8}=1.625\$.
  • Apply the formula: \$V = (+1)\times 1.625 \times 2^{2}=1.625 \times 4 = 6.5\$.

Thus the binary pattern represents the decimal number \$6.5\$.

Step‑by‑Step Conversion: Decimal → Binary (IEEE 754 Single‑precision)

  1. Determine the sign bit \$s\$ (0 for positive, 1 for negative).
  2. Express the absolute value in binary scientific notation: \$N = 1.f \times 2^{E}\$ where \$1 \le 1.f < 2\$.
  3. Calculate the biased exponent: \$e = E + 127\$ and write it as an 8‑bit binary number.
  4. Take the fractional part \$f\$ (the bits after the binary point) and fill the mantissa field with the first 23 bits; pad with zeros if fewer than 23 bits.
  5. Combine the three fields: s | e | f.

Example 2 – Decimal to Binary

Convert \$-0.15625\$ to IEEE 754 single‑precision.

  • Sign bit \$s\$: 1 (negative).
  • Convert \$0.15625\$ to binary:

    • 0.15625 × 2 = 0.3125 → 0
    • 0.3125 × 2 = 0.625 → 0
    • 0.625 × 2 = 1.25 → 1 (subtract 1 → 0.25)
    • 0.25 × 2 = 0.5 → 0
    • 0.5 × 2 = 1.0 → 1 (terminates)

    Result: \$0.00101_{2}\$.

  • Normalise: \$0.00101{2}=1.01{2}\times 2^{-3}\$, so \$E = -3\$.
  • Biased exponent: \$e = -3 + 127 = 124 = 01111100_{2}\$.
  • Fractional part after the leading 1 is \$01\$, followed by zeros to fill 23 bits:

    \$f = 01000000000000000000000\$.

  • Combine:

    1 01111100 01000000000000000000000

The 32‑bit pattern \$1\;01111100\;01000000000000000000000\$ represents \$-0.15625\$.

Common Pitfalls

  • For numbers less than 1, the exponent will be negative after bias removal.
  • The leading 1 in the mantissa is implicit; do not store it.
  • Rounding errors can occur when the binary fraction requires more than 23 bits; the excess bits are rounded (nearest even).
  • Special patterns:

    • All exponent bits = 1 and mantissa = 0 → \$\pm\infty\$.
    • All exponent bits = 1 and mantissa ≠ 0 → NaN (Not a Number).
    • All exponent bits = 0 → denormalised numbers (effective exponent \$E = -126\$, no implicit leading 1).

Practice Questions

  1. Convert the binary pattern 0 10000010 01000000000000000000000 to decimal.
  2. Express the decimal number \$12.75\$ as a 32‑bit IEEE 754 binary pattern.
  3. What decimal value does the pattern 1 11111111 00000000000000000000000 represent?

Suggested Diagram

Suggested diagram: Layout of the 32‑bit IEEE 754 single‑precision word showing sign, exponent, and mantissa fields.