8421, Aiken, 5421, excess-3, Gray excess-3 and 2-out-of-5, in a PLC and in BCD addition
Type in any box: the decimal, the packed BCD in hexadecimal or the bits of any code (with or without spaces between digits).
The digits are added in binary from right to left; if a sum is greater than 9, 6 (0110) is added to skip the six unused combinations, and 1 is carried.
In BCD (binary-coded decimal) each decimal digit is encoded separately with 4 bits. So \(2026 = 0010\;0000\;0010\;0110_{BCD}\), whereas in natural binary it would be \(111\,1110\,1010_2\). It uses more bits than binary (4 bits could count 16 values and only 10 are used), but it is converted to decimal digit by digit, without divisions. That is why it is used in 7-segment displays, real-time clocks, thumbwheel switches, the counters of older machines and accounting systems, where 0.10 € has to be exactly 0.10 €.
There are many ways of assigning 4 bits to each digit. These are the most common:
| Digit | 8421 (natural) | Aiken 2421 | 5421 | 8 4 −2 −1 | Excess-3 | Gray excess-3 | 2-out-of-5 |
|---|---|---|---|---|---|---|---|
| 0 | 0000 | 0000 | 0000 | 0000 | 0011 | 0010 | 11000 |
| 1 | 0001 | 0001 | 0001 | 0111 | 0100 | 0110 | 00011 |
| 2 | 0010 | 0010 | 0010 | 0110 | 0101 | 0111 | 00101 |
| 3 | 0011 | 0011 | 0011 | 0101 | 0110 | 0101 | 00110 |
| 4 | 0100 | 0100 | 0100 | 0100 | 0111 | 0100 | 01001 |
| 5 | 0101 | 1011 | 1000 | 1011 | 1000 | 1100 | 01010 |
| 6 | 0110 | 1100 | 1001 | 1010 | 1001 | 1101 | 01100 |
| 7 | 0111 | 1101 | 1010 | 1001 | 1010 | 1111 | 10001 |
| 8 | 1000 | 1110 | 1011 | 1000 | 1011 | 1110 | 10010 |
| 9 | 1001 | 1111 | 1100 | 1111 | 1100 | 1010 | 10100 |
In a weighted code, each bit is worth a fixed amount and the digit is the sum of the weights of the bits set to 1. In 8421 the weights are the binary ones (\(0110 = 4 + 2 = 6\)); in Aiken, 2-4-2-1 (\(1100 = 2 + 4 = 6\)); in 5421, 5-4-2-1 (\(1001 = 5 + 1 = 6\)), and in 8 4 −2 −1 there are negative weights (\(1010 = 8 - 2 = 6\)). When a digit can be written in two ways, the code picks one: in Aiken, 6 could be 0110 or 1100 and 1100 is used.
5421 has a curious property: the most significant bit separates digits 0–4 from 5–9, like counting on an abacus or biquinary counting.
A code is self-complementing when inverting the bits of a digit gives its nines’ complement (9 − digit). Aiken is one (\(2 = 0010 \to 1101 = 7\)), as are 8 4 −2 −1 and excess-3. With them, subtraction is done like addition, replacing the subtrahend with its complement, which is why the first decimal calculators and computers used them. 8421 is not: \(2 = 0010 \to 1101\), which is not even a valid digit.
It is a BCD where 3 is added to each digit: 0 is written 0011 and 9 is 1100. As well as being self-complementing, no digit is 0000, which helps to tell a zero from a dead line, and when two digits are added the carry appears exactly when the sum goes above 9. It is not weighted.
It is the Gray code of values 3 to 12. Only one bit changes from each digit to the next, and also from 9 to 0, so it is a cyclic unit-distance decimal code: it was used in position encoders with decimal wheels.
Each digit has 5 bits and exactly two are always 1 (there are 10 ways of choosing 2 bits out of 5, one per digit). Its weights are 7-4-2-1-0, except for 0, which is written 11000. It is an error-detecting code: if one bit changes, there are one or three ones left, and the receiver knows the digit is wrong. It is used in the Interleaved 2 of 5 and Code 2 of 5 barcodes and was used in telephone exchanges. See error detection and correction.
The typical mistake is reading a BCD as if it were an integer: the word 16#2026 is 8230 in binary, not 2026. If a value read from a device “jumps” from 9 to 16 or from 99 to 256, it is almost certainly BCD.
When two BCD digits are added with a 4-bit binary adder, the result is correct as long as it does not exceed 9. If it does, the adder keeps counting in base 16 and the six combinations that do not exist in BCD (1010 to 1111) have to be skipped by adding 6:
\(1000 + 0101 = 1101\) (13), which is not a BCD digit. Add 6: \(1101 + 0110 = 1\,0011\). The digit is 0011 (3) and 1 is carried: the result is 13.
The correction is applied when the binary sum is greater than 9 or when the adder has already produced a carry (for example, \(9 + 9 = 18 = 1\,0010\)). Many microprocessors have an instruction for it (DAA, decimal adjust) and a half-carry flag for each digit.
Related: binary and hexadecimal converter, adders and subtractors and BCD to 7-segment decoder.