Fixed point Q notation is a compact way to represent fractional numbers in binary using a fixed number of integer and fractional bits, written as Qm.n (for example, Q8.8 or Q15.16). It lets you perform arithmetic with fractions entirely using integer hardware, which is faster and cheaper than floating-point units, making it essential for DSP, embedded systems, and graphics. This article explains the format, conversion math, and practical pitfalls so you can choose and apply Q notation correctly.
Formula
The Q format is defined by two integers: m (integer bits, including sign) and n (fractional bits). The real value val is stored as an integer q using:
q = round(val × 2n)
To convert back: val = q / 2n
Where:
- q – the stored integer (unitless counts, often called LSBs).
- val – the real number you want to represent.
- m – total bits used for the integer part including the sign bit (for signed formats).
- n – bits used for the fractional part.
The resolution (smallest representable step) is 2−n. The range for a signed Qm.n number is −2m−1 to 2m−1 − 2−n.
Worked Example
Using the defaults: m = 8, n = 8, val = 12.5.
Step 1: Multiply by 2n = 28 = 256.
12.5 × 256 = 3200
Step 2: Round to nearest integer (already integer): q = 3200.
Step 3: Convert back: 3200 / 256 = 12.5 exactly.
Resolution: 1/256 ≈ 0.00390625.
Range for signed Q8.8: −128 to 127.99609375.
So the calculator outputs q = 3200, res = 12.500000, range = −128 to 127.996094.
Assumptions
- The format is signed (two’s complement). Unsigned Q formats exist but are less common; the range formula changes accordingly.
- Rounding is to nearest integer; some systems truncate instead, which introduces a bias.
- Overflow is not checked – you must ensure the result fits in m + n bits.
Why Use Q Notation Instead of Floating Point?
Floating-point arithmetic requires dedicated hardware (an FPU) or slow software emulation. Many microcontrollers, DSP chips, and FPGAs lack an FPU entirely. Q notation uses the same integer add, subtract, and multiply instructions the CPU already has, so it runs in a fraction of the cycles. It also gives deterministic timing – every operation takes the same number of cycles, which is critical for real-time audio, control loops, and communications. Floating-point results can vary with compiler optimizations or hardware rounding modes; Q arithmetic is bit-exact.
Another advantage is memory. A Q15.16 value fits in a 32-bit integer, while a float also uses 32 bits but offers far less precision in the fractional part for typical signal-processing ranges. For a fixed word size, Q notation lets you choose exactly how much resolution you need for the range of your data.
Choosing m and n: Precision vs. Range
The trade-off is fundamental: for a fixed total bit width (say 16 or 32 bits), increasing n gives finer resolution but shrinks the maximum representable value. Increasing m extends range but coarsens the step size. The rule of thumb:
- Determine the maximum absolute value your signal can reach. Add headroom for intermediate calculations (often 2–4×).
- Set m = bits needed for that max value, plus 1 for the sign.
- Use the remaining bits for n.
- Check resolution: is 2−n small enough for your accuracy needs?
For audio samples at 16 bits, Q1.15 is standard (range −1 to 0.99997). For coordinates in a game world, you might use Q16.16 (range ±32768, resolution 1/65536). For sensor readings with wide dynamic range, Q8.24 on a 32-bit processor gives a huge range (±128) with microscopic resolution (6×10−8).
Arithmetic Rules and Pitfalls
Addition and subtraction are trivial: align the binary points (they are already aligned if both operands share the same m,n) and use the integer operation. The result has the same Q format. The pitfall is overflow – adding two large Q8.8 numbers can exceed 16 bits. You must either widen the result or clamp.
Multiplication is where mistakes happen. Multiplying two Qm.n integers gives a result with 2n fractional bits. For Q8.8 × Q8.8, the product is Q16.16. To keep the same format, you must shift right by n bits after multiplying. Many programmers forget this shift and get wildly wrong values. Example: 12.5 (Q8.8 = 3200) × 2.0 (Q8.8 = 512) = 1,638,400. Dividing by 256 gives 6400, which converts back to 25.0 – correct. Without the shift, you’d read 6400/256 = 25,000, which is wrong.
Division is the opposite: you must shift the numerator left by n bits before dividing, then the quotient has the correct fractional bits. Integer division truncates, so rounding is often added.
Common Mistakes and How to Avoid Them
- Forgetting the sign bit – Q8.8 signed range is −128 to +127.99, not −256 to +255. Unsigned Q8.8 goes 0 to 255.99.
- Mixing formats – adding Q8.8 and Q4.12 requires converting one to the other first (shift by the difference in n).
- Rounding vs. truncation – truncating negative numbers rounds toward zero, not toward negative infinity, causing bias in filters.
- Overflow in intermediate products – a 16×16 multiply produces 32 bits; if you store back to 16 bits without checking, you get silent wraparound.
- Using the wrong constant – 2n is 256 for Q8.8, not 255 or 128. The step size is 1/256.
Fixed Point vs. Floating Point vs. Integer
Plain integers are Qm.0 – no fractional bits. They are fastest but unusable for anything with decimals unless you scale manually. Floating point (IEEE 754) has a dynamic exponent, so it covers enormous ranges with relative precision, but requires an FPU, consumes more power, and has non-deterministic timing on some platforms. Q notation sits between: fixed resolution across the entire range, deterministic, and integer-fast. The cost is that you must manage scaling yourself, and the dynamic range is limited – a Q8.8 cannot represent 0.00001 or 1000 simultaneously.
For control loops (PID), Q15.16 or Q31.32 is common on 32-bit ARM cores. For graphics transforms, Q16.16 is the classic choice. For audio, Q1.15 or Q1.31 matches the DAC/ADC bit depth. The choice always comes back to the signal’s range and required resolution.
Frequently Asked Questions
What does Q15 mean without a second number? It means Q1.15 – one sign bit, zero integer bits, 15 fractional bits. The range is −1 to 0.99997. This shorthand is common in DSP literature.
How do I convert a Q format number to a float in C? Cast the integer to double, then divide by 2n. For Q8.8: (double)q / 256.0. To go the other way: (int)round(val * 256.0). Always use round, not a plain cast, to avoid truncation bias.
What is the difference between Q and UQ? UQ is unsigned – no sign bit, so the range is 0 to 2m − 2−n. UQ8.8 goes 0 to 255.996. Signed Q8.8 goes −128 to 127.996. Use UQ when all values are known non-negative.
Why do I get 0 when I multiply two small Q numbers? Because the product’s fractional bits overflow the integer. For example, 0.1 × 0.1 in Q8.8: 26 × 26 = 676, then shift right 8 = 2 (0.0078), not 0.01. You need more fractional bits (larger n) or you must accumulate products in a wider format.
Can I use Q notation on a modern PC with an FPU? Yes, and it is often still faster for vectorized integer SIMD operations. It is also useful when you need bit-exact reproducibility across different machines, which floating point does not guarantee.
How do I handle saturation instead of wraparound? After each operation, check if the result exceeds the max or min for your Q format and clamp it. Many DSP processors have hardware saturation instructions; on general CPUs you use conditional moves or compare-and-clamp code.
Related
Formula used
in m | Number of integer bits (m) | bits | 8 | 0 | 31 in n | Number of fractional bits (n) | bits | 8 | 0 | 31 in val | Real number to convert | - | 12.5 | 0 | 1000000 out q | Q-format integer value | counts | round(val * 2^n) | 0 out res | Real value represented | - | q / 2^n | 6 out range | Usable range (m bits signed) | - | -2^(m-1) to 2^(m-1) - 2^-n | 6
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation with the constants and conversion factors written on the page, rounded as each result states. See method and constants, the disclaimer or report an error.
More in this section
- QPP Calculator Canada: How It Works & How to Use It
- PPP Calculator World Bank: How It Works & How to Use It2,000.00
- PPP Calculator: India vs USA Cost Comparison1,000.00 USD
- PPP Calculator USD to INR: How It Works2,335 INR
- PPP Calculator India vs UK: Real Cost Comparison103.63 GBP
- PPP Calculator India vs Dubai: Real Cost Comparison8,954.58 AED