NewAI Part GeneratorFree to get started
FREE TOOL

Fixed-point calculator (Q format)

DSP code stores fractions as scaled integers, and this fixed-point calculator shows what a decimal number becomes in Qm.n: the raw integer, hex, bit pattern and rounding error. It converts back too, and you can set the word layout, rounding and overflow behavior to match your DSP library or C code.

Convert
Common formats
Type
Notation

Excludes the sign bit: Q15 has m = 0.

One LSB is 2^−n.

On overflow

Q0.15 bit pattern

0x0CCD

raw integer 3277, stores 0.100006103515625

Raw integer
3277
Binary
0.000110011001101
Stored value (exact)
0.100006103515625
Error (stored − input)
0.000006104
Error in LSBs
0.2
Relative error
0.006104 %
Format
Q0.15 (Arm: Q1.15)
Word length
16 bits, int16_t
Minimum
-1
Maximum
0.999969482421875
Resolution (1 LSB)
0.000030517578125 (2^−15)
Product of two, no loss
Q1.30, 32 bits
Sum of two, no loss
Q1.15, 17 bits

Bit layout, most significant bit first

0000110011001101

SignFraction

How do you convert a decimal number to fixed point?

Multiply by 2n and round to an integer, where n is the number of fraction bits. In Q15, 0.1 × 32768 = 3276.8, which rounds to 3277 or 0x0CCD. That code stores 0.100006103515625, an error of 0.2 LSB. Divide the integer by 2n to convert back.

Q format fixed-point formula

A Qm.n number is an ordinary two’s-complement integer that you agree to read as if its binary point sat n bits above the least significant bit. The hardware does integer math; the scale lives in your head and your comments.

raw = round(x × 2ⁿ)     x = raw ÷ 2ⁿ
x
the real value
raw
the stored integer
n
fraction bits
m
integer bits, not counting the sign (TI notation)

The word length, range and resolution follow from m and n:

signed: m + n + 1 bits, −2ᵐ ≤ x ≤ 2ᵐ − 2⁻ⁿ     unsigned: m + n bits, 0 ≤ x ≤ 2ᵐ − 2⁻ⁿ     resolution = 2⁻ⁿ

Vendors disagree on what m counts. TI writes a 16-bit word with one sign bit and 15 fraction bits as Q0.15, leaving the sign out of m. Arm’s CMSIS-DSP writes the same word as 1.15, counting the sign as an integer bit. The Q format calculator accepts either and shows both names.

Multiplying and adding change the format. With no bits dropped, for signed values in TI notation:

Qa.b × Qc.d = Q(a + c + 1).(b + d)     Qa.b + Qc.d = Q(max(a, c) + 1).max(b, d)

So Q15 × Q15 is Q1.30 in 32 bits, Q7.8 × Q7.8 is Q15.16 in 32 bits, and Q15 + Q15 is Q1.15 in 17 bits.

Worked examples

Example 1: 0.1 in Q15

0.1 × 2¹⁵ = 3276.8, which rounds to 3277, or 0x0CCD. That code stores 0.100006103515625, so the error is 0.0000061 (0.2 LSB). A C cast truncates instead and gives 3276 (0x0CCC), an error of -0.8 LSB.

Example 2: reading 0xE400 as Q3.12

The top bit is set, so the 16-bit integer is negative: 0xE400 − 65536 = -7168. Dividing by 2¹² gives -1.75. Q3.12 covers -8 to 7.999755859375 in steps of 0.000244140625.

Example 3: what 1.0 does to Q15

1.0 × 2¹⁵ = 32768, one past the largest code. Saturating gives 0x7FFF, which reads back as 0.999969482421875: wrong by one LSB. Wrapping gives 0x8000, which reads back as -1, the opposite sign. This is why DSP libraries saturate. For comparison, −1.5 in Q7.8 is in range and encodes as 0xFE80.

Range and resolution of common Q formats

Exact limits. Names are in TI notation with the Arm name beside them for signed formats; Q7.24 is TI’s IQ24.

FormatArm nameBitsMinimumMaximumResolution
Q0.7Q1.78-10.99218750.0078125
Q0.15Q1.1516-10.9999694824218750.000030517578125
Q0.31Q1.3132-10.99999999953433871269226074218750.0000000004656612873077392578125
Q3.12Q4.1216-87.9997558593750.000244140625
Q7.8Q8.816-128127.996093750.00390625
Q15.16Q16.1632-3276832767.99998474121093750.0000152587890625
Q7.24Q8.2432-128127.9999999403953552246093750.000000059604644775390625
UQ8.8same160255.996093750.00390625
UQ0.16same1600.99998474121093750.0000152587890625
UQ16.16same32065535.99998474121093750.0000152587890625

Decimal to Q15 and Q31 hex

Values from the decimal to fixed-point converter above, rounded to nearest, with ties away from zero. These are the constants that end up in filter tables.

DecimalQ15 rawQ15 hexQ31 rawQ31 hex
0.99324400x7EB821260088120x7EB851EC
0.9294910x733319327352830x73333333
0.75245760x600016106127360x60000000
0.7071231700x5A8215184856880x5A8240B8
0.5163840x400010737418240x40000000
0.2581920x20005368709120x20000000
0.132770x0CCD2147483650x0CCCCCCD
0.013280x0148214748360x0147AE14
-0.1-32770xF333-2147483650xF3333333
-0.25-81920xE000-5368709120xE0000000
-0.5-163840xC000-10737418240xC0000000
-1-327680x8000-21474836480x80000000

Getting it right in C

  • A float-to-integer cast in C truncates toward zero (C11 6.3.1.4), so (int16_t)(x * 32768.0) can be off by almost a full LSB. Round first, for example with lround().
  • C gives no guaranteed result when a float converted to an integer is out of range, so clamp before you cast.
  • CMSIS-DSP’s arm_float_to_q15 multiplies by 32768 and saturates, and only rounds when built with ARM_MATH_ROUNDING, which adds ±0.5 before the cast (ties away from zero).
  • Right-shifting a negative integer is implementation-defined in C (6.5.7). Most compilers do an arithmetic shift, which floors, so repeated shifting biases negative values downward. Add half an LSB first to round.
  • Leave headroom when you sum. CMSIS-DSP’s Q15 dot product keeps its 2.30 products in a 64-bit 34.30 accumulator for this reason. As a rule, N additions need log₂(N) extra integer bits.
  • A float32 has only 24 bits of significand, so converting through float32 cannot fill the 31 fraction bits of Q31. Use double or integer math for Q31 constants.

This fixed-point converter models words of 1 to 64 bits with m and n at zero or above. Formats whose scale lies outside the word (negative m or n) are not covered: rescale those by hand.

Glossary

Fixed point
A number stored as an integer with an agreed scale, so the binary point never moves.
Qm.n
A format with m integer bits and n fraction bits. Whether m counts the sign bit depends on the vendor.
LSB
Least significant bit. In Qm.n one LSB is worth 2^−n.
Saturation
Clamping an out-of-range result to the largest or smallest code instead of letting it wrap.
Wraparound
Keeping only the low bits of an out-of-range result, which is what plain two’s-complement integer hardware does.
Convergent rounding
Rounding to the nearest code with ties going to the even code, which removes the small average bias of always rounding ties up.

Related guides

Frequently asked questions

What does the Q15 format mean?

Q15 is a 16-bit signed format with 15 fraction bits and no integer bits apart from the sign, so it is Q0.15 in full. It covers -1 to 0.999969482421875 in steps of 2^−15, about 0.0000305.

What is the Q7 format?

Q7 is an 8-bit signed format with 7 fraction bits and no integer bits apart from the sign, so it is Q0.7 in full. It covers -1 to 0.9921875 in steps of 2^−7, which is 0.0078125.

How do I convert a decimal number to Q15?

Multiply by 2^15 (32768) and round to an integer. 0.1 × 32768 = 3276.8, which rounds to 3277, or 0x0CCD. To go back, divide the integer by 32768.

What is the difference between Q0.15, Q1.15 and Q15?

They name the same 16-bit word. TI notation does not count the sign bit in m, so it writes Q0.15 (or Q15 for short). Arm documents such as CMSIS-DSP count the sign bit, so they write 1.15. Always check which notation a datasheet or library uses before trusting the number of integer bits.

Why can Q15 not represent +1.0?

Two’s complement has one more negative code than positive. The largest Q15 code is 0x7FFF, which is 0.999969482421875, while 0x8000 is exactly -1. Converting 1.0 either saturates to 0x7FFF or wraps to 0x8000, which flips the sign.

What format do I get when I multiply two Q15 numbers?

A 32-bit Q1.30 (2.30 in Arm notation). Shift right by 15 to return to Q15 and saturate, because −1 × −1 = +1 is the one product that does not fit back in Q15.

What is the range and resolution of the Qm.n format?

A signed Qm.n covers −2^m to 2^m − 2^−n and an unsigned UQm.n covers 0 to 2^m − 2^−n. The resolution is 2^−n in both cases, and each integer bit you add doubles the range at the cost of one fraction bit in a fixed word.

Should I round or truncate when converting to fixed point?

Round if accuracy matters. Truncation can be off by nearly a full LSB and biases the result toward zero, while rounding keeps the error within half an LSB. For 0.1 in Q15, truncation gives 3276 and rounding gives 3277.

More free PCB tools

See all free electronics tools →

Sources

Design the whole board, not one number

ProtoFlow is free AI PCB design software: describe a circuit and get a schematic with real parts, PCB layout with an autorouter, SPICE simulation and Gerber files. Native KiCad, Altium and EAGLE import and export. Free on macOS, Windows and Linux.

Get for Mac

Free to start. No card required.