Binary-coded decimal ==================== might use LEDs to express binary values. In this clock, each column of LEDs shows a binary-coded decimal numeral of the traditional sexagesimal time. In computing and electronic systems, binary-coded decimal (BCD) is a class of binary encodings of decimal numbers where each digit is represented by a fixed number of bits, usually four or eight. Sometimes, special bit patterns are used for a sign or other indications (e.g. error or overflow). In byte-oriented systems (i.e. most modern computers), the term unpacked BCD 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 tetrades (the nibble typically needed to hold them is also known as a tetrade) while the unused, don't care-states are named pseudo-tetrad(e)s , pseudo-decimals, or pseudo-decimal digits. BCD's main virtue, in comparison to binary positional systems, is its more accurate representation and rounding of decimal quantities, as well as its ease of conversion into conventional human-readable representations.… might use LEDs to express binary values. In this clock, each column of LEDs shows a binary-coded decimal numeral of the traditional sexagesimal time. In computing and electronic systems, binary-coded decimal (BCD) is a class of binary encodings of decimal numbers where each digit is represented by a fixed number of bits, usually four or eight. Sometimes, special bit patterns are used for a sign or other indications (e.g. error or overflow). In byte-oriented systems (i.e. most modern computers), the term unpacked BCD 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 tetrades (the nibble typically needed to hold them is also known as a tetrade) while the unused, don't care-states are named pseudo-tetrad(e)s , pseudo-decimals, or pseudo-decimal digits. BCD's main virtue, in comparison to binary positional systems, is its more accurate representation and rounding of decimal quantities, as well as its ease of conversion into conventional human-readable representations. Its principal drawbacks are a slight increase in the complexity of the circuits needed to implement basic arithmetic as well as slightly less dense storage. BCD was used in many early decimal computers, and is implemented in the instruction set of machines such as the IBM System/360 series and its descendants, Digital Equipment Corporation's VAX, the Burroughs B1700, and the Motorola 68000-series processors. BCD per se is not as widely used as in the past, and is unavailable or limited in newer instruction sets (e.g., ARM; x86 in long mode). However, decimal fixed-point and decimal floating-point formats are still important and continue to be used in financial, commercial, and industrial computing, where the subtle conversion and fractional rounding errors that are inherent in binary floating point formats cannot be tolerated. == 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. This scheme can also be referred to as Simple Binary-Coded Decimal (SBCD) or BCD 8421, and is the most common encoding. Others include the so-called "4221" and "7421" encoding – named after the weighting used for the bits – and "excess-3". For example, the BCD digit 6, in 8421 notation, is in 4221 (two encodings are possible), in 7421, while in excess-3 it is ( ). 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. 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). 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 big-endian format, a program would encode as follows: 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== Some computers whose words are multiples of an octet (8-bit byte), for example contemporary IBM mainframe systems, support packed BCD (or packed decimal ) numeric representations, in which each nibble represents either a decimal digit or a sign. Packed BCD has been in use since at least the 1960s and is implemented in all IBM mainframe hardware since then. Most implementations are big endian, i.e. with the more significant digit in the upper half of each byte, and with the leftmost byte (residing at the lowest memory address) containing the most significant digits of the packed decimal value. The lower nibble of the rightmost byte is usually used as the sign flag, although some unsigned representations lack a sign flag. 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). ILE RPG uses 1111 (F) for positive and 1101 (D) for negative. 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)). No matter how many bytes wide a word is, there is always an even number of nibbles because each byte has two of them. Therefore, a word of n bytes can contain up to decimal digits, which is always an odd number of digits. A decimal number with d digits requires bytes of storage space. 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 binary notation to store the same numbers), conversion to ASCII, EBCDIC, or the various encodings of Unicode is made trivial, as no arithmetic operations are required. The extra storage requirements are usually offset by the need for the accuracy and compatibility with calculator or hand calculation that fixed-point decimal arithmetic provides. Denser packings of BCD exist which avoid the storage penalty and also need no arithmetic operations for common conversions. 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). Ten's complement representations for negative numbers offer an alternative approach to encoding the sign of packed (and other) BCD numbers. In this case, positive numbers always have a most significant digit between 0 and 4 (inclusive), while negative numbers are represented by the 10's complement of the corresponding positive number. As a result, this system allows for 32-bit packed BCD numbers to range from to , 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 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. == Zoned decimal == Some implementations, for example IBM mainframe systems, support zoned decimal numeric representations. Each decimal digit is stored in one 8-bit 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, or to values representing plus or minus. EBCDIC systems use a zone value of ( ), yielding – , the codes for "0" through "9", a zone value of ( ) for positive, yielding – , the codes for "{" through "I" and a zone value of ( ) for negative, yielding – , the codes for the characters "}" through "R". 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 | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  |- style="text-align:center; font-family: monospace; font-size: larger" ||1− ||D1 ||B1 | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  ||J | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  |- style="text-align:center; font-family: monospace; font-size: larger" ||2− ||D2 ||B2 | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  ||K | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  |- style="text-align:center; font-family: monospace; font-size: larger" ||3− ||D3 ||B3 | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  ||L | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  |- style="text-align:center; font-family: monospace; font-size: larger" ||4− ||D4 ||B4 | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  ||M | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  |- style="text-align:center; font-family: monospace; font-size: larger" ||5− ||D5 ||B5 | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  ||N | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  |- style="text-align:center; font-family: monospace; font-size: larger" ||6− ||D6 ||B6 | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  ||O | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  |- style="text-align:center; font-family: monospace; font-size: larger" ||7− ||D7 ||B7 | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  ||P | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  |- style="text-align:center; font-family: monospace; font-size: larger" ||8− ||D8 ||B8 | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  ||Q | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  |- style="text-align:center; font-family: monospace; font-size: larger" ||9− ||D9 ||B9 | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  ||R | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  | style="background:#f0f0f0;"|  |} (*) Note: These characters vary depending on the local character code page setting. 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 2 4 = 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 on 8080 and Z80 / Decimal Adjust for Addition on x86). 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 0 3 5 7 + 1001 0101 0110 1000 9 5 6 8 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. 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 } === Telephony binary-coded decimal (TBCD) === 3GPP developed TBCD, an expansion to BCD where the remaining (unused) bit combinations are used to add specific telephony symbols, similar to those in telephone keypad design. 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. This format is used in modern mobile telephony to send dialed numbers, as well as operator ID (the MCC/MNC tuple), IMEI, IMSI (SUPI), et.c. REFERENCES ---------- BIBLIOGRAPHY (BibTeX) -------------------- @article{ref0, title = {Binary-coded decimal}, url = {https://encyclopegia.org/a/binary-coded-decimal} } BIBLIOGRAPHY (RIS) ------------------- TY - JOUR TI - Binary-coded decimal UR - https://encyclopegia.org/a/binary-coded-decimal ER - CITE THIS ARTICLE ---------------- Cite this article: Binary-coded decimal Derived from: https://en.wikipedia.org/w/index.php?oldid=1361818166 Licence: CC BY-SA 4.0 Snapshot: enwiki-001361818166 Permalink: /a/binary-coded-decimal