EN

BCD codes

8421, Aiken, 5421, excess-3, Gray excess-3 and 2-out-of-5, in a PLC and in BCD addition

Number systems: Binary and hexadecimal Gray code BCD codes Floating and fixed point Parity, Hamming and CRC

Decimal to BCD converter

Type in any box: the decimal, the packed BCD in hexadecimal or the bits of any code (with or without spaces between digits).

Number
Decimal codes

In a PLC’s memory

BCD addition step by step

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.

Addends

What BCD is

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 €.

Types of BCD codes

There are many ways of assigning 4 bits to each digit. These are the most common:

Digit8421 (natural)Aiken 242154218 4 −2 −1Excess-3Gray excess-32-out-of-5
000000000000000000011001011000
100010001000101110100011000011
200100010001001100101011100101
300110011001101010110010100110
401000100010001000111010001001
501011011100010111000110001010
601101100100110101001110101100
701111101101010011010111110001
810001110101110001011111010010
910011111110011111100101010100

Weighted codes

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.

Self-complementing codes

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.

Excess-3

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.

Gray excess-3

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.

2-out-of-5

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.

Packed BCD, unpacked BCD and ASCII

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.

BCD in PLCs and electronics

Why BCD addition needs the +6 correction

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:

Example: 8 + 5

\(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.