When encoding a QR code, there are eight mask patterns that you can use to change the outputted matrix. Each mask pattern changes the bits according to their coordinates in the QR matrix. The purpose of a mask pattern is to make the QR code easier for a QR scanner to read.
The Mask Patterns
Each mask pattern uses a formula to determine whether or not to change the color of the current bit. You put the coordinates of the current bit into the formula, and if the result is 0, you use the opposite bit at that coordinate. For example, if the bit for coordinate (1,3) is 1, and the formula is equal to 0 for that coordinate, then you put a 0 at (1,3) instead of a 1.
Here is the list of the mask pattern formulas. Please note that the QR code specification has an error on page 51 where it shows which axis corresponds to i and j. It should have i corresponding to rows and j corresponding to columns. In addition, the formula for mask pattern 7 is INCORRECT on page 51. The correct formula is listed below.
Note: mod is short for Modulo.
|Mask Number||If the formula below is true for a given row/column coordinate, switch the bit at that coordinate|
|0||(row + column) mod 2 == 0|
|1||(row) mod 2 == 0|
|2||(column) mod 3 == 0|
|3||(row + column) mod 3 == 0|
|4||( floor(row / 2) + floor(column / 3) ) mod 2 == 0|
|5||((row * column) mod 2) + ((row * column) mod 3) == 0|
|6||( ((row * column) mod 2) + ((row * column) mod 3) ) mod 2 == 0|
|7||( ((row + column) mod 2) + ((row * column) mod 3) ) mod 2 == 0|