Two-out-of-five code explained

A two-out-of-five code is a constant-weight code that provides exactly ten possible combinations of two bits, and is thus used for representing the decimal digits using five bits. Each bit is assigned a weight, such that the set bits sum to the desired value, with an exception for zero.

According to Federal Standard 1037C:

The weights give a unique encoding for most digits, but allow two encodings for 3: 0+3 or 10010 and 1+2 or 01100. The former is used to encode the digit 3, and the latter is used to represent the otherwise unrepresentable zero.

The IBM 7070, IBM 7072, and IBM 7074 computers used this code to represent each of the ten decimal digits in a machine word, although they numbered the bit positions 0-1-2-3-4, rather than with weights. Each word also had a sign flag, encoded using a two-out-of-three code, that could be A Alphanumeric, Minus, or + Plus. When copied to a digit, the three bits were placed in bit positions 0-3-4. (Thus producing the numeric values 3, 6 and 9, respectively.)

A variant is the United States Postal Service POSTNET barcode, used to represent the ZIP Code for automated mail sorting and routing equipment. This uses two tall bars as ones and three short bars as zeros. Here, the weights assigned to the bit positions are 7-4-2-1-0. Again, zero is encoded specially, using the 7+4 combination (binary 11000) that would naturally encode 11. This method was also used in North American telephone multi-frequency and crossbar switching systems.[1]

The USPS Postal Alpha Numeric Encoding Technique (PLANET) uses the same weights, but with the opposite bar-height convention.

The Code 39 barcode uses weights 1-2-4-7-0 (i.e. LSB first, Parity bit last) for the widths of its bars, but it also encodes two bits of extra information in the spacing between bars. The || ||| spacing is used for digits.

The following table represents decimal digits from 0 to 9 in various two-out-of-five code systems:

DigitTelecommunication
01236
POSTNET

74210

PLANETCode39
bar widths
12470
11100000011 ┃┃┃╻╻▮ | | | ▮
21010000101 ┃┃╻┃╻| ▮ | | ▮
31001000110 ┃┃╻╻┃▮ ▮ | | |
40101001001 ┃╻┃┃╻| | ▮ | ▮
50011001010 ┃╻┃╻┃▮ | ▮ | |
61000101100 ┃╻╻┃┃| ▮ ▮ | |
70100110001 ╻┃┃┃╻| | | ▮ ▮
80010110010 ╻┃┃╻┃▮ | | ▮ |
90001110100 ╻┃╻┃┃| ▮ | ▮ |
00110011000 ╻╻┃┃┃| | ▮ ▮ |
A1––10IBM 707x
Sign flags
1––01
+0––11
Digit01234
IBM 707x

The requirement that exactly two bits be set is strictly stronger than a parity check; like all constant-weight codes, a two-out-of-five code can detect not only any single-bit error, but any unidirectional error -- cases in which all the individual bit errors are of a single type (all 0→1 or all 1→0).

See also

Notes and References

  1. Web site: Vintage Telephone Equipment Museum — XBR #5 MTF Relay Equip. https://web.archive.org/web/20060316134506/http://simplethinking.com/photo/phone/vintage/xbr%20relays.shtml. 2006-03-16. SimpleThinking.com.