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:

LevelCan restore aboutBits in the format information
L7 %01
M15 %00
Q25 %11
H30 %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.

7
bytes
01,0002,0003,0001510152025303540VersionLMQH

Smallest version for 500 bytes: L: 15, M: 17, Q: 21, H: 24

Version 7: 196 codewords in all
LevelData bytesError correction bytesCan correct
L1564020 (10%)
M1247236 (18%)
Q8810854 (28%)
H6613065 (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 ii and columns jj are numbered from 0 at the top left,  mod \bmod means the remainder after dividing, and ⌊x⌋\lfloor x \rfloor means xx rounded down.

PatternReferenceFlip the module in row i, column j when
000(i+j) mod 2=0(i + j) \bmod 2 = 0
001i mod 2=0i \bmod 2 = 0
010j mod 3=0j \bmod 3 = 0
011(i+j) mod 3=0(i + j) \bmod 3 = 0
100(⌊i/2⌋+⌊j/3⌋) mod 2=0(\lfloor i / 2 \rfloor + \lfloor j / 3 \rfloor) \bmod 2 = 0
101(ij mod 2)+(ij mod 3)=0(ij \bmod 2) + (ij \bmod 3) = 0
110((ij mod 2)+(ij mod 3)) mod 2=0\big((ij \bmod 2) + (ij \bmod 3)\big) \bmod 2 = 0
111(((i+j) mod 2)+(ij mod 3)) mod 2=0\big(((i + j) \bmod 2) + (ij \bmod 3)\big) \bmod 2 = 0
The eight mask patterns on a version 1 code. Dark modules are the ones the mask flips. Grey modules are the fixed parts and the format information, which are never masked.

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 215=32 7682^{15} = 32\,768 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 xx, and the last bit the coefficient of x0=1x^0 = 1:

00101=0x4+0x3+1x2+0x+1=x2+1\begin{aligned} \texttt{00101} &= 0x^4 + 0x^3 + 1x^2 + 0x + 1 \\ &= x^2 + 1 \end{aligned}

The coefficients are bits, so their arithmetic wraps around at 2: 1+1=01 + 1 = 0. 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 −1-1 and 11 are the same bit.

(x4+x2)+(x2+1)=x4+110100⊕00101=10001\begin{gathered} (x^4 + x^2) + (x^2 + 1) = x^4 + 1 \\ \texttt{10100} \oplus \texttt{00101} = \texttt{10001} \end{gathered}

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 x10x^{10}. It divides the result by the generator polynomial

G(x)=x10+x8+x5+x4+x2+x+1G(x) = x^{10} + x^8 + x^5 + x^4 + x^2 + x + 1

(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 G(x)G(x), and taking away is the same as adding, so every valid string divides exactly by G(x)G(x), before the mask described below is applied. The 32 valid strings are exactly the multiples of G(x)G(x) 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 G(x)G(x), and adding two multiples of G(x)G(x) 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.

Error correction level

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:

001010000000000 10100110111 000000011011100

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.

0 flipped

No errors: these bits are a valid codeword (level M, mask 101).

Closest valid codewords
Level · maskBitsDiffers
M · 101sent1000000110011100
L · 0001110111110001007
L · 0111111000100111017
L · 1001100110001011117

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:

All 496 pairs of the 32 valid format information codewords
Bits that differPairs
7240
8240
1516

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 7−3=47 - 3 = 4 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):

G(x)=x12+x11+x10+x9+x8+x5+x2+1\begin{aligned} G(x) = x^{12} &+ x^{11} + x^{10} + x^9 \\ &+ x^8 + x^5 + x^2 + 1 \end{aligned}

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.

7

Encoding

Data bits: 7 in binary is 000111. Append 12 zeros, then divide by the generator 1111100100101, XORing it in under each leading 1:

000111000000000000 1111100100101 000000110010010100

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.

0 flipped

No errors: these bits are a valid codeword (version 7).

Closest valid codewords
VersionBitsDiffers
7sent0001111100100101000
80010000101101111008
90010011010100110018
100010100100110100118

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:

All 561 pairs of the 34 valid version information codewords
Bits that differPairs
8400
12161

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 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.

Level
7

Top left rows 0–8, columns 0–8

01234561413121110987

Top right rows 0–8, columns 34–44

0123456789101112131415161776543210

Bottom left rows 34–44, columns 0–8

03691215147101316258111417891011121314

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.