BrightKidz Library
Subjects

How a Barcode Catches Its Own Mistake

About 13 minutes

Look at the row of digits printed under a barcode. There are usually thirteen of them.

Only twelve of those digits say which thing it is. The thirteenth says nothing at all about the product. It is a guard, and its whole job is to notice when the other twelve have been read wrong.

That is the part worth knowing. A number that checks itself is not a stored list of every valid code sitting somewhere. It is a small piece of arithmetic travelling inside the number.

Work out the guard yourself

Here is a twelve-digit number: 1 2 3 4 5 6 7 8 9 0 1 2. The thirteenth digit is not a choice. Take it apart and it falls out.

Twelve bars, then one more that is worked out from all of them A row of twelve solid upright bars of different widths, the way a barcode is printed. A bold arrow points from the end of the row across a clear gap to a single square drawn in outline with a large tick inside it. The twelve bars carry the number; the mark on its own at the end is the guard digit, and it is calculated from the twelve rather than chosen.

Follow the same steps a scanner follows, one at a time.

  1. Number the digits from the left: 1st, 2nd, 3rd and so on, up to the 12th. The position matters more than you would expect, and everything below depends on it.
  2. Add up the digits in the odd positions — the 1st, 3rd, 5th, 7th, 9th and 11th. Those are 1, 3, 5, 7, 9 and 1, which come to 26.
  3. Now the even positions — the 2nd, 4th, 6th, 8th, 10th and 12th. Those are 2, 4, 6, 8, 0 and 2, which come to 22. Keep the two totals apart.
  4. The even ones count triple. So the whole total is 26 + 3 × 22, which is 26 + 66 = 92.
  5. Now climb to the next multiple of ten. From 92 that is 100, and the step is 8. That 8 is the guard digit, and the full number is 1234567890128.

Nobody picked the 8. It was already decided by the twelve digits in front of it.

That is the whole arrangement: with the guard on the end, the weighted total of all thirteen digits lands exactly on a multiple of ten. A scanner does not need to know what the product is. It adds up what it read, and if the answer does not end in a zero, something went wrong.

Try one of your own

Take 5 9 0 1 2 3 4 1 2 3 4 5 and work out its guard the same way before reading on.

The odd positions hold 5, 0, 2, 4, 2 and 4, which come to 17. The even positions hold 9, 1, 3, 1, 3 and 5, which come to 22. So the total is 17 + 66 = 83, the next multiple of ten is 90, and the guard is 7.

Why one wrong digit can never hide

Now the reason it works, which is short and rather satisfying.

Suppose a scanner misreads exactly one digit. The digit it saw is somewhere between 9 too big and 9 too small. Those are the only sizes of mistake a single digit can make.

If that digit sat in an odd position, it counted once. So the total moved by somewhere between 1 and 9, one way or the other. A total sitting on a multiple of ten cannot move by 1 to 9 and land on another one, because the nearest multiples of ten are a full ten apart.

If the digit sat in an even position, it counted triple, so the total moved by three times the mistake. These are the only moves available:

3 · 6 · 9 · 12 · 15 · 18 · 21 · 24 · 27

Look down that list for a multiple of ten. There is not one, and there could not be, because tripling something smaller than ten only ever lands on a number ending in 3, 6 or 9.

So a single misread digit always breaks the multiple of ten. Not usually. Always. The scanner refuses, and the number gets read again.

The mistake it lets through

Here is where it gets more interesting, because the guard is not perfect, and it is worth knowing exactly where the hole is.

The commonest mistake a person makes typing a long number is not a wrong digit. It is swapping two digits that sit next to each other — typing 38 where the number says 83.

Sometimes the guard catches that. Sometimes it does not.

Take 3 8 1 6 0 0 0 0 0 0 0 0, whose guard is 4. Now swap the first two digits to get 8 3 1 6 0 0 0 0 0 0 0 0 — and its guard is 4 as well. Two different numbers, the same guard. The scanner accepts the wrong one without a murmur.

But swap the first two digits of 1 2 3 4 0 0 0 0 0 0 0 0, whose guard is 8, and you get a number whose guard is 0. Caught straight away.

Some swaps slip through and some do not. There is a rule underneath that, and it is findable.

Six pairs of side-by-side digits, each one typed the wrong way round. Decide which swaps the guard notices. Look at the two digits in each pair and see what the ones it misses have in common.

  • 3 and 8
  • 1 and 2
  • 4 and 9
  • 3 and 9
  • 0 and 5
  • 7 and 8

Five apart, every time

The three that slip through are 3 and 8, 4 and 9, 0 and 5. Every one of those pairs is exactly five apart. The three it catches are one apart, six apart and one apart.

That is the rule, and here is where it comes from.

Two digits sitting side by side are always in an odd position and an even position, so one of them counts once and the other counts triple. Swap them and the one that counted triple now counts once, and the other way round.

The result is that the total moves by twice the difference between the two digits. Work down the possibilities:

2 · 4 · 6 · 8 · 10 · 12 · 14 · 16 · 18

Exactly one of those is a multiple of ten, and it is the fifth. Twice five is ten, and shifting a total by a whole ten leaves it on a multiple of ten — so the guard sees nothing wrong.

There are only five pairs of digits sitting five apart: 0 and 5, 1 and 6, 2 and 7, 3 and 8, 4 and 9. Every other swap is caught. Those five are the only way past, and they are the price of a guard small enough to fit into one digit.

A scanner reads a code and the weighted total comes to 97. What does it know?

Next time you are near a barcode, cover the last digit and work it out. If your answer matches the one printed there, you have done exactly what the scanner does — and you know something it does not, which is precisely which mistakes it is going to miss.