Guide

QR code masking, explained

Masking is the last step in making a QR code. The encoder flips some data squares using one of eight fixed patterns, so the code avoids solid blocks and shapes that look like the corner squares. It tries all eight, scores each with four penalty rules, and keeps the lowest score. The scanner undoes the mask before reading.

Masking happens after the text is turned into bits (encoding modes) and repair data is added (error correction). We asked our encoder to make https://example.com/menu/today at level M (a 29 × 29 code, version 3) and logged the penalty it gave each mask:

MaskRunsBlocksFinder look-alikesBalanceTotal
02472558010592
12692792000748
22952314010576
324421016010624
429327016010733
52822911200693
626425216010686
728423712010651

Mask 2 won. It didn't have the fewest runs or blocks; it won because it produced only one shape resembling a corner square, where mask 1 produced five. That single rule moves the totals more than any other.

The eight patterns

A mask is a rule that says, for the module at row i and column j (both counted from 0), "flip this one" or "leave it". Only data and error correction modules are flipped. The corner squares, timing lines, alignment squares and format information are left alone.

MaskFlip the module whenLooks like
0(i + j) mod 2 = 0Checkerboard
1i mod 2 = 0Horizontal stripes
2j mod 3 = 0Every third column
3(i + j) mod 3 = 0Diagonal stripes
4(⌊i / 2⌋ + ⌊j / 3⌋) mod 2 = 0Checkerboard of 2 × 3 blocks
5(i × j) mod 2 + (i × j) mod 3 = 0Cross-like grid
6((i × j) mod 2 + (i × j) mod 3) mod 2 = 0Diamond-like
7((i + j) mod 2 + (i × j) mod 3) mod 2 = 0Mixed diagonals

These come from ISO/IEC 18004, where i is always the row, so mask 1 makes horizontal stripes and mask 2 vertical ones.

The four penalty rules

  1. Runs. Five or more same-colored modules in a row or column score 3, plus 1 for each extra module.
  2. Blocks. Each 2 × 2 block of one color scores 3.
  3. Finder look-alikes. A dark-light-dark-dark-dark-light-dark stretch in the 1:1:3:1:1 ratio of a corner square, with light space beside it, scores 40. It could make a scanner see a corner that isn't there.
  4. Balance. Every full 5% that the share of dark modules strays from 50% scores 10.

Two generators can still draw different-looking codes for the same link. They may pick a different version, error correction level or mode split before masking, and that changes every module. Both codes decode to the same text.

How the scanner knows

The mask number sits in the format information, 15 bits next to the corner squares, alongside the error correction level. It's written twice so a scratch can't erase it, and XORed with 101010000010010 so it's never all light.

In our example the code carries 101111001111100. Undo the XOR and you get 000101001101110: 00 means level M, 010 means mask 2, and the last ten bits protect those five.

What this means if you style a code

Rounded modules, dots and logos change how the masked pattern is drawn, not which mask was picked. When we drew the same code with square, rounded and dot styles, all three decoded. If a styled code fails, look at contrast, size and the quiet zone before anything else.

You can switch between all eight masks on a live code in the interactive explainer and watch the pattern change while the message stays put.

Written by Last updated