Binary-coded decimal
From Wikipedia, the free encyclopedia
In computing and electronic systems, Binary-coded decimal (BCD) is an encoding for decimal numbers in which each digit is represented by its own binary sequence. Its main virtue is that it allows easier conversion to decimal digits for printing or display. Its drawbacks are the complexity of circuits needed to implement mathematical functions and the space wasted in the encoding - 6 wasted patterns per digit. Even though the importance of BCD has diminished, it is still encountered.
In BCD, a digit is usually represented by four bits which, in general, represent the values/digits/characters 0-9. Other combinations are sometimes used for sign or other indications.
Contents |
[edit] Basics
To BCD-encode a decimal number using the common encoding, each digit is encoded using the four-bit binary bit pattern for each digit. For example, the number 127 would be:
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:
- each digit is stored in one byte, and the other four bits are then set to all zeros, all ones (as in the EBCDIC code), or to 0011 (as in the ASCII code)
- two digits are stored in each byte.
Unlike pure binary encodings large numbers can easily be displayed by splitting up the nibbles and sending each to a different character with the logic for each display being a simple mapping function. Converting from pure binary to decimal for display is much harder involving integer multiplication or divide operations. The BIOS in many PCs keeps the date and time in BCD format, probably for historical reasons (it avoided the need for binary to ASCII conversion).
[edit] 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. Therefore, in cases where the calculations are relatively simple working throughout with BCD can lead to a simpler overall system than converting to 'pure' binary.
The same argument applies when hardware of this type uses an embedded microcontroller or other small processor. Often, smaller code results when representing numbers internally in BCD format, since a conversion from or to binary representation can be expensive on such limited processors. For these applications, some small processors feature BCD arithmetic modes, which assist when writing routines that manipulate BCD quantities.
[edit] Packed BCD
A widely used variation of the two-digits-per-byte encoding is called "packed BCD", where numbers end with a sign 'digit', for which the preferred values are 1100 for + and 1101 for −. In packed BCD the number 127 would be represented as the bytes 00010010 01111100, and −127 as 00010010 01111101.
While packed BCD does not make optimal use of storage (about 1/6 of the available memory is wasted in packed BCD), conversion to ASCII, EBCDIC, or the various encodings of Unicode is still trivial, as no arithmetic operations are required. More dense packings of BCD exist; these avoid the storage penalty and also need no arithmetic operations for common conversions.
[edit] Higher-density encodings
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. The latter has the advantage that subsets of the encoding encode two digits in the optimal 7 bits and one digit in 4 bits, as in regular BCD.
[edit] 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 Architecture 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 labelled 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.
In the Decimal Architecture IBM 7070, IBM 7072, and IBM 7074 alphamerics were encoded using digit pairs (using two-out-of-five code in the digits, not BCD) of the 10-digit word, with the "zone" in the left digit and the "digit" in the right digit. Input/Output translation hardware converted between the internal digit pairs and the external standard six-bit BCD codes.
[edit] Addition With BCD
To perform addition in BCD, you can first add-up in binary format, and then perform the conversion to BCD afterwards. This conversion involves adding 6 to each group of four digits that has a value of greater-than 9. For example:
- 9+6=15 = [1001] + [0110] = [1111] in binary.
However, in binary we cannot have a value greater-than 9 (1001) per-nibble. To correct this, one adds 6 to that group:
- 15+6 = [0000 1111] + [0000 0110] = [0001 0101]
which gives us two-nibbles, [0001] and [0101] which correspond to "1" and "5" respectively. This gives us the 15 in BCD which is the correct result.
See also Douglas Jones' Tutorial.
[edit] Background
The binary-coded decimal scheme described in this article is the most common encoding, but there are many others. The method here can be referred to as Simple Binary-Coded Decimal (SBCD) or BCD 8421. Ine the headers to the table, the '8 4 2 1' indicates the four bit weights; note that in the 5th column two of the weights are negative.
The following table represents decimal digits from 0 to 9 in various BCD systems:
| Digit | BCD 8 4 2 1 | Excess-3 or Stibitz Code | BCD 2 4 2 1 or Aiken Code | BCD 8 4 −2 −1 | IBM 702 IBM 705 IBM 7080 IBM 1401 8 4 2 1 |
|---|---|---|---|---|---|
| 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 |
[edit] Legal history
In 1972, the U.S. Supreme Court overturned a lower court decision which had allowed a patent for converting BCD encoded numbers to binary on a computer (see Gottschalk v Benson). This was an important case in determining the patentability of software and algorithms.
[edit] Comparison with pure binary
[edit] Advantages
- Scaling by a factor of 10 (or a power of 10) is simple, this is useful when a decimal scaling factor is needed to represent a non-integer quantity (e.g., in financial calculations where it is required that a computer get the same result that a human would).
- Rounding at a decimal digit boundary is easier.
- Conversion to a character form or for display (e.g., to a text-based format such as XML, or to drive signals for a 7 segment display) is a simple per-digit mapping (conversion from pure binary involves relatively complex logic that spans digits, and gets geometrically worse as the length of the number increases).
[edit] Disadvantages
- Some arithmetic operations are more complex to implement. Adders require extra logic to cause them to wrap and generate a carry early. 15%-20% more circuitry is needed for BCD add compared to pure binary. Multiplication requires the use of algorithms that are somewhat more complex than shift-mask-add (a binary multiplication, requiring binary shifts and adds or the equivalent, per-digit or group of digits is required)
- BCD in raw form requires four bits per digit. When packed so that three digits are encoded in ten bits, the extra storage requirement over pure binary is insignificant for most applications but this makes the encoding even more complex to process.
[edit] See also
[edit] External links
[edit] References
- http://www.danbbs.dk/~erikoest/bcd.htm
- Schmid, Hermann, Decimal computation. New York, Wiley, 1974
- See also the Decimal Arithmetic Bibliography
- Fundamentals of Digital Logic by Brown and Vranesic, 2003cs:BCD
da:BCD (tal) de:BCD-Code es:Código binario decimal fr:Binary coded decimal it:Binary-coded decimal he:עשרוני בקידוד בינארי nl:BCD-code ja:二進化十進表現 pl:Kod BCD pt:Codificação binária decimal sk:BCD kód sv:BCD zh:二進碼十進數

