L-codes, G-codes, R-codes
The three different 7- module patterns that the EAN/UPC family uses to encode digits 0-9.
Why three sets?
In the EAN/UPC family, each digit 0-9 has three different 7-module bar/space patterns - the L-code (odd parity), the G-code (even parity), and the R-code. Most beginners expect one pattern per digit; the three-pattern design is what gives the family two valuable properties:
- Orientation detection. The R-codes are the bitwise complement of the L-codes. That asymmetry lets a scanner detect whether it's reading the symbol left-to-right or right-to-left and decode either way. (Walk past a checkout scanner upside-down - the beep still happens.)
- Implicit 13th digit. EAN-13 prints only 12 patterns but encodes 13 digits. The 13th (leading) digit is carried by the choice between L-code and G-code on each of the six left-half digit positions - that's the parity selection trick.
UPC-A and EAN-8 don't use the parity-selection trick (they always pick L-code for left-half digits and R-code for right-half digits), but they share the same pattern tables. UPC-E uses parity selection too.
The patterns
All three sets are exactly 7 modules per digit. 1 is a dark module (bar); 0 is a light module (space). Source: ISO/IEC 15420 Tables 1-3.
| Digit | Bits | Pattern |
|---|---|---|
| 0 | 0001101 | |
| 1 | 0011001 | |
| 2 | 0010011 | |
| 3 | 0111101 | |
| 4 | 0100011 | |
| 5 | 0110001 | |
| 6 | 0101111 | |
| 7 | 0111011 | |
| 8 | 0110111 | |
| 9 | 0001011 |
| Digit | Bits | Pattern |
|---|---|---|
| 0 | 0100111 | |
| 1 | 0110011 | |
| 2 | 0011011 | |
| 3 | 0100001 | |
| 4 | 0011101 | |
| 5 | 0111001 | |
| 6 | 0000101 | |
| 7 | 0010001 | |
| 8 | 0001001 | |
| 9 | 0010111 |
| Digit | Bits | Pattern |
|---|---|---|
| 0 | 1110010 | |
| 1 | 1100110 | |
| 2 | 1101100 | |
| 3 | 1000010 | |
| 4 | 1011100 | |
| 5 | 1001110 | |
| 6 | 1010000 | |
| 7 | 1000100 | |
| 8 | 1001000 | |
| 9 | 1110100 |
How the three sets relate
- R-code = NOT(L-code). Bit-for-bit, every R pattern is the inversion of the L pattern for the same digit. Compare L[0]
0001101with R[0]1110010- every bit is flipped. That's how a scanner reading the right half knows it's the right half: every "1 module" in the R-pattern is light in the L-pattern. - G-code = reverse(R-code). Each G pattern is the R pattern read right-to-left. Compare R[0]
1110010with G[0]0100111- same bits, opposite order. That makes the G-code "even parity" (an even number of 1-bits) which is the property the parity-selection table exploits. - L-code parity is always odd. Every L-pattern has an odd number of 1-bits (count them:
0001101= three 1-bits). G-patterns always have an even number. The parity selection is literally "is this digit odd-parity or even-parity?"
Where each set appears
| Symbology | Left half | Right half |
|---|---|---|
| EAN-13 | Mix of L and G chosen by the parity-selection table | R only |
| EAN-8 | L only | R only |
| UPC-A | L only | R only |
| UPC-E | Mix of L and G chosen by parity selection on the implicit number system + check digit | (no right half - UPC-E has only 6 data digits) |