BCD code

BCD code
Number of digits 4th
assessable Yes
Weight 3
Minimum distance 1
Maximum distance 4th
Hamming distance 1
redundancy 0.678
Code table
Decimal
digit
8-4-2-1-BCD-
coded
HEX
coded
0 0 0 0 0 0
1 0 0 0 1 1
2 0 0 1 0 2
3 0 0 1 1 3
4th 0 1 0 0 4th
5 0 1 0 1 5
6th 0 1 1 0 6th
7th 0 1 1 1 7th
8th 1 0 0 0 8th
9 1 0 0 1 9

BCD code is in English for Binary Coded Decimal , so binary coded decimal digit . Each decimal digit 0 to 9 is represented by four bits each (0000 to 1001, see code table), i.e. in a half byte ( nibble ). Another name is 8-4-2-1-BCD-Code . The digit sequence 8-4-2-1 stands for the values ​​of the digits in a binary-coded decimal digit. In some cases, BCD denotes the general binary coding of individual decimal digits (see below ). The BCD code should not be confused with the BCD counting code .

Coding

To a number as 8-4-2-1-BCD number is present, each decimal digit (0 to 9) by four bit shown (0000-1001, see code table), that is in a half byte ( nibble ). The remaining six values ​​(1010 2 to 1111 2 ), which can be represented with four bits, do not represent valid BCD numbers and are also referred to as pseudotetrads .

To code numbers with more than one decimal digit, the BCD representations of the individual digits are set one after the other (e.g. the number 2687 is shown as 0010 0110 1000 0111). One byte (8 bits) can be used to represent two decimal digits. If the 4 bits of a BCD number are encoded in the lower-order bits and the remaining 4 bits are padded with zeros, this is called an unpacked BCD number. If both halves of a byte are assigned a BCD number each, this is called a packed BCD number.

The number 10 is the smallest number that is coded differently in binary (0000 1010) and BCD (0001 0000). Since four bits correspond to a digit in hexadecimal notation, BCD-coded numbers can be read directly visually in hexadecimal notation. This also applies to multi-digit numbers.

The representation of signed numbers requires another separate bit if the coded area is to be the same for negative and positive numbers. This means that only 3 digits can be coded with 16 bits (−999 to +999), because no more 4 bits are available for the 4th digit. Coding using pseudotetrads is only possible if the display area is asymmetrical (e.g. −09.99 ° C to +99.99 ° C) or if the most significant digit can have a maximum value of 7 (e.g. −199, 9 ° C to +199.9 ° C).

application

BCD arithmetic is rarely used nowadays because it uses memory wastefully and offers few advantages in terms of accuracy compared to 32-bit floating point numbers . However, in finance and science there is still a need for accuracy that goes beyond floating point representation. Therefore one uses u in these areas. a. the Densely Packed Decimal Encoding , which on some mainframes is also supported by the hardware directly, but is available through software libraries on other computers, down to modern technical and scientific calculators. BCD coding is also used for clocks.

BCD arithmetic comes from times when you wanted to keep the effort of converting between internal representation and external representation low. The missing rounding errors in the representation of decimal numbers can also be achieved by using floating point numbers with a base 10 exponent or by using fixed point numbers with a base 10 exponent. The processing speed of this representation for today's processors is 2 to 2 1/2 orders of magnitude higher than that of the BCD representation. The number is not stored in the form M · 2 E , but in the form M · 10 E. · M is the (integer) mantissa and E is the exponent. Both are stored in binary form; for commercial problems, E is usually fixed anyway and is −2 (most bills) or −4 (telephone bills).

The BCD code is rarely used in arithmetic systems because circuits that calculate with BCD numbers require considerably more switching elements ( gates ) than circuits that work with binary numbers . However, the code is occasionally used in control systems, for example to control LCD or LED number displays, and to send SMS messages in so-called PDU format.

Today's processors either no longer have any support for BCD arithmetic or it has stopped at the level of 8-bit processors (i.e. 8-bit addition and 8-bit subtraction are supported) and is now almost unimaginably slow (up to 71 bars latency). But there were processors with extensive support for BCD arithmetic (z. B. Sharp SC61860 in the Sharp PC-1401 or the HP-Saturn processor family, which in calculators of HP 32 / 39 / 40 / 42 / 48 / 49 / 50 -Series was used until 2015). The processor of the entire HP 80 series also included extensive BCD support. All arithmetic calculations were done in BCD. The technology of the processor formed the basis for the Saturn pocket calculator CPUs. Intel x86 processors in 16-bit and 32-bit mode also have commands for calculations in BCD format, which can also be used with skill to carry out many other bit manipulations that are otherwise cumbersome to implement. In 64-bit mode, however, Intel processors no longer support a single command for BCD arithmetic. There is an instruction for converting 80-bit BCD into 80-bit double extended (FBLD [mem80]). The actual calculations are always carried out in the binary system.

The BCD code is also used to encode the time in the DCF77 signal. Inside, digital clocks run in BCD code. In contrast to binary counters, no further gates are necessary. The conversion of the meter reading using the LC display is much easier. Some AD converters deliver their measurement results directly as a BCD number.

Other BCD codes

In addition to the 8-4-2-1 coding described above, which is also known as “Natural Binary Coded Decimal” (NBCD), there is a whole range of other codings for BCD numbers. For example, the 1 out of 10 code , 2 out of 5 code, 5-4-2-1 code, and the 2-4-2-1 code (also Aiken code ), excess 3 code and the Gray code are common. These differ, as can partly be seen in the name, in the significance of the individual bits. The 1-out-of-10 code and the 2-out-of-5 code are primarily used for error analysis. With the help of the Chen-Ho coding or the densely packed decimal coding , 3 decimal digits can be coded by 10 bits instead of 12 bits. This offers a 20% better efficiency, because with 1024 possibilities there are only 24 unused code words.