Printed a code and need to change the link? QR Gen makes codes you can edit later.Edit codes after printing with QR Gen.See QR Gen

Open on your phone

How QR codes work, explained with a real code

See how QR codes work by dissecting a real one: finder squares, timing lines, format bits, masking and Reed-Solomon repair, with measured sizes.

How QR codes work comes down to a short sequence. A phone finds three large squares to learn where the code is and which way it faces. It reads a small strip of settings next to those squares, undoes a scrambling pattern, and unpacks the data bytes, using spare bytes to repair any damage. We took apart a real code, the web address https://example.com/menu, so every number below is measured rather than quoted from a diagram.

If you are here to make a code, the main guide is how to make a QR code. This page is for the moment after, when you wonder why the thing looks the way it does.

The code on the table

We ran the address through the same size rules our generator uses. At error correction level M it is version 2, a grid of 25 by 25 modules, 625 squares in all. Here it is, drawn out with # for dark and . for light (the quiet zone, the blank border, is left off):

#######..#...#..#.#######
#.....#..#.#.####.#.....#
#.###.#.##.#..#...#.###.#
#.###.#.#.#.####..#.###.#
#.###.#.#.#.##..#.#.###.#
#.....#.###.#.##..#.....#
#######.#.#.#.#.#.#######
........##.##.#.#........
#.#####...##.#....#####..
....#..#.#.###...#.#...#.
#..#..#......####..#.#.##
##.#.#..##.##.###.##....#
#.#.#.#.####.###.##.#.###
#...#...##..##..#..#.#.#.
#.##.###..#...##..####.##
#.##...##...#.#######...#
#.#.####..#.###.#####.#..
........###....##...##...
#######..#.##.#.#.#.#.###
#.....#.#.##..#.#...##..#
#.###.#.#############.#..
#.###.#.##..##.#.##.#####
#.###.#.#....###.....##.#
#.....#..#......##.###..#
#######.#....##..########

The grid comes from the same library our size script uses, so a different generator may choose a different mask and draw a different-looking square for the same address. The structure is identical. Rows and columns below are counted from 0, starting at the top left.

Three squares and a missing fourth

The big squares in three corners are the finder patterns. Thonky’s tutorial describes each as a 7 by 7 dark square, with a 5 by 5 light ring inside it and a 3 by 3 dark centre. They sit at the top left, top right and bottom left, and nowhere else.

Take a line straight through the middle of one. It crosses 1 dark module, 1 light, 3 dark, 1 light, 1 dark. That 1:1:3:1:1 rhythm is easy to detect, and it appears the same whether the line runs along a row, a column or a diagonal, which is how a phone spots a code lying at any angle. Denso Wave lists “Readable from any direction in 360°” as a feature of the code.

Three corners rather than four is deliberate. With squares on three corners, the empty fourth corner tells the reader which way is up, without needing a separate marker. A one-module light border, the separator, surrounds each finder on its inner sides so the squares do not blur into the data.

Timing lines and the alignment square

Look at row 6 in the picture: #######.#.#.#.#.#.#######. Between the two top finders the modules alternate dark, light, dark, light. Column 6 does the same going down. These are the timing patterns, and they work like the tick marks on a ruler. If the photo is slightly bent, the reader counts ticks to know where module 10 or module 20 should be.

The same source says timing patterns always start and end with a dark module, which you can see at both ends of row 6.

Version 2 and larger codes also carry alignment patterns, a 5 by 5 square with a light ring and a dark centre. Ours sits at rows 16 to 20, columns 16 to 20, centred on row 18, column 18, in the bottom right where there is no finder. A curled flyer or a curved bottle bends the middle of the picture most, and this square gives the reader a fresh anchor point to re-measure from. Version 1, at 21 by 21, is small enough to need none. Bigger versions get more of them.

Finally there is a single dark module that is always present, just above the bottom left finder, at row 17, column 8 in version 2. It never carries data.

The format strip: settings in 15 bits

Every code spends 15 modules, twice, on a strip of settings. The format information is 5 data bits and 10 error-correction bits. The 5 data bits are 2 for the error correction level (the numbering is not in order: L is 01, M is 00, Q is 11, H is 10) and 3 for the mask pattern. The 15 bits are then combined with a fixed bit pattern before they are placed.

Ours is level M, which is 00, with mask pattern 2, which is 010, giving the data bits 00010. Run through that process they become:

101111001111100

The strip is written twice. One copy wraps around the top-left finder, and the other is split between the bottom-left and top-right finders. We read both copies out of the grid we drew, and both read 101111001111100, so a reader gets two chances to understand its own settings. Everything else in the code depends on this strip being understood first: without the level, the reader does not know how many bytes are repair bytes, and without the mask number, it cannot unscramble the data.

The data: bits, modes and padding

The payload is turned into a string of bits in one of three modes. A mode indicator comes first, followed by a character count, then the data itself.

  • Numeric packs 3 digits into 10 bits. Indicator 0001.
  • Alphanumeric packs 2 characters from a set of 45 into 11 bits. The set is 0 to 9, capital A to Z, space and $ % * + - . / :. Indicator 0010.
  • Byte uses 8 bits per character. Indicator 0100. Lower case needs it.

The cost shows up in the size. We encoded three strings of 20 characters each at level M:

20 characters Mode Version Grid
12345678901234567890 Numeric 1 21 by 21
ABCDEFGHIJKLMNOPQRST Alphanumeric 1 21 by 21
abcdefghijklmnopqrst Byte 2 25 by 25

Same length, but lower case pushes the code up a version. Our generator picks one mode for the whole string, which is the simple rule. A mixed-mode encoder can switch modes mid-string and sometimes produce a smaller code. We tested a web address with a 26-digit run in it, https://example.com/01/59012341234575901234123457: the single-mode byte version comes out as version 4, and a mixed-mode encoder from the library we measured with fits it in version 3.

For our menu address, the bit string begins like this:

0100  00011000  01101000  01110100 ...
mode  count=24  'h'       't'

That is the 4-bit byte-mode indicator, an 8-bit count for 24 characters (version 1 to 9 codes use 8 bits for byte mode), then each character as a byte. The 24 characters take 192 bits. With the indicator, the count and a 4-bit terminator the total is 208 bits, which is exactly 26 codewords of 8 bits. Version 2 at level M has room for 28 data codewords, so the code is filled with two padding bytes, 11101100 and 00010001 (236 and 17 in decimal), that exist only to fill the space. Thonky’s data encoding page lists both.

We also checked the limit directly. A 26-byte address stays at version 2 at level M. At 27 bytes it jumps to version 3, which is 29 by 29.

Reed-Solomon: spare bytes that repair damage

A version 2 code holds 44 codewords. Of those, 28 carry data and 16 are error correction at level M. The 16 are computed from the 28 using Reed-Solomon coding.

Denso Wave’s page on error correction puts the rule in one line: correcting a given number of codewords takes Reed-Solomon codewords equal to twice that amount. So 16 spare codewords can repair up to 8 damaged codewords out of 44. Denso gives 15 per cent as the figure for level M. The exact share depends on the version and how the damage falls, so treat the four headline levels, 7, 15, 25 and 30 per cent, as approximate. The trade is that a higher level makes room for repairs by making the code bigger. We cover the cost in QR code error correction levels.

A smudge across the middle ruins a few bytes. The reader notices the arithmetic no longer balances, works out which bytes are wrong and fixes them. That is why a code with a logo laid over its centre can still scan.

Masking: why it looks like static

Left alone, data would sometimes line up into big solid blocks or into stripes that look like finder squares, which would confuse the reader. So the encoder scrambles the data area with one of eight fixed mask patterns, each of which flips modules according to a simple row and column rule. Only the data and error correction modules are changed. The finders, timing lines and format strip stay as they are.

The encoder tries all eight and scores each on four penalties, according to Thonky’s explanation of masking: long runs of one colour, 2 by 2 blocks of one colour, finder-like patterns inside the data, and an imbalance between dark and light. Lowest score wins. The library we measured with picked mask 2, which flips every third column. Another encoder may pick a different mask for the same text, and the picture will look different while still decoding to the same address. That is also why two generators can produce two codes for one link that both scan.

The mask number is stored in the format strip, which is why the format strip is read first.

Where the 625 modules go

All 625 modules can be accounted for in version 2:

Three finders with separators   3 x 64 = 192
Alignment pattern                        25
Timing patterns                    9 + 9 = 18
Format strips and dark module      30 + 1 = 31
Function modules                        266
Data and error correction  44 x 8 = 352
Leftover bits                             7
Total                      266 + 352 + 7 = 625

Where the 625 modules go

266 of 625 modules carry no message at all.

  • Finders and separators192Where the code is and which way up
  • Alignment square25Corrects for a tilted phone
  • Timing lines18Sets the grid spacing
  • Format strips and dark module31Error level and mask, stored twice
  • Data and repair bytes359352 for 44 codewords, plus 7 leftover bits
The real version 2 code for https://example.com/menu at level M. Light modules inside each part are tinted so the shapes show.

More than 40 per cent of the code, 266 of 625 modules, is structure that carries no message. In version 1, the smallest code, the share is higher, which is why a tiny payload cannot make the code small.

A short history

Denso Wave released the QR Code in 1994. Its history page says the project was run by a two-person team led by Masahiro Hara, and that it took about a year and a half to reach a working code. The reason was that the barcodes in use could hold only around 20 alphanumeric characters, and users wanted to encode Kanji and Kana. The first version could hold about 7,000 numerals and be read more than 10 times faster than other codes at the time. The QR stands for quick response.

Denso Wave kept its patent rights but declared it would not exercise them, and made the specifications public. With the specifications public, anyone can write a generator, and that is why a code from this site and a code from any other tool follow the same standard.

Using this when you make one

Each extra character can push a code up a size, and the table above shows how much. A few habits follow:

  • Shorter is smaller. A 24-character address at level M is 25 by 25. A 62-character address is version 4, 33 by 33. Bigger squares mean smaller modules at the same print width, so see what size to print a QR code.
  • Where you control the text, capitals and digits are cheaper. A table number or an ID written in capitals fits a smaller code than the same text in lower case.
  • Keep the border blank. The quiet zone is not decoration; the reader needs it to find the first finder.
  • Test before you print. If a code will not scan, 12 causes and fixes goes through them in order.

If the destination might change or the address is long, a dynamic code stores only a short redirect, and so the printed square stays small whatever the final page is. QR Gen makes those. For a fixed link, the static codes from our QR generator never expire and do the job.

Ready to try it? Free, no sign-up, made in your browser.

Open the QR generator