Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 67 ยท Data Representation

Fixed-Point Numbers and the Binary Point

Learn how fixed-point numbers store fractions in plain integers: the binary point, scaling, unsigned and signed ranges, and arithmetic, with Python code you can run.

Integers are not enough for everything. Prices have cents, sensors report 21.5 degrees, and audio samples sit between -1 and 1. The next lesson shows floating point, the format that CPUs use for general real numbers. But there is an older and simpler way to store fractions that is still everywhere in embedded chips, digital signal processors, graphics hardware and money handling: fixed point.

The idea is to store an ordinary integer and agree, once and for all, where the binary point sits. The hardware needs no extra support, because it only ever adds and multiplies integers. This lesson explains the format, the range and precision it gives, and how arithmetic works. It builds on Signed Integers and Two's Complement.

Where is the binary point?

In decimal, the digit positions to the right of the point have weights 1/10, 1/100, and so on. In binary the weights are 1/2, 1/4, 1/8, and so on. The bit pattern 101.11 means 4 + 1 + 1/2 + 1/4 = 5.75.

A fixed-point number does not store the point. The point is a convention shared by everyone who uses the number. If we say an 8-bit value has 3 fraction bits (written Q5.3: 5 integer bits, 3 fraction bits), then the stored pattern 00101110 is read as 00101.110, which is 5.75.

Equivalently, the stored value is an integer N and the real value is

Text
value = N / 2^f          (f = number of fraction bits)

So 5.75 is stored as the integer 46, because 46 / 8 = 5.75. Dividing by 2^f is the same as moving the point f places to the left. That is all fixed point is: an integer with a scale factor.

Range and resolution

Two numbers describe a format: how big a value can be (range) and how small a step is (resolution, the gap between neighbours). With f fraction bits the resolution is 1/2^f, and it is the same everywhere in the range. That is the opposite of floating point, where steps grow with the size of the number.

Format (8 bits) Resolution Unsigned range Signed (two's complement) range
Q8.0 (integer) 1 0 to 255 -128 to 127
Q5.3 0.125 0 to 31.875 -16 to 15.875
Q4.4 0.0625 0 to 15.9375 -8 to 7.9375
Q0.8 0.00390625 0 to 0.99609375 -0.5 to 0.49609375

You can only move the point at the cost of something else. More fraction bits give finer steps but a smaller maximum. This trade between range and precision is the central design decision for any fixed-point format.

Python Encode and decode fixed-point values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at 3.3. It cannot be written exactly with a resolution of 0.125, so the nearest representable value (3.25) is stored. Fixed point, like all finite formats, has rounding error, but the maximum error is a predictable half a step (here 0.0625).

Unsigned and signed fixed point

Unsigned fixed point is a plain unsigned integer with a scale. Signed fixed point almost always uses two's complement: the top bit has negative weight -2^(i-1), and the fraction bits keep their positive weights. The pattern 1110.1000 in Q4.4 is -8 + 4 + 2 + 0.5 = -1.5. Negation works exactly as for integers: flip all bits and add one unit in the last place (the pattern 0000.0001).

Because the hardware sees only integers, a normal integer ALU already does everything. Adding fixed-point numbers with the same format is just integer addition. The only care is overflow: the sum of two Q5.3 numbers may need 6 integer bits.

Arithmetic

Addition and subtraction need both operands to have the same number of fraction bits. If they differ, shift the one with fewer fraction bits left to align the points.

Multiplication is where the scale shows up. Multiply two integers a and b that represent a/2^f and b/2^f. The integer product is a x b, which represents (a x b)/2^(2f). It has twice as many fraction bits. To return to the original format, shift the product right by f bits (this rounds or truncates).

Division is the reverse: shift the numerator left by f bits first, then divide the integers, otherwise the fraction is lost.

Python Fixed-point add, multiply and divide in Q8.8

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line shows a classic effect: 0.1 is stored as 26/256 = 0.1015625, so ten of them give 1.015625, not 1. If you need money to be exact, store an integer number of cents (a fixed-point format in base 10, with scale factor 100), not binary fractions.

Saturation and overflow

Ordinary integer arithmetic wraps on overflow. For a volume control, wrapping is terrible: a loud sample would turn into a loud sample of the opposite sign. Signal processors therefore offer saturating arithmetic: if the result is out of range, it is clamped to the largest or smallest value. Many fixed-point DSP chips and SIMD instruction sets include saturating add and multiply for this reason.

Fixed point versus floating point

Fixed point Floating point
Resolution Constant everywhere Relative to the magnitude
Range Small, set by the format Enormous
Hardware Integer ALU only Dedicated FPU
Speed and energy Fast, low power Slower, more power
Predictability Very predictable errors Rounding depends on magnitude
Typical use Embedded, DSP, audio, money, games on old hardware Science, graphics, general programs

Choose fixed point when the range of values is known in advance, when you have no floating-point unit, or when you need identical results on every machine. Choose floating point when the range is huge or unknown.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. An 8-bit unsigned fixed-point format has 3 fraction bits. What does the pattern 00101110 represent?

    The integer value is 46, and dividing by 2^3 = 8 gives 5.75. Equivalently, the pattern is read as 00101.110 = 4 + 1 + 0.5 + 0.25.
  2. 2. What is the resolution of a fixed-point format with 4 fraction bits?

    The smallest step is 1/2^f = 1/16 = 0.0625, and it is the same across the whole range.
  3. 3. After multiplying two Q8.8 numbers as integers, what must you do to get a Q8.8 result?

    The integer product has 16 fraction bits (8 + 8). Shifting right by 8 returns it to 8 fraction bits.
  4. 4. Which statement about fixed point versus floating point is true?

    Fixed point has uniform step size but a small range. Floating point trades uniform resolution for a huge range, and fixed point works on an integer ALU.
  5. 5. Why do audio chips often use saturating arithmetic?

    Saturation clamps an overflowing result to the maximum or minimum, which is a mild distortion, instead of wrapping to the opposite extreme.

Summary

  • A fixed-point number is an integer with an agreed scale: value = N / 2^f, so the binary point is a convention, not stored.
  • The step (resolution) is 1/2^f everywhere; more fraction bits mean finer steps but a smaller range.
  • Signed fixed point uses two's complement, so a plain integer ALU adds and subtracts it.
  • A product has twice the fraction bits and must be shifted back; division needs the numerator pre-shifted.
  • Rounding error is bounded by half a step; money is best stored as integer cents.
  • Saturating arithmetic clamps instead of wrapping and is common in signal processing.

Next: Floating-Point Numbers and IEEE 754.