Binary-coded decimal

This is an old revision of this page, as edited by Mfc (talk | contribs) at 15:59, 1 September 2004 (tweak english etc.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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:

Digit Bits      Digit Bits
  0   0000        5   0101
  1   0001        6   0110
  2   0010        7   0111
  3   0011        8   1000
  4   0100        9   1001

To encode a 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 ORed 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, but 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.

Higher density encoding

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 codes that represented numbers, upper-case letters and special characters. Some variation of BCD was used in most early IBM computers, including the 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 labled B, A, 8, 4, 2 and 1. For encoding digits, B and A were zero. The letter A was encoded (B,A,1).