Skip to content

Encoding & Structure

What is Mask Pattern?

By Codexengr, QR Systems Engineer · Last reviewed: 2026-10-19

One of 8 XOR patterns applied to the data modules of a QR code to break up large dark or light areas that would confuse the scanner, recorded in the format information bits.

A mask pattern is an exclusive-or (XOR) transformation applied to the data and error-correction modules of a QR code — one of exactly eight patterns, numbered 000 through 111, defined by simple arithmetic on each module's row (i) and column (j). Mask 000, for example, flips every module where (i + j) mod 2 equals zero, producing a checkerboard effect. The functional patterns (finders, timing, alignment, separators) are never masked; only the modules carrying payload data are transformed.

Why masking exists

Raw encoded data is not random. A payload of mostly zeros, a long URL with repeated characters, or structured binary data can produce large solid blocks of dark or light modules, long straight runs, or accidental patches that mimic the finder pattern's 1:1:3:1:1 signature. These regularities are hostile to the decoder: solid regions give the binarization algorithm no edges to lock onto, and finder-like accidents create false position candidates that slow or break detection.

Masking breaks up these regularities by XOR-ing the data area with a pseudo-random-looking pattern. Because the mask is deterministic and its number is stored in the symbol's format information, the decoder simply applies the same XOR a second time to undo it (XOR is its own inverse) and recover the original bits.

How the encoder chooses: penalty scoring

The specification defines four penalty rules that quantify how "decoder-hostile" a masked symbol is: long runs of same-colored modules in rows or columns, 2×2 blocks of identical color, regions resembling the finder pattern ratio, and overall imbalance between dark and light modules. A compliant encoder applies all eight masks to the data area, scores each result against these rules, and selects the mask with the lowest total penalty. This is why the same payload can produce visually different QR codes in different generators only if one of them skips this step — a correct encoder converges on the optimal mask deterministically.

Format information carries the mask number

The decoder needs to know which mask was applied. That information lives in the format information: a 15-bit sequence placed in two copies near the finders, encoding the 2-bit error correction level and the 3-bit mask number, protected by its own error-correcting code so it survives moderate damage. If the format information is unreadable, the decoder can brute-force all eight masks and test which one yields valid data — a fallback most libraries implement, though it costs decode time.

What this means for designers

You cannot and should not choose a mask manually — it is an encoder-internal optimization, and any generator worth using performs the full 8-mask evaluation. Where masking matters to you is in understanding two things: first, that the "random-looking" texture of a QR code is deliberate engineering, not noise; and second, that heavy artistic styling of the data area (large color blocks, gradients) fights against the very uniformity the mask was chosen to achieve. Keep data modules crisp, high-contrast, and geometrically square, and the mask can do its job.

Diagram description: The same payload shown unmasked (with a large solid dark block) versus masked with pattern 000, annotated with the four penalty rules and the format information location that records the chosen mask.

Frequently asked questions

How many mask patterns does a QR code have?

Eight, numbered 000 to 111. Each is defined by a simple arithmetic condition on the module row and column, such as (i + j) mod 2 = 0 for mask 000. The encoder tries all eight and keeps the one with the lowest penalty score.

Can I pick the mask pattern for my QR code?

No, and you should not need to. Mask selection is an automatic encoder optimization defined by the specification. Any compliant generator evaluates all eight masks and selects the best one for your data.

Are the finder patterns affected by masking?

No. Masking applies only to data and error-correction modules. Finder patterns, timing patterns, alignment patterns, separators, and format/version information are functional patterns and are never masked.

How does the scanner know which mask was used?

The 3-bit mask number is stored in the 15-bit format information near the finder patterns, protected by its own error-correcting code. The decoder reads it, applies the same XOR to unmask the data, and decodes normally.

Sources

Related terms

Related tools