CE2704 · Digital Logic Design
Theme 1 · Numbers & codes

Beyond integers: fixed & floating-point

Integers can't hold 6.625 or 10²⁴. Here are the two ways a machine represents fractions and huge numbers — and one human-friendly code (BCD) along the way.

Built from first principles. Secondary topic — good general literacy; the MPU6050 itself reports integers, not floats.

Before you start

What you need first

  • Base conversions — binary place value, including writing a number in the form 1.xxx × 2ⁿ.

What you'll be able to do

  • Encode a decimal digit in BCD.
  • Read a fixed-point (Q-format) value.
  • Lay out a number in IEEE-754 floating-point, and name its pitfalls.

A quick aside

Binary-Coded Decimal (BCD)

Sometimes we want to keep each decimal digit separate — for 7-segment displays, money, or a real-time-clock chip. BCD encodes each decimal digit in its own 4-bit nibble:

Decimal947
BCD100101000111
The cost: the patterns 10101111 are invalid in BCD, so it wastes 6 of every 16 nibble values. Use BCD at the human interface (displays, clocks, money); use plain binary everywhere else.

Fixed-point: agree where the point sits

The simplest way to hold a fraction: fix an implied binary point at an agreed place. Qm.n means \(m\) integer bits, then the point, then \(n\) fraction bits. The fraction columns are negative powers of two:

bitb3b2b1b0b−1b−2b−3b−4
weight84210.50.250.1250.0625
📐 Worked example

Read 0110.1010 as Q4.4

$$ \underbrace{0110}_{4+2\,=\,6} \;.\; \underbrace{1010}_{0.5+0.125\,=\,0.625} $$
$$ = 6 + 0.625 = 6.625 $$
Why embedded loves fixed-point: it reuses the fast integer adder. A motor controller at 10 kHz can't afford floating-point — fixed-point gets it done with plain add/multiply. Q4.4 spans 0 to 15.9375 in steps of \(2^{-4}=0.0625\).

✏️ Try it yourself

In Q4.4 fixed-point, what decimal value does 00010110 represent?

Split Q4.4: integer 0001 = 1; fraction 0110 = 0.25 + 0.125. Answer: \(1 + 0.375 = 1.375\).

Floating-point: scientific notation, in binary

We want one format that holds both the mass of an electron (≈\(10^{-30}\) kg) and the mass of the Earth (≈\(10^{24}\) kg). The trick is the one scientists already use — scientific notation — done in base 2:

$$ \underbrace{1.0110\ldots_2}_{\text{mantissa}} \times 2^{\,\underbrace{n}_{\text{exponent}}} $$
Why "floating"? The binary point isn't fixed — the exponent tells the hardware where to put it, so it "floats" with the number's size.

IEEE 754 — three fields, three jobs

A 32-bit float is just three fields stored side by side:

S 1 bit Exponent 8 bits Mantissa 23 bits
Sign + exponent + mantissa = 32 bits (a double is 1 + 11 + 52 = 64).
FieldJob
Sign (S)0 = positive, 1 = negative
ExponentHow big/small — the power of 2, stored biased by +127
MantissaThe digits after the implied leading "1."
📐 Worked example

Store 5.0 in 32-bit IEEE 754

1To binary, then normalise to 1.xxx × 2ⁿ:
$$ 5 = 101_2 = 1.01_2 \times 2^{2} $$
2Sign: positive → S = 0. Exponent: bias it by +127:
$$ 2 + 127 = 129 = 10000001_2 $$
3Mantissa: drop the implied "1.", keep 01, pad to 23 bits:
$$ 01000000000000000000000 $$
4Glue the three fields (1 + 8 + 23):
$$ \mathtt{0}\;|\;\mathtt{10000001}\;|\;\mathtt{0100\ldots0} = \mathtt{0x40A00000} $$
Why bias the exponent by 127? Real exponents can be negative (e.g. \(2^{-2}\)). Adding 127 keeps the stored 8-bit field always 0–255, so hardware can compare two floats almost like plain integers.

Three things to watch out for

  • Not every decimal is exact in binary float — 0.1 + 0.2 ≠ 0.3. Equality tests like x == 0.3 often fail.
  • Float math is slower than integer math — often 5–20× on a CPU with an FPU.
  • Many small microcontrollers have no hardware FPU, so floats are emulated in software — sometimes 100× slower than integers.
Rule of thumb: use integers or fixed-point inside an embedded system; reach for floats only when you truly need huge dynamic range (physics, signal processing, graphics).

✏️ Try it yourself

(a) Encode decimal 47 in BCD. (b) Why does 0.1 + 0.2 == 0.3 return false in most languages?

(a) 4 → 0100, 7 → 0111, so 47 = 0100 0111. (b) 0.1 and 0.2 have no exact binary representation; the stored approximations sum to ≈ 0.30000000000000004, not exactly 0.3.

Recap — the whole topic on one screen

FormatIdeaUse it for
BCDEach decimal digit in its own nibbleDisplays, clocks, money
Fixed-point (Qm.n)Implied binary point; integer hardwareFast embedded fractions
Floating-point (IEEE 754)Sign + biased exponent + mantissaHuge dynamic range

Next topic

Register maps: reading a byte

You can now represent any number a chip might hold. Next we read the chip's "control panel" — how the bits in one byte map to real settings. That's the bridge to microcontrollers.

→ Register maps: reading a byte