Binary-coded decimal
In
In byte-oriented systems (i.e. most modern computers), the term unpacked BCD[1] usually implies a full byte for each digit (often including a sign), whereas packed BCD typically encodes two digits within a single byte by taking advantage of the fact that four bits are enough to represent the range 0 to 9. The precise four-bit encoding, however, may vary for technical reasons (e.g. Excess-3).
The ten states representing a BCD digit are sometimes called ,
BCD's main virtue, in comparison to binary
BCD was used in many early
BCD per se is not as widely used as in the past, and is unavailable or limited in newer instruction sets (e.g.,
Background
BCD takes advantage of the fact that any one decimal numeral can be represented by a four-bit pattern. An obvious way of encoding digits is Natural BCD (NBCD), where each decimal digit is represented by its corresponding four-bit binary value, as shown in the following table. This is also called "8421" encoding.
Decimal digit | BCD | |||
---|---|---|---|---|
8 | 4 | 2 | 1 | |
0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 1 |
2 | 0 | 0 | 1 | 0 |
3 | 0 | 0 | 1 | 1 |
4 | 0 | 1 | 0 | 0 |
5 | 0 | 1 | 0 | 1 |
6 | 0 | 1 | 1 | 0 |
7 | 0 | 1 | 1 | 1 |
8 | 1 | 0 | 0 | 0 |
9 | 1 | 0 | 0 | 1 |
This scheme can also be referred to as Simple Binary-Coded Decimal (SBCD) or BCD 8421, and is the most common encoding.[12] Others include the so-called "4221" and "7421" encoding – named after the weighting used for the bits – and "Excess-3".[13] For example, the BCD digit 6, 0110'b
in 8421 notation, is 1100'b
in 4221 (two encodings are possible), 0110'b
in 7421, while in Excess-3 it is 1001'b
().
Bit | Weight | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | Comment |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
4 | 8 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | Binary |
3 | 4 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | |
2 | 2 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 1 | |
1 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | 0 | 1 | |
Name | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | Decimal | |
8 4 2 1 (XS-0) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | [14][15][16][17][nb 2] | |
7 4 2 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | [18][19][20] | |||||||
Aiken (2 4 2 1) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | [14][15][16][17][nb 3] | |||||||
Excess-3 (XS-3) |
-3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | [14][15][16][17][nb 2] | |
Excess-6 (XS-6) |
-6 | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | [18][nb 2] | |
Jump-at-2 (2 4 2 1) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | [16][17] | |||||||
Jump-at-8 (2 4 2 1) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | [21][22][16][17][nb 4] | |||||||
4 2 2 1 (I) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | [16][17] | |||||||
4 2 2 1 (II) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | [21][22] | |||||||
5 4 2 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | [18][14][16][17] | |||||||
5 2 2 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | [14][16][17] | |||||||
5 1 2 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | [19] | |||||||
5 3 1 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | [16][17] | |||||||
White (5 2 1 1) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | [23][18][14][16][17] | |||||||
5 2 1 1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | [24] | |||||||
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | |||
Magnetic tape | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | [15] | |||||||
Paul | 1 | 3 | 2 | 6 | 7 | 5 | 4 | 0 | 8 | 9 | [25] | |||||||
Gray |
0 | 1 | 3 | 2 | 6 | 7 | 5 | 4 | 15 | 14 | 12 | 13 | 8 | 9 | 11 | 10 | [26][14][15][16][17][nb 2] | |
Glixon |
0 | 1 | 3 | 2 | 6 | 7 | 5 | 4 | 9 | 8 | [27][14][15][16][17] | |||||||
Ledley | 0 | 1 | 3 | 2 | 7 | 6 | 4 | 5 | 8 | 9 | [28] | |||||||
4 3 1 1 | 0 | 1 | 2 | 3 | 5 | 4 | 6 | 7 | 8 | 9 | [19] | |||||||
LARC | 0 | 1 | 2 | 4 | 3 | 5 | 6 | 7 | 9 | 8 | [29] | |||||||
Klar | 0 | 1 | 2 | 4 | 3 | 9 | 8 | 7 | 5 | 6 | [2][3] | |||||||
Petherick (RAE) |
1 | 3 | 2 | 0 | 4 | 8 | 6 | 7 | 9 | 5 | [30][31][nb 5] | |||||||
O'Brien I (Watts) |
0 | 1 | 3 | 2 | 4 | 9 | 8 | 6 | 7 | 5 | [32][14][16][17][nb 6] | |||||||
5-cyclic | 0 | 1 | 3 | 2 | 4 | 5 | 6 | 8 | 7 | 9 | [28] | |||||||
Tompkins I |
0 | 1 | 3 | 2 | 4 | 9 | 8 | 7 | 5 | 6 | [33][14][16][17] | |||||||
Lippel | 0 | 1 | 2 | 3 | 4 | 9 | 8 | 7 | 6 | 5 | [34][35][14] | |||||||
O'Brien II |
0 | 2 | 1 | 4 | 3 | 9 | 7 | 8 | 5 | 6 | [32][14][16][17] | |||||||
Tompkins II |
0 | 1 | 4 | 3 | 2 | 7 | 9 | 8 | 5 | 6 | [33][14][16][17] | |||||||
Excess-3 Gray |
-3 | -2 | 0 | -1 | 4 | 3 | 1 | 2 | 12 | 11 | 9 | 10 | 5 | 6 | 8 | 7 | [16][17][20][nb 7][nb 2] | |
6 3 −2 −1 (I) | 3 | 2 | 1 | 0 | 5 | 4 | 8 | 9 | 7 | 6 | [29][36] | |||||||
6 3 −2 −1 (II) | 0 | 3 | 2 | 1 | 6 | 5 | 4 | 9 | 8 | 7 | [29][36] | |||||||
8 4 −2 −1 | 0 | 4 | 3 | 2 | 1 | 8 | 7 | 6 | 5 | 9 | [29] | |||||||
Lucal |
0 | 15 | 14 | 1 | 12 | 3 | 2 | 13 | 8 | 7 | 6 | 9 | 4 | 11 | 10 | 5 | [37] | |
Kautz I | 0 | 2 | 5 | 1 | 3 | 7 | 9 | 8 | 6 | 4 | [18] | |||||||
Kautz II | 9 | 4 | 1 | 3 | 2 | 8 | 6 | 7 | 0 | 5 | [18][14] | |||||||
Susskind I | 0 | 1 | 4 | 3 | 2 | 9 | 8 | 5 | 6 | 7 | [35] | |||||||
Susskind II | 0 | 1 | 9 | 8 | 4 | 3 | 2 | 5 | 6 | 7 | [35] | |||||||
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
The following table represents decimal digits from 0 to 9 in various BCD encoding systems. In the headers, the "8 4 2 1
" indicates the weight of each bit. In the fifth column ("BCD 8 4 −2 −1"), two of the weights are negative. Both ASCII and EBCDIC character codes for the digits, which are examples of zoned BCD, are also shown.
Digit |
BCD 8 4 2 1 |
Stibitz code or Excess-3 | Aiken-Code or BCD2 4 2 1 |
BCD 8 4 −2 −1 |
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 |
As most computers deal with data in 8-bit bytes, it is possible to use one of the following methods to encode a BCD number:
- Unpacked: Each decimal digit is encoded into one byte, with four bits representing the number and the remaining bits having no significance.
- Packed: Two decimal digits are encoded into a single byte, with one digit in the least significant nibble (bits 0 through 3) and the other numeral in the most significant nibble (bits 4 through 7).[nb 8]
As an example, encoding the decimal number 91
using unpacked BCD results in the following binary pattern of two bytes:
Decimal: 9 1 Binary : 0000 1001 0000 0001
In packed BCD, the same number would fit into a single byte:
Decimal: 9 1 Binary : 1001 0001
Hence the numerical range for one unpacked BCD byte is zero through nine inclusive, whereas the range for one packed BCD byte is zero through ninety-nine inclusive.
To represent numbers larger than the range of a single byte any number of contiguous bytes may be used. For example, to represent the decimal number 12345
in packed BCD, using
Decimal: 0 1 2 3 4 5 Binary : 0000 0001 0010 0011 0100 0101
Here, the most significant nibble of the most significant byte has been encoded as zero, so the number is stored as 012345
(but formatting routines might replace or remove leading zeros). Packed BCD is more efficient in storage usage than unpacked BCD; encoding the same number (with the leading zero) in unpacked format would consume twice the storage.
Shifting and masking operations are used to pack or unpack a packed BCD digit. Other bitwise operations are used to convert a numeral to its equivalent bit pattern or reverse the process.
Packed BCD
In packed BCD (or packed decimal
As an example, a 4-byte value consists of 8 nibbles, wherein the upper 7 nibbles store the digits of a 7-digit decimal value, and the lowest nibble indicates the sign of the decimal integer value. Standard sign values are 1100 (hex C) for positive (+) and 1101 (D) for negative (−). This convention comes from the zone field for EBCDIC characters and the signed overpunch representation.
Other allowed signs are 1010 (A) and 1110 (E) for positive and 1011 (B) for negative. IBM System/360 processors will use the 1010 (A) and 1011 (B) signs if the A bit is set in the PSW, for the ASCII-8 standard that never passed. Most implementations also provide unsigned BCD values with a sign nibble of 1111 (F).[39][40][41] ILE RPG uses 1111 (F) for positive and 1101 (D) for negative.[42] These match the EBCDIC zone for digits without a sign overpunch. In packed BCD, the number 127 is represented by 0001 0010 0111 1100 (127C) and −127 is represented by 0001 0010 0111 1101 (127D). Burroughs systems used 1101 (D) for negative, and any other value is considered a positive sign value (the processors will normalize a positive sign to 1100 (C)).
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
For example, a 4-byte (32-bit) 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 −
Like character strings, the first byte of the packed decimal – that 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 4-byte 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 (using about 20% more memory than
Packed BCD is supported in the COBOL programming language as the "COMPUTATIONAL-3" (an IBM extension adopted by many other compiler vendors) or "PACKED-DECIMAL" (part of the 1985 COBOL standard) data type. It is supported in PL/I as "FIXED DECIMAL". Beside the IBM System/360 and later compatible mainframes, packed BCD is implemented in the native instruction set of the original VAX processors from Digital Equipment Corporation and some models of the SDS Sigma series mainframes, and is the native format for the Burroughs Medium Systems line of mainframes (descended from the 1950s Electrodata 200 series).
As a result, this system allows for 32-bit packed BCD numbers to range from −50,000,000 to +49,999,999, and −1 is represented as 99999999. (As with two's complement binary numbers, the range is not symmetric about zero.)
Fixed-point packed decimal
Fixed-point decimal numbers are supported by some programming languages (such as COBOL and PL/I). These languages allow the programmer to specify 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 fixed-point value +1,234.567 when the implied decimal point is located between the fourth and fifth digits:
12 34 56 7C 12 34.56 7+
The decimal point is not actually stored in memory, as the packed BCD storage format does not provide for it. Its location is simply known to the compiler, and the generated code acts accordingly for the various arithmetic operations.
Higher-density encodings
If a decimal digit requires four bits, then three decimal digits require 12 bits. However, since 210 (1,024) is greater than 103 (1,000), if three decimal digits are encoded together, only 10 bits are needed. Two such encodings are Chen–Ho encoding and densely packed decimal (DPD). The latter has the advantage that subsets of the encoding encode two digits in the optimal seven bits and one digit in four bits, as in regular BCD.
Zoned decimal
Some implementations, for example 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
EBCDIC zoned decimal conversion table
BCD digit | Hexadecimal | EBCDIC character | ||||||
---|---|---|---|---|---|---|---|---|
0+ | C0 | A0 | E0 | F0 | { (*) | \ (*) | 0 | |
1+ | C1 | A1 | E1 | F1 | A | ~ (*) | 1 | |
2+ | C2 | A2 | E2 | F2 | B | s | S | 2 |
3+ | C3 | A3 | E3 | F3 | C | t | T | 3 |
4+ | C4 | A4 | E4 | F4 | D | u | U | 4 |
5+ | C5 | A5 | E5 | F5 | E | v | V | 5 |
6+ | C6 | A6 | E6 | F6 | F | w | W | 6 |
7+ | C7 | A7 | E7 | F7 | G | x | X | 7 |
8+ | C8 | A8 | E8 | F8 | H | y | Y | 8 |
9+ | C9 | A9 | E9 | F9 | I | z | Z | 9 |
0− | D0 | B0 | } (*) | ^ (*) | ||||
1− | D1 | B1 | J | |||||
2− | D2 | B2 | K | |||||
3− | D3 | B3 | L | |||||
4− | D4 | B4 | M | |||||
5− | D5 | B5 | N | |||||
6− | D6 | B6 | O | |||||
7− | D7 | B7 | P | |||||
8− | D8 | B8 | Q | |||||
9− | D9 | B9 | R |
(*) Note: These characters vary depending on the local character code page setting.
Fixed-point zoned decimal
Some languages (such as COBOL and PL/I) directly support fixed-point zoned decimal values, assigning an implicit decimal point at some location between the decimal digits of a number.
For example, given a six-byte 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
Operations with BCD
Addition
It is possible to perform addition 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 five-bit result of adding a pair of digits has a value greater than 9. The reason for adding 6 is that there are 16 possible 4-bit BCD values (since 24 = 16), but only 10 values are valid (0000 through 1001). For example:
1001 + 1000 = 10001 9 + 8 = 17
10001 is the binary, not decimal, representation of the desired result, but the most significant 1 (the "carry") cannot fit in a 4-bit binary number. In BCD as in decimal, there cannot exist a value greater than 9 (1001) per digit. To correct this, 6 (0110) is added to the total, and then the result is treated as two nibbles:
10001 + 0110 = 00010111 => 0001 0111 17 + 6 = 23 1 7
The two nibbles of the result, 0001 and 0111, 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 5-bit result of each digit-pair sum to 9. Some CPUs provide a half-carry flag to facilitate BCD arithmetic adjustments following binary addition and subtraction operations. The Intel 8080, the Zilog Z80 and the CPUs of the x86 family provide the opcode DAA (Decimal Adjust Accumulator).
Subtraction
Subtraction is done by adding the ten's complement of the
In signed BCD, 357 is 0000 0011 0101 0111. The ten's complement of 432 can be obtained by taking the
Now that both numbers are represented in signed BCD, they can be added together:
0000 0011 0101 0111 0 3 5 7 + 1001 0101 0110 1000 9 5 6 8 = 1001 1000 1011 1111 9 8 11 15
Since BCD is a form of decimal representation, several of the digit sums above are invalid. In the event that an invalid entry (any BCD digit greater than 1001) exists, 6 is added to generate a carry bit and cause the sum to become a valid entry. So, adding 6 to the invalid entries results in the following:
1001 1000 1011 1111 9 8 11 15 + 0000 0000 0110 0110 0 0 6 6 = 1001 1001 0010 0101 9 9 2 5
Thus the result of the subtraction is 1001 1001 0010 0101 (−925). To confirm the result, note that the first digit is 9, which means negative. This seems to be correct since 357 − 432 should result in a negative number. The remaining nibbles are BCD, so 1001 0010 0101 is 925. The ten's complement of 925 is 1000 − 925 = 75, so the calculated answer is −75.
If there are a different number of nibbles being added together (such as 1053 − 2), the number with the fewer digits must first be prefixed with zeros before taking the ten's complement or subtracting. So, with 1053 − 2, 2 would have to first be represented as 0002 in BCD, and the ten's complement of 0002 would have to be calculated.
BCD in computers
IBM
IBM used the terms
The IBM 1400 series are character-addressable machines, each location being six bits labeled B, A, 8, 4, 2 and 1, plus an odd parity check bit (C) and a word mark bit (M). For encoding digits 1 through 9, B and A are zero and the digit value represented by standard 4-bit BCD in bits 8 through 1. For most other characters bits B and A are derived simply from the "12", "11", and "0" "zone punches" in the punched card character code, and bits 8 through 1 from the 1 through 9 punches. A "12 zone" punch set both B and A, an "11 zone" set B, and a "0 zone" (a 0 punch combined with any others) set A. Thus the letter A, which is (12,1) in the punched card format, is encoded (B,A,1). The currency symbol $, (11,8,3) in the punched card, was encoded in memory as (B,8,2,1). This allows the circuitry to convert between the punched card format and the internal storage format to be very simple with only a few special cases. One important special case is digit 0, represented by a lone 0 punch in the card, and (8,2) in core memory.[43]
The memory of the IBM 1620 is organized into 6-bit addressable digits, the usual 8, 4, 2, 1 plus F, used as a flag bit and C, an odd parity check bit. BCD alphamerics are encoded using digit pairs, with the "zone" in the even-addressed digit and the "digit" in the odd-addressed digit, the "zone" being related to the 12, 11, and 0 "zone punches" as in the 1400 series. Input/output translation hardware converted between the internal digit pairs and the external standard 6-bit BCD codes.
In the decimal architecture
With the introduction of
On the
Today, BCD data is still heavily used in IBM databases such as IBM Db2 and processors such as z/Architecture and POWER6 and later Power ISA processors. In these products, the BCD is usually zoned BCD (as in EBCDIC or ASCII), packed BCD (two decimal digits per byte), or "pure" BCD encoding (one decimal digit stored as BCD in the low four bits of each byte). All of these are used within hardware registers and processing units, and in software.
Other computers
The Digital Equipment Corporation
The Intel x86 architecture supports a unique 18-digit (ten-byte) BCD format that can be loaded into and stored from the floating point registers, from where computations can be performed.[44]
The Motorola 68000 series had BCD instructions.[45]
In more recent computers such capabilities are almost always implemented in software rather than the CPU's instruction set, but BCD numeric data are still extremely common in commercial and financial applications.
There are tricks for implementing packed BCD and zoned decimal add–or–subtract operations using short but difficult to understand sequences of word-parallel logic and binary arithmetic operations.[46] For example, the following code (written in C) computes an unsigned 8-digit packed BCD addition using 32-bit binary operations:
uint32_t BCDadd(uint32_t a, uint32_t b)
{
uint32_t t1, t2; // unsigned 32-bit intermediate values
t1 = a + 0x06666666;
t2 = t1 ^ b; // sum without carry propagation
t1 = t1 + b; // provisional sum
t2 = t1 ^ t2; // all the binary carry bits
t2 = ~t2 & 0x11111110; // just the BCD carry bits
t2 = (t2 >> 2) | (t2 >> 3); // correction
return t1 - t2; // corrected BCD sum
}
BCD in electronics
This section has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
|
BCD is 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 employing 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 seven-segment displays to build a metering circuit, for example. If the numeric quantity were stored and manipulated as pure binary, interfacing with such a display would require complex circuitry. Therefore, in cases where the calculations are relatively simple, working throughout with BCD can lead to an overall simpler system than converting to and from binary. Most pocket calculators do all their calculations in BCD.
The same argument applies when hardware of this type uses an embedded microcontroller or other small processor. Often, representing numbers internally in BCD format results in smaller code, since a conversion from or to binary representation can be expensive on such limited processors. For these applications, some small processors feature dedicated arithmetic modes, which assist when writing routines that manipulate BCD quantities.[47][48]
Comparison with pure binary
Advantages
- Scaling by a power of 10 is simple.
- Rounding at a decimal digit boundary is simpler. Addition and subtraction in decimal do not require rounding.[dubious ]
- The 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 text-based format such as Binary numeral system § Conversion to and from other numeral systems).
- Many non-integral values, such as decimal 0.2, have an infinite place-value representation in binary (.001100110011...) but have a finite place-value in binary-coded decimal (0.0010). Consequently, a system based on binary-coded decimal representations of decimal fractions avoids errors representing and calculating such values. This is useful in financial calculations.
Disadvantages
- 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.[49]
- Some operations are more complex to implement. binary multiplication, requiring binary shifts and adds or the equivalent, per-digit or group of digits is required).
- Standard BCD requires four bits per digit, roughly 20 per cent more space than a binary encoding (the ratio of 4 bits to log210 bits is 1.204). When packed so that three digits are encoded in ten bits, the storage overhead is greatly reduced, at the expense of an encoding that is unaligned with the 8-bit byte boundaries common on existing hardware, resulting in slower implementations on these systems.
Representational variations
Various BCD implementations exist that employ other representations for numbers.
Signed variations
Signed decimal values may be represented in several ways. The COBOL programming language, for example, supports five zoned decimal formats, with each one encoding the numeric sign in a different way:
Type | Description | Example |
---|---|---|
Unsigned | No sign nibble | F1 F2 F3
|
Signed trailing (canonical format) | Sign nibble in the last (least significant) byte | F1 F2 C3
|
Signed leading (overpunch) | Sign nibble in the first (most significant) byte | C1 F2 F3
|
Signed trailing separate | Separate sign character byte ('+' or '−' ) following the digit bytes
|
F1 F2 F3 2B
|
Signed leading separate | Separate sign character byte ('+' or '−' ) preceding the digit bytes
|
2B F1 F2 F3
|
Telephony binary-coded decimal (TBCD)
Decimal digit |
TBCD 8 4 2 1 |
---|---|
* | 1 0 1 0 |
# | 1 0 1 1 |
a | 1 1 0 0 |
b | 1 1 0 1 |
c | 1 1 1 0 |
Used as filler when there is an odd number of digits | 1 1 1 1 |
The mentioned 3GPP document defines TBCD-STRING with swapped nibbles in each byte. Bits, octets and digits indexed from 1, bits from the right, digits and octets from the left.
bits 8765 of octet n encoding digit 2n
bits 4321 of octet n encoding digit 2(n – 1) + 1
Meaning number 1234
, would become 21 43
in TBCD.
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 binary-encoded integer and a binary-encoded 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
The
Application
The
The Atari 8-bit computers use a BCD format for floating point numbers. The MOS Technology 6502 processor has a BCD mode for the addition and subtraction instructions. The Psion Organiser 1 handheld computer's manufacturer-supplied software also uses BCD to implement floating point; later Psion models use binary exclusively.
Early models of the
Legal history
In the 1972 case
The decision noted that a patent "would wholly pre-empt the mathematical formula and in practical effect would be a patent on the algorithm itself".[55] This was a landmark judgement that determined the patentability of software and algorithms.
See also
- Bi-quinary coded decimal
- Binary-coded ternary(BCT)
- Binary integer decimal (BID)
- Bitmask
- Chen–Ho encoding
- Decimal computer
- Densely packed decimal (DPD)
- Double dabble, an algorithm for converting binary numbers to BCD
- Year 2000 problem
Notes
- ^ unused statesin 1024 states (10 bits for three digits).
- ^ a b c d e Code states (shown in black) outside the decimal range 0–9 indicate additional states of the non-BCD variant of the code. In the BCD code variant discussed here, they are pseudo-tetrades.
- ^ The Aiken code is one of several 2 4 2 1 codes. It is also known as 2* 4 2 1 code.
- ^ The Jump-at-8 code is also known as unsymmetrical 2 4 2 1 code.
- Petherick code is also known as Royal Aircraft Establishment(RAE) code.
- O'Brien code type I is also known as Wattscode or Watts reflected decimal (WRD) code.
- Stibitzcode.
- ^ IBM SQUOZE and DEC RADIX 50.
References
- ^ Intel. "ia32 architecture manual" (PDF). Intel. Archived (PDF) from the original on 2022-10-09. Retrieved 2015-07-01.
- ^ exists as well.)
- ^ ISBN 3-11011700-2. p. 25:
[…] Die nicht erlaubten 0/1-Muster nennt man auch Pseudodezimalen. […]
(320 pages) - ISBN 3-486-22662-2.
- ISBN 3-446-10569-7.
- LCCN 73-80607.
- ISBN 978-3642812415. 9783642812415. Retrieved 2015-08-05.
- ISBN 978-3642876639. 9783642876639, 978-3-642-87664-6. Retrieved 2015-08-05.
- ISBN 978-3642980886. 9783642980886. Retrieved 2015-08-05.
- LCCN 65-14624. 0978.
- Cowlishaw, Mike F. (2015) [1981, 2008]. "General Decimal Arithmetic". Retrieved 2016-01-02.
- odd parityof the resulting 5-bit code is also known as Ferranti code.)
- ISBN 978-0-470-07296-7.
- ^ 9s complement.)
- ^ Akademie-Verlag GmbH. p. 161. License no. 202-100/416/69. Order no. 4666 ES 20 K 3. (NB. A second edition 1973 exists as well.)
- ^ SBN 333-13360-9. Archived from the originalon 2020-07-16. Retrieved 2020-05-11. (270 pages) (NB. This is based on a translation of volume I of the two-volume German edition.)
- ^ ISBN 3-87145-272-6. (xii+327+3 pages) (NB. The German edition of volume I was published in 1969, 1971, two editions in 1972, and 1975. Volume II was published in 1970, 1972, 1973, and 1975.)
- ^ I.R.E. pp. 47–57 [49, 51–52, 57]. Archived from the original on 2020-07-03. Retrieved 2020-07-03. p. 52:.)
[…] The last column [of Table II], labeled "Best," gives the maximum fraction possible with any code—namely 0.60—half again better than any conventional code. This extremal is reached with the ten [heavily-marked vertices of the graph of Fig. 4 for n = 4, or, in fact, with any set of ten code combinations which include all eight with an even (or all eight with an odd) number of "1's." The second and third rows of Table II list the average and peak decimal change per undetected single binary error, and have been derived using the equations of Sec. II for Δ1 and δ1. The confusion index for decimals using the criterion of "decimal change," is taken to be cij = |i − j| i,j = 0, 1, … 9. Again, the "Best" arrangement possible (the same for average and peak), one of which is shown in Fig. 4, is substantially better than the conventional codes. […] Fig. 4 Minimum-confusion code for decimals. […] δ1=2 Δ1=15 […]
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] (11 pages) (NB. Besides the combinatorial set of 4-bit BCD "minimum-confusion codes for decimals", of which the author illustrates only one explicitly (here reproduced as code I) in form of a 4-bit graph, the author also shows a 16-state 4-bit "binary code for analog data" in form of a code table, which, however, is not discussed here. The code II shown here is a modification of code I discussed by Berger - ^ Éditions Dunod].)
- ^ a b Military Handbook: Encoders - Shaft Angle To Digital (PDF). United States Department of Defense. 1991-09-30. MIL-HDBK-231A. Archived (PDF) from the original on 2020-07-25. Retrieved 2020-07-25. (NB. Supersedes MIL-HDBK-231(AS) (1970-07-01).)
- ^ Runge, Wilhelm Tolmé (ed.). "Ermittlung des Codes und der logischen Schaltung einer Zähldekade". Telefunken-Zeitung (TZ) - Technisch-Wissenschaftliche Mitteilungen der Telefunken GMBH (in German). 33 (127). Berlin, Germany: Telefunken: 13–19. (7 pages)
- ^ ISBN 978-3-642-80561-5. (viii+252 pages) 1st edition
- S2CID 51674710. (3 pages)
- ^ "Different Types of Binary Codes". Electronic Hub. 2019-05-01 [2015-01-28]. Section 2.4 5211 Code. Archived from the original on 2020-05-18. Retrieved 2020-08-04.
- unit-distance code for full-circle rotatory slip ringapplications. Avoiding the all-zero code pattern allows for loop self-testing and to use the data lines for uninterrupted power distribution.)
- Bell Telephone Laboratories, Incorporated. U.S. patent 2,632,058. Serial No. 785697. Archived(PDF) from the original on 2020-08-05. Retrieved 2020-08-05. (13 pages)
- ISSN 0010-8049. (5 pages)
- ^ SBN 07036981-X. . ark:/13960/t72v3b312. Archived (PDF) from the original on 2021-02-19. Retrieved 2021-02-19. p. 517:(xxiv+835+1 pages) (NB. Ledley classified the described cyclic code as a cyclic decimal-coded binary code.)
[…] The cyclic code is advantageous mainly in the use of relay circuits, for then a sticky relay will not give a false state as it is delayed in going from one cyclic number to the next. There are many other cyclic codes that have this property. […]
[12] - ^ a b c d Savard, John J. G. (2018) [2006]. "Decimal Representations". quadibloc. Archived from the original on 2018-07-16. Retrieved 2018-07-16.
- ^ Petherick, Edward John (October 1953). A Cyclic Progressive Binary-coded-decimal System of Representing Numbers (Technical Note MS15). Farnborough, UK: Royal Aircraft Establishment (RAE). (4 pages) (NB. Sometimes referred to as A Cyclic-Coded Binary-Coded-Decimal System of Representing Numbers.)
- ^ Petherick, Edward John; Hopkins, A. J. (1958). Some Recently Developed Digital Devices for Encoding the Rotations of Shafts (Technical Note MS21). Farnborough, UK: Royal Aircraft Establishment (RAE).
- ^ S2CID 51657314. Paper 56-21. Retrieved 2020-05-18. (3 pages) (NB. This paper was prepared for presentation at the AIEE Winter General Meeting, New York, USA, 1956-01-30 to 1956-02-03.)
- ^ ISSN 0367-9950. Retrieved 2020-05-18. (1 page)
- ISSN 0367-9950. (2 pages)
- ^ Servomechanisms Laboraratory, Department of Electrical Engineering, MIT, for Special Summer Programs held in 1956 and 1957. The code Susskind actually presented in his work as "reading-type code" is shown as code type II here, whereas the type I code is a minor derivation with the two most significant bit columns swapped to better illustrate symmetries.)
- ^ from the original on 2020-08-08. Retrieved 2020-08-08.
- S2CID 206673385. (10 pages)
- LCCN 89-77320. (xviii+462 pages)
- ^ "Chapter 8: Decimal Instructions". IBM System/370 Principles of Operation. IBM. March 1980.
- ^ "Chapter 3: Data Representation". PDP-11 Architecture Handbook. Digital Equipment Corporation. 1983.
- ^ a b VAX-11 Architecture Handbook. Digital Equipment Corporation. 1985.
- ^ "ILE RPG Reference".
- ^ IBM BM 1401/1440/1460/1410/7010 Character Code Chart in BCD Order[permanent dead link]
- double-extended-precision floating-point format. All decimal integers are exactly representable in double extended-precision format. […] [13]
- ^ "The 68000's Instruction Set" (PDF). Archived (PDF) from the original on 2023-11-20. Retrieved 2023-11-21. (58 pages)
- ^ Jones, Douglas W. (2015-11-25) [1999]. "BCD Arithmetic, a tutorial". Arithmetic Tutorials. Iowa City, Iowa, USA: The University of Iowa, Department of Computer Science. Retrieved 2016-01-03.
- IEEE. Archived(PDF) from the original on 2010-01-05. Retrieved 2015-08-15.
- ^ "Decimal CORDIC Rotation based on Selection by Rounding: Algorithm and Architecture" (PDF). British Computer Society. Archived (PDF) from the original on 2022-10-09. Retrieved 2015-08-14.
- ISBN 978-0-07-460222-5.
- ^ 3GPP TS 29.002: Mobile Application Part (MAP) specification (Technical report). 2013. sec. 17.7.8 Common data types.
- ^ "Signalling Protocols and Switching (SPS) Guidelines for using Abstract Syntax Notation One (ASN.1) in telecommunication application protocols" (PDF). p. 15. Archived (PDF) from the original on 2013-12-04.
- ^ "XOM Mobile Application Part (XMAP) Specification" (PDF). p. 93. Archived from the original (PDF) on 2015-02-21. Retrieved 2013-06-27.
- ^ "Timer Counter Circuits in an IBM PC" (PDF). www.se.ecu.edu.au. Archived from the original (PDF) on 2008-10-10. Retrieved 2022-05-22. (7 pages)
- ^ MC6818 datasheet
- ^ Gottschalk v. Benson, 409 U.S. 63, 72 (1972).
Further reading
- Mackenzie, Charles E. (1980). Coded Character Sets, History and Development (PDF). The Systems Programming Series (1 ed.). (PDF) from the original on May 26, 2016. Retrieved August 25, 2019.
- Richards, Richard Kohler (1955). Arithmetic Operations in Digital Computers. New York, USA: van Nostrand. pp. 397–.
- misprintswith defective pages 115–146.)
- ISBN 0-8186-0805-6. Archived(PDF) from the original on 2017-07-04. Retrieved 2012-04-25. (Also: ACM SIGPLAN Notices, Vol. 22 #10, IEEE Computer Society Press #87CH2440-6, October 1987)
- "GNU Superoptimizer". HP-UX.
- Shirazi, Behrooz; Yun, David Y. Y.; Zhang, Chang N. (March 1988). VLSI designs for redundant binary-coded decimal addition. IEEE Seventh Annual International Phoenix Conference on Computers and Communications, 1988. IEEE. pp. 52–56.
- Brown; Vranesic (2003). Fundamentals of Digital Logic.
- Thapliyal, Himanshu; Arabnia, Hamid R. (November 2006). Modified Carry Look Ahead BCD Adder With CMOS and Reversible Logic Implementation. Proceedings of the 2006 International Conference on Computer Design (CDES'06). CSREA Press. pp. 64–69. ISBN 1-60132-009-4.
- Kaivani, A.; Alhosseini, A. Zaker; Gorgin, S.; Fazlali, M. (December 2006). Reversible Implementation of Densely-Packed-Decimal Converter to and from Binary-Coded-Decimal Format Using in IEEE-754R. 9th International Conference on Information Technology (ICIT'06). IEEE. pp. 273–276.
- Cowlishaw, Mike F. (2009) [2002, 2008]. "Bibliography of material on Decimal Arithmetic – by category". General Decimal Arithmetic. IBM. Retrieved 2016-01-02.
External links
- Cowlishaw, Mike F. (2014) [2000]. "A Summary of Chen-Ho Decimal Data encoding". General Decimal Arithmetic. IBM. Retrieved 2016-01-02.
- Cowlishaw, Mike F. (2007) [2000]. "A Summary of Densely Packed Decimal encoding". General Decimal Arithmetic. IBM. Retrieved 2016-01-02.
- Convert BCD to decimal, binary and hexadecimal and vice versa
- BCD for Java