Home / Hardware y Moviles / The last digit of a barcode exists so the scanner knows it got it wrong

The last digit of a barcode exists so the scanner knows it got it wrong

You push the trolley to the till, hear a beep and the price appears on the screen. Between the reader and the packaging, something more interesting than a simple read has just happened: the scanner had to find the start and the end of a bar pattern less than four centimetres long, work out how many dark modules sit in each stretch, check the arithmetic for itself and, if it does not add up, refuse to charge you. All of that, two hundred times a second.

The symbol on nearly every supermarket product is an EAN-13: thirteen digits spread over 95 modules, where a module is the smallest unit of width, typically 0.33 millimetres on a small pack. Of those 95 modules, 45 are bars and 50 are spaces, and the whole thing is printed to a tolerance of tenths of a millimetre. A cheap reader from thirty years ago already decoded it; a modern one does it with red light and no moving parts to wear out.

Four dark modules out of every seven

Each digit takes seven modules and is drawn with exactly two bars and two spaces. That is not a matter of taste: if every character always has four dark modules and three light ones, the ink total stays constant and the reader can rebuild the width of a module from the distance between edges, even if the packaging is curved or the print has spread. This is what engineers call a self-clocking code: every character carries its own scale reference.

With seven modules and two bars there are twenty possible combinations; the standard uses ten of them for the digits 0 to 9 in each of its three encoding tables. The tables are called A, B and C: A and B draw the same digit with bars and spaces swapped, and C is the only one that packs two digits into seven modules, because it only needs to count from 00 to 99 without worrying about symmetry.

The thirteenth digit hides in the parity

Here is the trick that fits thirteen numbers into twelve characters. The left half of the symbol, six characters, uses table A or table B depending on what the first digit of the code is. That mix of tables is called parity and has ten valid combinations, one for each value of the first digit. The reader never reads the first number: it deduces it from the way the other six are encoded. The right half always uses table C, which is also the most compact.

Between the two halves sits a central guard pattern, and at the ends, start and end patterns with bars that run longer than the rest. Their job is not decorative: they are synchronisation marks that tell the decoder where the module grid begins and which way it runs. Before and after the whole symbol lies the quiet zone, a blank margin at least nine modules wide, which lets the scanner tell the code apart from the advertising printed on the pack.

A sum that exposes the error before you pay

The last of the thirteen digits is a check digit. It is calculated by multiplying the digits in odd positions by one and those in even positions by three —or the other way round, depending on which end you count from—, adding the results and taking whatever is missing to reach the next multiple of ten. If the reader gets anything else, it throws the whole read away and tries again. Nobody charges you on a misread code.

That scheme catches every single-digit error and almost every transposition of two adjacent digits, the two typical failures of a blurred print or a half-finished read. It does not catch, however, a swap between positions an even distance apart, because the weights repeat every two places and the sum does not change. It is a known and accepted limitation: a barcode is not a strong integrity system, it is a cheap filter that removes the vast majority of doubtful reads.

How a stripe becomes a number

In classic laser readers, a diode emits coherent light that a spinning mirror —a multi-faceted polygon on a motor— sweeps along the symbol. The reflected light returns to a photodiode, which delivers a current proportional to brightness. That signal goes through a differentiator that turns it into peaks at every bar edge, and a comparator digitises it into a sequence of relative widths. The decoder normalises those widths against the local module, tries the three tables, validates the parity and, if everything fits, checks the check digit.

Today’s supermarket scanners have no moving mirror: they use a CCD or CMOS image sensor and process a picture of a strip of the symbol, which lets them read in any orientation in a single pass, even if the cashier drags the product across at an angle. Speed is no longer set by the motor but by the processor: several hundred reads per second, each with its own validation.

When the packet is crumpled

The reason a barcode still works on a bag of crisps is spatial redundancy. A curved pack is never read with a single line: the scanner fires dozens of lines at different angles and heights, and it only takes one of them crossing the whole symbol, guards and quiet zone included. Individual bars can be damaged; what cannot be missing is the sequence.

That is why quality verifiers do not check whether the code “reads”, but with how much margin: they measure symbol contrast, module width uniformity, ink gain or loss and decoding at several points, and grade it from A to F. A low-grade code may read at a well-lit counter and fail in a warehouse with a handheld reader and poor light.

The format that refuses to retire

EAN-13 only identifies a generic product. Logistics has GS1-128, built on Code 128: three switchable character tables, variable length, and an application identifier syntax that tells a batch number apart from an expiry date or a destination address. For pharmaceuticals and unit-level traceability the standard is DataMatrix, a two-dimensional code with error correction that withstands abrasion better and fits on a medicine box.

RFID, with its tag that answers by radio and needs no line of sight, has been announcing the takeover for years. It has not arrived because a barcode costs a fraction of a cent to print inside the packaging itself, and because it works with no battery, no antenna and no interference between trolleys. At the supermarket till, the beep you hear is still the consequence of counting dark modules seven at a time.