Binary-coded decimal (BCD) is a numeral system used in computing and in electronics systems. In BCD, numbers are represented as a sequence of decimal digits in which each digit is represented by four bits.
The binary-coded decimal method discussed in this article, BCD, is the most common one but not the only one. For clarity the method here can be referred to as Natural Binary-Coded Decimal (NBCD) or BCD 8421. The 8421 digit series comes from the bit weights in the byte. Other BCD methods include Excess-3 and BCD 2421 where the bits are balanced. Unlike in NBCD, in BCD 2421 zeros will appear as often as ones. In electronic devices unbalance could cause unwanted electric charges. Another balanced system is BCD 84-2-1 (eight, four, minus two, minus one).
The following table represents decimal digits from 0 to 9 in various BCD systems:
NBCD BCD BCD IBM 1401 Digit 8421 Excess-3 2421 84-2-1 8421 0 0000 0011 0000 0000 1010 1 0001 0100 0001 0111 0001 2 0010 0101 0010 0110 0010 3 0011 0110 0011 0101 0011 4 0100 0111 0100 0100 0100 5 0101 1000 1011 1011 0101 6 0110 1001 1100 1010 0110 7 0111 1010 1101 1001 0111 8 1000 1011 1110 1000 1000 9 1001 1100 1111 1111 1001
To BCD-encode a decimal number such as 127, for example, each of the decimal digits is encoded using the bit pattern shown above, that is: 0001, 0010, 0111.
Since most computers store data in eight-bit bytes, there are two common ways of storing four-bit BCD digits in those bytes: either one can simply ignore the extra four bits of each byte, usually filling them with either zero bits or one bits (as in EBCDIC); or one can store two digits per byte, called "packed" BCD (packed BCD numbers also usually end with a sign 'digit', for which the preferred values are 1100 for + and 1101 for -). Thus the number 127 would be represented as (11110001, 11110010, 11110111) in EBCDIC or (00010010, 01111100) in packed BCD.
While BCD is wasteful (about 1/6 of the available memory is wasted, even in packed BCD), it has a direct correspondence to the ASCII character set if the BCD number is prepended with 0011 or OR'ed with 00110000 (decimal 48), and similarly maps to EBCDIC characters if the BCD code is prefixed with 1111.
Further, large numbers can easily be displayed on 7-element displays by splitting up the nybbles and sending each to a different character (the individual characters often have the wiring to display the correct figures). The BIOS in PCs usually keeps the date and time in BCD format, probably for historical reasons (it avoided the need for binary to ASCII conversion).
BCD is still in wide use, and decimal arithmetic is often carried out using BCD or similar encodings.
BCD in electronics
BCD is very common in electronic systems where a numeric value is to be displayed, especially in systems consisting solely of digital logic, and not containing a microprocessor. By utilising BCD, the manipulation of numerical data for display can be greatly simplified by treating each digit as a separate single sub-circuit. This matches much more closely the physical reality of display hardware - a designer might choose to use a series of separate identical 7-segment displays to build a metering circuit, for example. If the numeric quantity were stored and manipulated as pure binary, interfacing to such a display would require complex circuitry. By working throughout with BCD, a much simpler overall system results.
If a decimal digit requires four bits, then three decimal digits require 12 bits. However, since 210>103, if three decimal digits are encoded together then only 10 bits are needed. Two such encodings are Chen-Ho encoding and densely packed decimal.
IBM and BCD
IBM used the terms binary-coded decimal and BCD for six-bit alphameric codes that represented numbers, upper-case letters and special characters. Some variation of BCD was used in most early IBM computers, including the IBM 1620, IBM 1400 series and non-decimal members of the IBM 700/7000 series. With the introduction of System/360, IBM replaced BCD with 8-bit EBCDIC.
Bit positions in BCD were usually labeled B, A, 8, 4, 2 and 1. For encoding digits, B and A were zero. The letter A was encoded (B,A,1).
In the 1620 BCD alphamerics were encoded using digit pairs, with the "zone" in the even digit and the "digit" in the odd digit. Input/Output translation hardware converted between the internal digit pairs and the external standard six-bit BCD codes.
- IBM: Chen-Ho encoding (http://www2.hursley.ibm.com/decimal/chen-ho.html)
- IBM: densely packed decimal (http://www2.hursley.ibm.com/decimal/DPDecimal.html).