The Universal Product Code (UPC) is a barcode used in United States and Canada for tracking items in stores. It reliably encodes exactly 12 digits and cannot be used to encode letters or other non digit characters. The last digit of the UPC code is called a check digit.
UPC-A uses as many as seven bits to encode a digit. The bit pattern for each numeral is designed to be as different from others as possible. Also, no pattern has more than four subsequent 1s or 0s in order. This increases reliability of scanning. The code can detect all possible single digit errors and most of (89%) transposition errors.
In total, UPC-A encodes 12 digits as S LLLLLL M RRRRRR E, where S (start) and E (end) are the bit pattern 101, M (middle) is the bit pattern 01010. These helper patterns are called guard bars. Every L (left) and R (right) is the mentioned seven bit digit. The encoding always contains 95 bits in total.
Since S, M, and E all include two bars, and each of the 12 digits of the UPC-A barcode consists of two bars and two spaces, all UPC-A barcodes consist of exactly (3 × 2) + (12 × 2) = 30 bars.
The check digit is computed by formula
where d is a value of the digit at the given position. The first counts odd positions and the second counts even positions. The first position is assumed to be 1 and is odd. The same formula is used both for UPC-E and UPC-A.
The numbers in the barcode are standardized, agreed prefixes having the following meanings:
UPC-E is used for smaller packages where a full 12-digit barcode does not fit. This symbology uses a 3 digits per section instead of 6 and does not use middle guard bars. For example a UPC-E barcode with the number 0745982 would expand to the UPC-A 007459000087.
UPC-E check digits are calculated using this expanded string in the same way as used by UPC-A. The exact algorithm of the conversion is the following:
Using LaTeX, this can be written in the form:
UPC-A Bar code symbols can be printed at various densities to accommodate variety of printing and scanning processes. The significant dimensional parameter is called X-dimension, the ideal width of single module element. The X-dimension has to be constant in UPC-A symbol. The width of each bar (dark bar) and space (light bar) is determined by multiplying the X-dimension by the module width of each dark bar or light bar (1,2,3, or 4).