Binary-coded decimal

This is an old revision of this page, as edited by 202.158.108.144 (talk) at 08:08, 28 October 2004 (BCD in electronics). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

"BCD" redirects here. For "Buoyancy Control Device" see Buoyancy compensator.

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.

Background

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).

Basics

The following table represents decimal digits from 0 to 9 in various BCD systems:

       NBCD              BCD     BCD
Digit  8421   Excess-3  2421   84-2-1
  0    0000    0011     0000    0000
  1    0001    0100     0001    0111
  2    0010    0101     0010    0110
  3    0011    0110     0011    0101
  4    0100    0111     0100    0100
  5    0101    1000     1011    1011
  6    0110    1001     1100    1010
  7    0111    1010     1101    1001
  8    1000    1011     1110    1000
  9    1001    1100     1111    1111

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

Al Ayyubi21

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 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 labled 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.

See also