Check digits, worked by hand
The last digit of a retail barcode is not part of the product number. It is arithmetic on the digits before it, and it exists so that a scanner which misreads one bar produces an error rather than somebody else's product.
Why the last digit is not data
A scanner reading a smudged label can misread a digit. Without a check, the result is a valid-looking number for a different product, and the till happily charges for it. That failure is worse than no read at all, because nothing about it looks wrong.
A check digit turns a silent wrong answer into a loud refusal. The scanner recomputes the arithmetic from the digits it read; if the answer does not match the digit on the label, it rejects the scan and beeps at the operator to try again.
The EAN-13 calculation, step by step
Take the first twelve digits. Working from the left, multiply them alternately by 1 and 3. Add the results. The check digit is whatever you must add to reach the next multiple of ten.
Work it through for 590123412345.
| Digit | 5 | 9 | 0 | 1 | 2 | 3 | 4 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Weight | 1 | 3 | 1 | 3 | 1 | 3 | 1 | 3 | 1 | 3 | 1 | 3 |
| Product | 5 | 27 | 0 | 3 | 2 | 9 | 4 | 3 | 2 | 9 | 4 | 15 |
- Add the products: 5 + 27 + 0 + 3 + 2 + 9 + 4 + 3 + 2 + 9 + 4 + 15 = 83.
- The next multiple of ten above 83 is 90.
- 90 minus 83 is 7. That is the check digit.
- The complete number is 5901234123457.
The same idea, in the formats around it
Once you have seen it once, most of the family is recognizable.
| Format | How it differs |
|---|---|
| UPC-A | Twelve digits, same alternating scheme, weights starting at 3 from the left |
| EAN-8 | Eight digits, same method over the shorter number |
| ITF-14 | Fourteen digits, same modulo-10 arithmetic |
| ISBN-13 | It is an EAN-13, so it is the same calculation |
| Code 128 | A modulo-103 checksum over character values and their positions, computed and hidden by the encoder rather than printed for humans |
Code 39 is the odd one out: it has an optional check character that many systems do not use at all, relying instead on the fact that each character is individually self-checking in the way its bars are constructed.
What it catches, and the case it misses
A modulo-10 check with alternating weights catches every single-digit error. Change any one digit and the weighted sum shifts by an amount that cannot be a multiple of ten, so the check fails. That is the common case and it is fully covered.
Transpositions, where two adjacent digits swap, are mostly caught but not always. Swapping neighbors changes the weighted sum by twice the difference between them. That lands on a multiple of ten exactly when the two digits differ by five. So swapping a 2 and a 7, or a 3 and an 8, or a 4 and a 9, produces a different number that passes the check.
What to do with this
- Never type a check digit you were given. Let the encoder compute it from the other digits, so a typo anywhere in the number becomes a mismatch you find immediately rather than a valid barcode for the wrong product.
- If a supplier sends you a twelve-digit number for an EAN-13, do not pad it with a zero. Compute the thirteenth. Padding produces a well-formed number that fails at the till.
- If a number a partner supplied fails its own check, stop and ask. It is usually a digit lost in a spreadsheet, and inventing a check digit to make it validate cements the error.