Chapter 2
Format and version information
On this page
Before a scanner can read the data in a QR code, it needs to know two settings: the error correction level and the mask pattern. They are stored in the format information, the two 15-bit strips beside the finder patterns. Codes of version 7 and up also store their version number, in the version information blocks.
This chapter explains what the two settings mean, and then how those few bits are protected against damage. That protection uses the idea behind all of a QR code’s error correction: add extra bits, chosen so that the valid combinations are all very different from each other.
Error correction levelsLink to this section
Every QR code stores error correction bytes alongside its data, so that it can still be read when part of it is dirty, damaged or covered. Whoever makes the code chooses how much, from four levels:
| Level | Can restore about | Bits in the format information |
|---|---|---|
| L | 7 % | 01 |
| M | 15 % | 00 |
| Q | 25 % | 11 |
| H | 30 % | 10 |
The percentages are shares of the code’s bytes (ISO/IEC 18004, Table 8). The bits are how the level is written in the format information (Table 12); note that they aren’t in alphabetical order.
The size of a code is fixed by its version, so more error correction means less room for data. Chapter 1 showed that the data area of each version holds a fixed number of 8-bit codewords, or bytes: 26 for version 1, 196 for version 7. At each level, a set number of those bytes are error correction, and the rest carry the data. Repairing a damaged byte takes two error correction bytes, one to find where it is and one to work out its value, so a code can correct about half as many bytes as it has error correction bytes. Chapter 5 shows how.
The chart below shows how many bytes of data each version holds at each level. Pick a version to see its numbers, or enter an amount of data to find the smallest version that holds it: the rings mark that version for each level.
Smallest version for 500 bytes: L: 15, M: 17, Q: 21, H: 24
| Level | Data bytes | Error correction bytes | Can correct |
|---|---|---|---|
| L | 156 | 40 | 20 (10%) |
| M | 124 | 72 | 36 (18%) |
| Q | 88 | 108 | 54 (28%) |
| H | 66 | 130 | 65 (33%) |
A byte isn’t always one character. Digits and capital letters can be packed more tightly, so a code holds more of them; chapter 3 shows how, and how many characters fit. Larger codes also split their bytes into several blocks, each with its own error correction; chapter 5 explains why.
In the smallest codes, a few error correction bytes are kept back to detect errors without correcting them (7.5.1). If a small code is too badly damaged, a scanner then rejects it rather than reading the wrong data. That’s why version 1-L, with 7 error correction bytes, can only correct 2.
Mask patternsLink to this section
Before the data is placed in the code, it is combined with one of eight mask patterns: every module where the pattern is dark is flipped, from light to dark or dark to light. This breaks up large areas of one colour, and accidental copies of the finder pattern’s dark-light-dark-dark-dark-light-dark sequence, either of which could confuse a scanner (7.8.1). Whoever makes the code tries all eight and keeps the one that scores best; chapter 6 explains the scoring.
To read the data, a scanner has to undo the mask, so the format information records which one was used, as a 3-bit reference. Here are the eight, with the conditions from the standard (Table 10). Rows and columns are numbered from 0 at the top left, means the remainder after dividing, and means rounded down.
| Pattern | Reference | Flip the module in row i, column j when |
|---|---|---|
000 | ||
001 | ||
010 | ||
011 | ||
100 | ||
101 | ||
110 | ||
111 |
Encoding the format informationLink to this section
The format information carries five bits: two for the error correction level, then three for the mask pattern. Five bits are all a scanner needs, but if even one of them is misread, it will undo the wrong mask or expect the wrong amount of error correction, and read nonsense. So the standard adds 10 error correction bits, for 15 in all.
There are possible 15-bit strings, but only 32 of them are valid, one for each level and mask. The error correction bits are chosen so that any two valid strings differ in at least 7 bits. A few misread bits can’t turn one valid string into another, so the scanner can work out which one was meant.
Bits as polynomialsLink to this section
The error correction bits are calculated by dividing polynomials. A string of bits can be read as a polynomial, with each bit the coefficient of a power of , and the last bit the coefficient of :
The coefficients are bits, so their arithmetic wraps around at 2: . Adding two polynomials then adds each pair of bits on its own, with nothing carried, which is exactly the XOR (⊕) of the two bit strings. Subtracting is the same as adding, since and are the same bit.
Long division works as it does with numbers, but is simpler. Each step lines the divisor up under the leading 1 of what is left, and subtracts it, which here is XOR. When fewer bits are left than the divisor has, they are the remainder.
The (15, 5) BCH codeLink to this section
To encode the five data bits, the standard appends 10 zeros, which multiplies the polynomial by . It divides the result by the generator polynomial
(10100110111 in bits) and puts the 10-bit remainder after the data bits as the error correction bits (Annex C). Taking away the remainder
leaves something that divides exactly by , and taking away is the same as adding, so every valid string divides
exactly by , before the mask described below is applied. The 32 valid strings are exactly the multiples of
that fit in 15 bits. This is called a
(15, 5) BCH code: 15 bits, 5 of them data.
The format maskLink to this section
Last, the 15 bits are XORed with the fixed pattern 101010000010010. Without it, level M (00) with mask 000 would
give all-zero data and an all-zero remainder, and the standard requires that the format information is never all zeros
(C.2). XORing the same pattern into every valid string doesn’t change how many bits any two of them differ in.
There is a matching case at the other extreme. Level Q (11) with mask 111 gives the data bits 11111, and their
error correction bits come out as 1111111111, so without the mask all 15 bits would be 1s. That is no coincidence:
flipping every bit of a valid string always gives another valid string. Valid strings are the multiples of , and
adding two multiples of gives another one. The all-1s string is itself valid, and flipping every bit is the same
as adding (XORing) the all-1s string, so the flipped string is valid too. Every valid string has its flipped twin, and
M/000 and Q/111 are the twins of each other. The standard only mentions all zeros, but the mask breaks up both: after
it, no stored string has more than 6 identical bits in a row.
Try it below: choose a level and mask to follow the division step by step, then flip some bits to see how a scanner
decodes them. The default, level M with mask 101, is the standard’s own example, which needs only one division step.
Encoding
Data bits: level M is 00, mask 101, giving 00101. Append 10 zeros, then divide by the generator 10100110111, XORing it in under
each leading 1:
0 zeros left of the leading 1, with nothing left to divide · 1 the generator · 1 the remainder · the dashed line marks where the 10 appended zeros start
The last 10 bits are the remainder: the error correction bits. Put them after the data bits: 001010011011100. Finally, XOR with 101010000010010 to get the bits stored in the code: 100000011001110.
Decoding
Select bits to flip them, as if the code were damaged. The decoder compares what it reads with every valid codeword, 32 in all, and takes the closest if it is no more than 3 bits away.
No errors: these bits are a valid codeword (level M, mask 101).
| Level · mask | Bits | Differs |
|---|---|---|
| M · 101sent | 100000011001110 | 0 |
| L · 000 | 111011111000100 | 7 |
| L · 011 | 111100010011101 | 7 |
| L · 100 | 110011000101111 | 7 |
DecodingLink to this section
A scanner reads 15 bits, some of which may be wrong. It compares them with each of the 32 valid strings, and takes the closest, the one that differs in the fewest bits, as long as it differs in no more than 3 (C.3).
Why 3? Every pair of valid strings differs in at least 7 bits, as this table of every pair shows:
| Bits that differ | Pairs |
|---|---|
| 7 | 240 |
| 8 | 240 |
| 15 | 16 |
The 16 pairs that differ in all 15 bits are the flipped twins described above.
If at most 3 bits were misread, the string that was sent is at most 3 bits away from what was read, and every other valid string is at least bits away. The closest is then always the right one. With 4 or more errors, what was read may land closer to a different valid string, and the scanner picks the wrong one. Flip 4 bits in the widget to see this happen.
The standard states the distance of 7 without showing why (C.3); comparing every pair, as the table does, confirms it.
Why this generator? BCH codes are designed so their codewords are far apart by choosing the generator from arithmetic in a finite field, here one with 16 elements. That theory goes beyond this explainer, and isn’t needed to use the code: the table above checks the distances directly.
Two copiesLink to this section
The format information is stored twice, as chapter 1 showed, because without it nothing else can be read (7.9.1). The standard’s reference decoder reads the copy around the top-left finder pattern first. If that copy has more than 3 errors, it tries the other one, split between the other two finder patterns (section 12).
Version informationLink to this section
From version 7 up, the version number is stored too, in 18 bits: the 6-bit version number, then 12 error correction bits. They are calculated the same way, by appending 12 zeros and dividing, with a different generator (Annex D):
which is 1111100100101 in bits. The standard calls this an (18, 6) Golay code. There is no mask this time: the version is at least 7, so the data bits
are never all zero, and neither is the version information (7.10). For version 7, the result is the
000111 110010010100 from chapter 1.
Encoding
Data bits: 7 in binary is 000111. Append 12 zeros, then divide by the generator 1111100100101, XORing it in under
each leading 1:
0 zeros left of the leading 1, with nothing left to divide · 1 the generator · 1 the remainder · the dashed line marks where the 12 appended zeros start
The last 12 bits are the remainder: the error correction bits. Put them after the data bits: 000111110010010100.
Decoding
Select bits to flip them, as if the code were damaged. The decoder compares what it reads with every valid codeword, 34 in all, and takes the closest if it is no more than 3 bits away.
No errors: these bits are a valid codeword (version 7).
| Version | Bits | Differs |
|---|---|---|
| 7sent | 000111110010010100 | 0 |
| 8 | 001000010110111100 | 8 |
| 9 | 001001101010011001 | 8 |
| 10 | 001010010011010011 | 8 |
Decoding works the same way, comparing what was read with the 34 valid strings, for versions 7 to 40. Any two of them differ in at least 8 bits:
| Bits that differ | Pairs |
|---|---|
| 8 | 400 |
| 12 | 161 |
That still means up to 3 errors can be corrected (D.3). With 4, what was read could be exactly as far from two valid strings, and the scanner couldn’t tell which was meant. The version information is stored twice too: the reference decoder reads the block beside the top-right finder pattern first, and falls back to the one beside the bottom-left finder pattern.
Where the bits goLink to this section
Chapter 1 showed which modules hold the format and version information. Within them, every bit has a module of its own in each copy. The standard numbers the bits from 0, the last bit of the string, up to 14 for the format information and 17 for the version information, and stores a 1 as a dark module (6.1).
The format information’s two copies are laid out differently (Figure 25):
- The first copy wraps around the top-left finder pattern. Bits 0 to 7 run down column 8 from the top edge to row 8, stepping over the timing pattern in row 6. Bits 8 to 14 then run left along row 8 to the left edge, stepping over the timing pattern in column 6.
- The second copy is split in two. Bits 0 to 7 run left along row 8 from the right edge, below the top-right finder pattern. Bits 8 to 14 run down column 8 from just below the dark module to the bottom edge, beside the bottom-left finder pattern.
The two copies never share a corner, so damage to any one corner leaves at least one of them whole.
The version information’s two blocks hold their bits in mirror image (Figure 28). In the block left of the top-right finder pattern, 6 modules tall and 3 wide, the bits fill row by row: 0, 1 and 2 along the top row, then 3, 4 and 5, down to 15, 16 and 17 along the bottom. The block above the bottom-left finder pattern is the same block reflected across the diagonal, so its bits fill column by column instead.
None of these modules are touched by the data mask (7.8.2). The format information has its own fixed mask, applied before its bits are placed, and the version information has none.
Choose a level, mask and version, then point at a bit, in a corner or in the strings, to see where both of its copies go.
Top left rows 0–8, columns 0–8
Top right rows 0–8, columns 34–44
Bottom left rows 34–44, columns 0–8
Format information, level M, mask 101
Version information, version 7
Point at a bit, in a corner or in the strings below, to see where both of its copies go.