This guide explains how to manually calculate the IEEE 754 single-precision (32-bit) floating-point representation of a decimal number, which is how most computers store non-integer values. You'll learn to convert a decimal like 12.375 into its binary form, normalize it, and encode it as sign, exponent, and mantissa bits. By the end, you'll be able to do this step-by-step for any real number, and understand what the bits mean.
What You'll Need
- A basic understanding of binary (base-2) numbers.
- Familiarity with exponents and scientific notation.
- Paper and pencil, or a text editor to work through the steps.
- Optional: A calculator that can convert between decimal and binary.
Step-by-Step
We'll use the example number 12.375 throughout. The goal is to produce the 32-bit representation: 0 10000010 10001100000000000000000.
- Determine the sign bit. If the number is positive, the sign bit is
0. If negative, it's1. For 12.375, the sign bit is0. - Convert the integer part to binary. Divide the integer part by 2 repeatedly, recording remainders. For 12:
12 ÷ 2 = 6 remainder 0
6 ÷ 2 = 3 remainder 0
3 ÷ 2 = 1 remainder 1
1 ÷ 2 = 0 remainder 1
Read remainders bottom-up:1100. - Convert the fractional part to binary. Multiply the fraction by 2 repeatedly. For 0.375:
0.375 × 2 = 0.75 → integer part 0
0.75 × 2 = 1.5 → integer part 1
0.5 × 2 = 1.0 → integer part 1
Read integer parts top-down:011. So 0.375 in binary is0.011. - Combine the parts. 12.375 in binary is
1100.011. - Normalize the binary number. Write it in scientific notation:
1.100011 × 2^3. The mantissa is100011(the digits after the decimal point), and the exponent is3. - Calculate the biased exponent. For single precision, the bias is 127. Add the bias to the exponent: 3 + 127 = 130. Convert 130 to binary:
10000010. - Assemble the 32 bits. The layout is: 1 sign bit, 8 exponent bits, 23 mantissa bits.
Sign:0
Exponent:10000010
Mantissa:10001100000000000000000(pad with zeros to 23 bits).
Final representation:0 10000010 10001100000000000000000. - Verify (optional). Convert back: sign = +, exponent = 130 - 127 = 3, mantissa = 1.100011 (binary). Value = 1.100011 × 2^3 = 1100.011 (binary) = 12.375. Correct.
Common Mistakes
- Forgetting the implicit leading 1. In normalized form, the mantissa always starts with 1, so you don't store it. Forgetting to add it back when converting back is a frequent error.
- Using the wrong bias. Single precision uses 127, double precision uses 1023. Mixing them up gives wildly wrong results.
- Not padding the mantissa to 23 bits. The mantissa field must be exactly 23 bits. If your binary fraction has fewer digits, pad with zeros on the right; if more, you need to round.
- Incorrect binary conversion of the fraction. Multiplying by 2 and reading the integer part is the correct method; some mistakenly divide or read remainders.
- Forgetting to handle negative exponents. For numbers less than 1 (e.g., 0.25), the exponent will be negative. The biased exponent is still calculated by adding 127, so 0.25 gives exponent -2, biased = 125.
Tips
- Use a table for repeated conversions. Write down each step for the integer and fractional parts to avoid errors.
- Check your exponent range. For single precision, the unbiased exponent must be between -126 and 127. If it's outside, you're dealing with special cases (denormals, infinity, or zero).
- Practice with simple numbers first. Numbers like 0.5 (binary 0.1), 1.0 (binary 1.0), and 2.0 (binary 10.0) are good starting points.
- Remember that many decimals are not exact. For example, 0.1 in binary is a repeating fraction. You'll need to round to fit in 23 bits, which is why floating-point arithmetic can have small errors.
Troubleshooting
If your final representation doesn't match a known converter, consider:
- Double-check your binary conversion. Recompute the integer and fractional parts separately. A single mistake in the binary digits will change the mantissa.
- Verify the normalization. Ensure you've moved the decimal point so that there is exactly one non-zero digit (1) to its left. The exponent is the number of positions you moved.
- Check the exponent bias. Recalculate the biased exponent: unbiased exponent + 127. If you get a negative biased exponent, you've made an arithmetic error.
- Look for rounding issues. If your binary fraction has more than 23 bits after the leading 1, you must round. The default rounding mode is round-to-nearest, ties-to-even. For example, if the 24th bit is 1, you may need to round up the mantissa.
Frequently Asked Questions
What is the difference between single and double precision?
Single precision uses 32 bits (1 sign, 8 exponent, 23 mantissa) with a bias of 127. Double precision uses 64 bits (1 sign, 11 exponent, 52 mantissa) with a bias of 1023. Double precision has a larger range and more precision.
Why is there an implicit leading 1 in the mantissa?
In normalized form, the mantissa always starts with 1 (e.g., 1.xxx). Since it's always 1, it's not stored, saving one bit of precision. This is why the stored mantissa is called the "fraction" or "significand" without the leading 1.
How do I represent zero in floating point?
Zero is represented by all exponent bits and mantissa bits being 0. The sign bit can be 0 (+0) or 1 (-0). These are distinct but compare equal.
What are NaN and Infinity in floating point?
Infinity is represented by all exponent bits set to 1 and mantissa bits all 0. NaN (Not a Number) has all exponent bits 1 and a non-zero mantissa. These are used for overflow results or invalid operations like 0/0.
Why is 0.1 + 0.2 not exactly 0.3?
Because 0.1 and 0.2 cannot be represented exactly in binary floating-point. They are stored as approximations, and the addition of two approximations yields a result that is slightly off from the true decimal sum. This is a fundamental limitation of binary floating-point arithmetic.
How do I convert a negative number?
The sign bit is set to 1, and the rest of the process is identical. For example, -12.375 has the same exponent and mantissa as +12.375, but the sign bit is 1.