All articles
Explainertechnical9 min read

How Barcodes Actually Work: Encoding, Scanning, and Check Digits

Every day, billions of barcode scans happen without anyone thinking about what's actually going on. A beam of light hits some printed lines, and a number appears. But the engineering behind that process is genuinely clever.

This article breaks down how barcodes encode data, how scanners read them, and the math that makes it all reliable. If you've ever wondered what those lines actually mean, this is for you.

The basic idea

A barcode encodes a number as a pattern of dark bars and light spaces. Each digit is represented by a specific sequence of bar and space widths. A scanner reads the pattern by measuring the widths of the bars and spaces, then translating them back into digits.

The key insight: it's not about the bars themselves. It's about the relative widths. A UPC barcode uses four different widths (1, 2, 3, and 4 "modules" wide). Each digit is encoded as a pattern of two bars and two spaces, totaling 7 modules wide.

Inside a UPC barcode

A standard UPC-A barcode (the most common type in North America) encodes 12 digits. Here's the anatomy:

ZoneContentWidth
Left quiet zoneBlank spaceAt least 9 modules
Start guardBar-space-bar (101)3 modules
Left digits6 digits, "odd" encoding42 modules
Center guardSpace-bar-space-bar-space (01010)5 modules
Right digits6 digits, "even" encoding42 modules
End guardBar-space-bar (101)3 modules
Right quiet zoneBlank spaceAt least 9 modules

The guard patterns (start, center, end) tell the scanner where the data begins and ends. They're always the same pattern, so the scanner can orient itself.

Why left and right digits are encoded differently

Here's a clever design choice: the left-side digits use a different encoding than the right-side digits. In the left encoding, each digit starts with a space (light). In the right encoding, each digit starts with a bar (dark).

This means the barcode can be scanned in either direction. If the scanner reads it backwards (right to left), it will see the "left" encoding where it expects the "right" encoding and vice versa. The software simply reverses the result. This is why cashiers don't have to worry about which way the barcode faces the scanner.

How scanners read the pattern

A barcode scanner works by:

  1. Illuminating the barcode with a beam of light (usually red laser or LED)
  2. Measuring reflections as the beam sweeps across the bars. Dark bars absorb light (low reflection). Light spaces reflect it (high reflection).
  3. Digitizing the reflection pattern into a signal of highs and lows
  4. Measuring widths of each high and low segment to determine the module count
  5. Decoding the module patterns back into digits using a lookup table
  6. Verifying the result with the check digit

Modern image-based scanners (like the camera on your phone) take a photo and process it with software rather than sweeping a laser. The principle is the same: measure the widths of dark and light regions.

The check digit algorithm

The last digit of a UPC (and any GTIN) is a check digit calculated from the other digits. It catches scanning errors, misreads, and data entry mistakes.

Here's how it works for a 12-digit UPC. Take the first 11 digits:

  1. Starting from the first digit, multiply alternating digits by 3 and 1:
Position1234567891011
Digit01234567890
Multiplyx3x1x3x1x3x1x3x1x3x1x3
Result01631251872490
  1. Sum all the results: 0 + 1 + 6 + 3 + 12 + 5 + 18 + 7 + 24 + 9 + 0 = 85

  2. Find the remainder when divided by 10: 85 mod 10 = 5

  3. Subtract from 10: 10 - 5 = 5 (if the result is 10, the check digit is 0)

The check digit is 5, making the full UPC 012345678905.

When a scanner reads the barcode, it runs the same calculation. If the calculated check digit doesn't match the scanned one, the scanner rejects the read and tries again. This is why barcode scanners almost never ring up the wrong product.

EAN-13: the international version

The 13-digit EAN-13 barcode (used outside North America) works similarly but uses the extra digit differently. The first 2 to 3 digits are a "GS1 prefix" that indicates which GS1 organization issued the company prefix:

GS1 prefixIssuing organization
000 to 019GS1 US (these are UPC-A barcodes with a leading 0)
300 to 379GS1 France
400 to 440GS1 Germany
450 to 459, 490 to 499GS1 Japan
460 to 469GS1 Russia
690 to 699GS1 China
880GS1 South Korea

The GS1 prefix indicates where the company is registered, not where the product is manufactured. A product with a GS1 US prefix (starting with 0) may be manufactured anywhere in the world.

From 1D to 2D: what QR codes do differently

Traditional barcodes are one-dimensional: they encode data in one direction (the width of the bars). The height of the bars is just for scanning convenience; it doesn't carry data.

QR codes are two-dimensional: they encode data in both the horizontal and vertical directions using a grid of black and white modules (squares). This lets them carry dramatically more data.

FeatureUPC barcodeQR code
Data capacity12 to 14 digitsUp to 7,089 numeric characters
Error correctionCheck digit (catches ~90% of single-digit errors)Reed-Solomon codes (recovers from up to 30% damage)
OrientationMust be scanned roughly perpendicularCan be scanned at any angle (finder patterns handle orientation)
Data typesNumbers onlyNumbers, text, URLs, binary data

The QR code's three large squares in the corners (called finder patterns) are what allow any camera to instantly locate and orient the code, regardless of angle. The traditional barcode's guard patterns serve a similar purpose but only work in one dimension.

Why this matters for Sunrise 2027

The Sunrise 2027 transition is, at its core, a shift from 1D encoding (lines carrying a number) to 2D encoding (a matrix carrying a URL). The GS1 Digital Link standard defines how to put a GTIN inside a URL that a QR code carries.

The scanner at checkout still extracts the GTIN, just like it always has. But now the code can also carry batch numbers, serial numbers, and a link to a resolver that provides product information. Same purpose, dramatically more capability.

Want the story behind the invention? Read The Barcode: From a Beach Drawing to Every Product on Earth. Ready for the spec? Check out GS1 Digital Link URI Structure.