Binarycoded decimal
From Wikipedia, the free encyclopedia
In computing and electronic systems, binarycoded decimal (BCD) (sometimes called natural binarycoded decimal, NBCD) is an encoding for decimal numbers in which each digit is represented by its own binary sequence. Its main virtue is that it allows easy conversion to decimal digits for printing or display and faster decimal calculations. Its drawbacks are the increased complexity of circuits needed to implement mathematical operations and a relatively inefficient encoding—it occupies more space than a pure binary representation.
In BCD, a digit is usually represented by four bits which, in general, represent the values/digits/characters 0–9. Other bit combinations are sometimes used for a sign or other indications.
Although BCD is not as widely used as it once was, decimal fixedpoint and floatingpoint are still important and continue to be used in financial, commercial, and industrial computing.^{[1]} Modern decimal floatingpoint representations use base10 exponents, but not BCD encodings. Current hardware implementations, however, convert the compressed decimal encodings to BCD internally before carrying out computations. Software implementations use BCD or some other 10^{n} base, depending on the operation.
Contents 
[edit] Basics
To BCDencode a decimal number using the common encoding, each decimal digit is stored in a fourbit nibble.
Decimal: 0 1 2 3 4 5 6 7 8 9 BCD: 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
Thus, the BCD encoding for the number 127 would be:
0001 0010 0111
Since most computers store data in eightbit bytes, there are two common ways of storing fourbit BCD digits in those bytes:
 each digit is stored in one nibble of a byte, with the other nibble being 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 binaryencoded numbers, BCDencoded numbers can easily be displayed by mapping each of the nibbles to a different character. Converting a binaryencoded number to decimal for display is much harder, as this generally involves integer multiplication or divide operations. BCD also avoids problems where fractions that can be represented exactly in decimal cannot be in binary (eg onetenth).
[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 utilizing BCD, the manipulation of numerical data for display can be greatly simplified by treating each digit as a separate single subcircuit. This matches much more closely the physical reality of display hardware—a designer might choose to use a series of separate identical 7segment 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 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 twodigitsperbyte encoding is called packed BCD (or simply packed decimal). All of the upper bytes of a multibyte word plus the upper four bits (nibble) of the lowest byte are used to store decimal integers. The lower four bits of the lowest byte are used as the sign flag. As an example, a 32 bit word contains 4 bytes or 8 nibbles. Packed BCD uses the upper 7 nibbles to store the integers of a 7digit decimal value and uses the lowest nibble to indicate the sign of those integers.
Standard sign values are 1100 (hex C) for positive (+) and 1101 (D) for negative (−). Other allowed signs are 1010 (A) and 1110 (E) for positive and 1011 (B) for negative. Some implementations also provide unsigned BCD values with a sign nibble of 1111 (F). In packed BCD, the number 127 is represented by 0001 0010 0111 1100 (127C) and −127 is represented by 0001 0010 0111 1101 (127D).
Sign Digit 
BCD 8 4 2 1 
Sign  Notes 

A  1 0 1 0  +  
B  1 0 1 1  −  
C  1 1 0 0  +  Preferred 
D  1 1 0 1  −  Preferred 
E  1 1 1 0  +  
F  1 1 1 1  +  Unsigned 
No matter how many bytes wide a word is, there are always an even number of nibbles because each byte has two of them. Therefore, a word of n bytes can contain up to (2n)−1 decimal digits, which is always an odd number of digits. A decimal number with d digits requires ½(d+1) bytes of storage space.
For example, a fourbyte (32bit) word can hold seven decimal digits plus a sign, and can represent values ranging from ±9,999,999. Thus the number −1,234,567 is 7 digits wide and is encoded as:
0001 0010 0011 0100 0101 0110 0111 1101 1 2 3 4 5 6 7 −
(Note that, like character strings, the first byte of the packed decimal — with the most significant two digits — is usually stored in the lowest address in memory, independent of the endianness of the machine).
In contrast, a fourbyte binary two's complement integer can represent values from −2,147,483,648 to +2,147,483,647.
While packed BCD does not make optimal use of storage (about ^{1}/_{6} of the memory used is wasted), conversion to ASCII, EBCDIC, or the various encodings of Unicode is still trivial, as no arithmetic operations are required. The extra storage requirements are usually offset by the need for the accuracy that fixedpoint decimal arithmetic provides. Denser packings of BCD exist which avoid the storage penalty and also need no arithmetic operations for common conversions.
[edit] Fixedpoint packed decimal
Fixedpoint decimal numbers are supported by some programming languages (such as COBOL and PL/I), and provide an implicit decimal point in front of one of the digits. For example, a packed decimal value encoded with the bytes 12 34 56 7C represents the fixedpoint value +1,234.567 when the implied decimal point is located between the 4th and 5th digits.
12 34 56 7C 12 34.56 7+
[edit] Higherdensity encodings
If a decimal digit requires four bits, then three decimal digits require 12 bits. However, since 2^{10} (1,024) is greater than 10^{3} (1,000), if three decimal digits are encoded together, only 10 bits are needed. Two such encodings are ChenHo 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] Zoned decimal
Some implementations (notably IBM mainframe systems) support zoned decimal numeric representations. Each decimal digit is stored in one byte, with the lower four bits encoding the digit in BCD form. The upper four bits, called the "zone" bits, are usually set to a fixed value so that the byte holds a character value corresponding to the digit. EBCDIC systems use a zone value of 1111 (hex F); this yields bytes in the range F0 to F9 (hex), which are the EBCDIC codes for the characters "0" through "9". Similarly, ASCII systems use a zone value of 0011 (hex 3), giving character codes 30 to 39 (hex).
For signed zoned decimal values, the rightmost (least significant) zone nibble holds the sign digit, which is the same set of values that are used for signed packed decimal numbers (see above). Thus a zoned decimal value encoded as the hex bytes F1 F2 D3 represents the signed decimal value −123:
F1 F2 D3 1 2 −3
[edit] EBCDIC zoned decimal conversion table
BCD Digit  EBCDIC Character  Hexadecimal  

0+ 
{ (*) 
\ (*) 
C0 
A0 
E0 

1+ 
A 
~ (*) 
C1 
A1 
E1 

2+ 
B 
s 
S 
C2 
A2 
E2 
3+ 
C 
t 
T 
C3 
A3 
E3 
4+ 
D 
u 
U 
C4 
A4 
E4 
5+ 
E 
v 
V 
C5 
A5 
E5 
6+ 
F 
w 
W 
C6 
A6 
E6 
7+ 
G 
x 
X 
C7 
A7 
E7 
8+ 
H 
y 
Y 
C8 
A8 
E8 
9+ 
I 
z 
Z 
C9 
A9 
E9 
0− 
} (*) 
^ (*) 
D0 
B0 

1− 
J 
D1 
B1 

2− 
K 
D2 
B2 

3− 
L 
D3 
B3 

4− 
M 
D4 
B4 

5− 
N 
D5 
B5 

6− 
O 
D6 
B6 

7− 
P 
D7 
B7 

8− 
Q 
D8 
B8 

9− 
R 
D9 
B9 
(*) Note: These characters vary depending on the local character code page.
[edit] Fixedpoint zoned decimal
Some languages (such as COBOL and PL/I) directly support fixedpoint zoned decimal values, assigning an implicit decimal point at some location between the decimal digits of a number. For example, given a sixbyte signed zoned decimal value with an implied decimal point to the right of the fourth digit, the hex bytes F1 F2 F7 F9 F5 C0 represent the value +1,279.50:
F1 F2 F7 F9 F5 C0 1 2 7 9. 5 +0
[edit] IBM and BCD
IBM used the terms binarycoded decimal and BCD for 6bit alphameric codes that represented numbers, uppercase letters and special characters. Some variation of BCD alphamerics was used in most early IBM computers, including the IBM 1620, IBM 1400 series, and nonDecimal Architecture members of the IBM 700/7000 series.
Bit positions in BCD alphamerics 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 IBM 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 6bit BCD codes.
In the Decimal Architecture IBM 7070, IBM 7072, and IBM 7074 alphamerics were encoded using digit pairs (using twooutoffive code in the digits, not BCD) of the 10digit 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 sixbit BCD codes.
With the introduction of System/360, IBM expanded 6bit BCD alphamerics to 8bit EBCDIC, allowing the addition of many more characters (e.g., lowercase letters). A variable length Packed BCD numeric data type was also implemented.
Today, BCD data is still heavily used in IBM processors and databases, such as IBM DB2, mainframes, and Power6. In these products, the BCD is usually zoned BCD (as in EBCDIC or ASCII), Packed BCD, or 'pure' BCD encoding. All of these are used within hardware registers and processing units, and in software.
[edit] Addition with BCD
It is possible to perform addition in BCD by first adding in binary, and then converting to BCD afterwards. Conversion of the simple sum of two digits can be done by adding 6 (that is, 16 – 10) when the result has a value greater than 9. For example:
1001 + 1000 = 10001 = 0001 0001 9 + 8 = 17 = 1 1
In BCD, there cannot exist a value greater than 9 (1001) per nibble. To correct this, 6 (0110) is added to that sum to get the correct first two digits:
0001 0001 + 0000 0110 = 0001 0111 1 1 + 0 6 = 1 7
which gives two nibbles, 0001 and 0111, which correspond to the digits "1" and "7". This yields "17" in BCD, which is the correct result. This technique can be extended to adding multiple digits, by adding in groups from right to left, propagating the second digit as a carry, always comparing the 5bit result of each digitpair sum to 9.
[edit] Subtraction with BCD
Subtraction is done by adding the ten's complement of the subtrahend. To represent the sign of a number in BCD, the number 0000 is used to represent a positive number, and 1001 is used to represent a negative number. The remaining 14 combinations are invalid signs. To illustrate signed BCD subtraction, consider the following problem: 357  432.
In signed BCD, 357 is 0000 0011 0101 0111. The ten's complement of 432 can be obtained by taking the nine's complement of 432, and then adding one. So, 999  432 = 567, and 567 + 1 = 568. By preceding 568 in BCD by the negative sign code, the number 432 can be represented. So, 432 in signed BCD is 1001 0101 0110 1000.
Now that both numbers are represented in signed BCD, they can be added together:
0000 0011 0101 0111 + 1001 0101 0110 1000 = 1001 1000 1011 1111 0 3 5 7 + 9 5 6 8 = 9 8 11 15
Since BCD is a form of decimal representation, several digits sums above are invalid. In the event that an invalid entry (any BCD digit greater than 1001) exists, simply add 6 to generate a carry bit and cause the sum to become a valid entry. The reason for adding 6 is because there are 16 possible 4bit BCD values (since 2^{4} = 16), but only 10 values are valid (0000 through 1001). So, adding 6 to the invalid entries results in the following:
1001 1000 1011 1111 + 0000 0000 0110 0110 = 1001 1001 0010 0101 9 8 11 15 0 0 6 6 9 9 2 5
So, the result of the subtraction is 1001 1001 0010 0101 (925). To check the answer, note that the first bit is the sign bit, which is negative. This seems to be correct, since 357  432 should result in a negative number. To check the rest of the digits, represent them in decimal. 1001 0010 0101 is 925. The ten's complement of 925 is 1000  925 = 999  925 + 1 = 074 + 1 = 75, so the calculated answer is 75. To check, perform standard subtraction to verify that 357  432 is 75.
Note that in the event that there are a different number of nibbles being added together (such as 1053  122), the number with the fewest number of digits must first be padded with zeros before taking the ten's complement or subtracting. So, with 1053  122, 122 would have to first be represented as 0122, and the ten's complement of 0122 would have to be calculated.
[edit] Background
The binarycoded 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 BinaryCoded Decimal (SBCD) or BCD 8421. In the headers to the table, the '8 4 2 1', etc., indicates the weight of each bit shown; note that in the 5^{th} column two of the weights are negative. Both ASCII and EBCDIC character codes for the digits are examples of zoned BCD, and are also shown in the table.
The following table represents decimal digits from 0 to 9 in various BCD systems:
Digit 
BCD 8 4 2 1 
Excess3 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 
ASCII 0000 8421 
EBCDIC 0000 8421 

0  0000  0011  0000  0000  1010  0011 0000  1111 0000 
1  0001  0100  0001  0111  0001  0011 0001  1111 0001 
2  0010  0101  0010  0110  0010  0011 0010  1111 0010 
3  0011  0110  0011  0101  0011  0011 0011  1111 0011 
4  0100  0111  0100  0100  0100  0011 0100  1111 0100 
5  0101  1000  1011  1011  0101  0011 0101  1111 0101 
6  0110  1001  1100  1010  0110  0011 0110  1111 0110 
7  0111  1010  1101  1001  0111  0011 0111  1111 0111 
8  1000  1011  1110  1000  1000  0011 1000  1111 1000 
9  1001  1100  1111  1111  1001  0011 1001  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
 Many nonintegral values, such as decimal 0.2, have an infinite placevalue representation in binary (.001100110011...) but have a finite placevalue in binarycoded decimal (0.0010). Consequently a system based on binarycoded decimal representations of decimal fractions avoids errors representing and calculating such values.
 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 noninteger quantity (e.g., in financial calculations)
 Rounding at a decimal digit boundary is simpler. Addition and subtraction in decimal does not require rounding.
 Alignment of two decimal numbers (for example 1.3 + 27.08) is a simple, exact, shift.
 Conversion to a character form or for display (e.g., to a textbased format such as XML, or to drive signals for a sevensegment display) is a simple perdigit mapping, and can be done in linear (O(n)) time. Conversion from pure binary involves relatively complex logic that spans digits, and for large numbers no lineartime conversion algorithm is known (see Binary numeral system).
[edit] Disadvantages
 Some 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 shiftmaskadd (a binary multiplication, requiring binary shifts and adds or the equivalent, perdigit or group of digits is required)
 Standard BCD requires four bits per digit, roughly 20% more space than a binary encoding. When packed so that three digits are encoded in ten bits, the storage overhead is reduced to about 0.34%, at the expense of an encoding that is unaligned with the 8bit byte boundaries common on existing hardware, resulting in slower implementations on these systems.
 Practical existing implementations of BCD are typically slower than operations on binary representations, especially on embedded systems, due to limited processor support for native BCD operations.
[edit] Application
The BIOS in many personal computers keeps the date and time in BCD, probably for historical reasons: it avoids difficult conversions between binary and ASCII.
The Atari 8bit family of computers implemented its floatingpoint algorithms in BCD, which feature was facilitated by the availability of BCD addition and subtraction in the computer's 6502 microprocessor.
[edit] Representational variations
Various BCD implementations exist that employ other representations for numbers. Programmable calculators manufactured by Texas Instruments, HewlettPackard, and others typically employ a floatingpoint BCD format, typically with two or three digits for the (decimal) exponent. The extra bits of the sign digit may be used to indicate special numeric values, such as infinity, underflow/overflow, and error (a blinking display).
[edit] Alternative encodings
If errors in representation and computation are more important than the speed of conversion to and from display, a scaled binary representation may be used, which stores a decimal number as a binaryencoded integer and a binaryencoded signed decimal exponent. For example, 0.2 can be represented as 2 × 10^{1}.
This representation allows rapid multiplication and division, but may require shifting by a power of 10 during addition and subtraction, to align the decimal points. It is appropriate for applications with a fixed number of decimal places that do not then require this adjustment— particularly financial applications where 2 or 4 digits after the decimal point are usually enough. Indeed this is almost a form of fixed point arithmetic since the position of the radix point is implied.
ChenHo encoding provides a boolean transformation for converting groups of three BCDencoded digits to and from 10bit values that can be efficiently encoded in hardware with only 2 or 3 gate delays. Densely Packed Decimal is a similar scheme that deals more efficiently and conveniently with the case where the number of digits is not a multiple of 3; this is the decimal encoding used in the IEEE 7542008 Standard.
[edit] See also
[edit] References
 Arithmetic Operations in Digital Computers, R. K. Richards, 397pp, D. Van Nostrand Co., NY, 1955
 Schmid, Hermann, Decimal computation, ISBN 047176180X, 266pp, Wiley, 1974
 Superoptimizer: A Look at the Smallest Program, Henry Massalin, ACM Sigplan Notices, Vol. 22 #10 (Proceedings of the Second International Conference on Architectural support for Programming Languages and Operating Systems), pp122126, ACM, also IEEE Computer Society Press #87CH24406, October 1987
 VLSI designs for redundant binarycoded decimal addition, Behrooz Shirazi, David Y. Y. Yun, and Chang N. Zhang, IEEE Seventh Annual International Phoenix Conference on Computers and Communications, 1988, pp5256, IEEE, March 1988
 Fundamentals of Digital Logic by Brown and Vranesic, 2003
 Modified Carry Look Ahead BCD Adder With CMOS and Reversible Logic Implementation, Himanshu Thapliyal and Hamid R. Arabnia, Proceedings of the 2006 International Conference on Computer Design (CDES'06), ISBN 1601320094, pp6469, CSREA Press, November 2006
 Reversible Implementation of DenselyPackedDecimal Converter to and from BinaryCodedDecimal Format Using in IEEE754R, A. Kaivani, A. Zaker Alhosseini, S. Gorgin, and M. Fazlali, 9th International Conference on Information Technology (ICIT'06), pp273276, IEEE, December 2006.
See also the Decimal Arithmetic Bibliography