Decimal Codes: BCD, Excess-3 and 2421
Store decimal digits in four bits each and correct a binary sum of BCD digits with +6. Complement a digit by inverting its bits.
You Will Be Able To
- Write a number in BCD and in packed BCD, and spot a pattern that is not a digit.
- Add two BCD digits in binary and apply the +6 correction when the sum is above 9.
- Encode a digit in excess-3 and in 2421, and find its nines' complement by inverting bits.
- Lesson
- G1.6
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
A shop scale adds two prices, 38 and 25, stored as decimal digits. It uses a plain binary adder. The display shows a 5 and a blank: the low four bits are 1101, which is not a decimal digit.
Weights show bit magnitudes. For signed values, the leftmost bit has a negative weight.
- Hexadecimal
- 0x5D
- What the adder gave. Hex 5d. Read one nibble per digit: the high nibble 0101 is the digit 5. The low nibble 1101 is 13, and no decimal digit has that pattern.
- The right answer. 38 + 25 is 63. In BCD that is 0110 0011: the digit 6, then the digit 3.
The display reads one decimal digit per nibble. Click the bits: any nibble above 1001 is not a digit.
One Digit in Four Bits
Binary-coded decimal (BCD) stores each decimal digit in its own four bits. The bit weights are 8, 4, 2 and 1, so it is also called 8421 code.
Four bits have 16 patterns and a digit needs only 10. The six patterns 1010 to 1111 are never used. Packed BCD puts two digits in one byte, so a byte holds 00 to 99.
BCD costs extra bits, but each digit can go straight to a display or a printer. Clocks, meters and calculators use it for that reason.
Adding BCD Needs a Correction
A binary adder can add two BCD digits. The sum is right up to 9. Above 9 it is a binary number, not a decimal digit and a carry.
The fix is to add 6. Six skips the six unused patterns, so the four low bits become the right digit and the carry becomes the decimal carry.
| Digits | Binary sum | Above 9? | Corrected | Decimal result |
|---|---|---|---|---|
| 3 + 4 | 00111 | no | 00111 | 7 |
| 5 + 4 | 01001 | no | 01001 | 9 |
| 8 + 5 | 01101 | yes: +6 | 10011 | 13 |
| 9 + 9 | 10010 | yes: +6 | 11000 | 18 |
Measured on the simulator. 9 + 9 already carries out of four bits, and still needs the +6.
- I/O
- Datapath
- Data
- Control
- 3 + 4
The binary sum is 00111, which is 7. It is not above 9, so it goes straight out as the digit 7.
- 8 + 5
The binary sum is 01101, which is 13. It is above 9, so 6 is added: 10011, a carry of 1 and the digit 3.
Read the diagram as text
- Digit a: 0 to 9
- Digit b: 0 to 9
- Binary adder: sum 0 to 18
- Above 9?: not a digit
- Add 6: skip 1010 to 1111
- Digit and carry: decimal result
- Digit a → Binary adder: four bits
- Digit b → Binary adder: four bits
- Binary adder → Above 9?: five-bit sum
- Above 9? → Digit and carry: no: already a digit
- Above 9? → Add 6: yes
- Add 6 → Digit and carry: low four bits and carry
For a multi-digit number, the carry joins the next digit, as in decimal by hand. Lesson G10.3 builds this adder as a circuit.
Codes That Complement Themselves
Decimal subtraction uses the nines' complement: 9 - d for each digit. In a self-complementing code, inverting the four bits of d gives the code of 9 - d. Inverters are cheap, so this saves an adder.
Two common codes have this property. Excess-3 stores d + 3. 2421 (Aiken code) weights its bits 2, 4, 2 and 1. It stores 0 to 4 as in BCD, and 5 to 9 as d + 6 (the self-complementing choice).
| Digit | 8421 (BCD) | Excess-3 | 2421 | Pairs with |
|---|---|---|---|---|
| 0 | 0000 | 0011 | 0000 | 9 |
| 1 | 0001 | 0100 | 0001 | 8 |
| 2 | 0010 | 0101 | 0010 | 7 |
| 3 | 0011 | 0110 | 0011 | 6 |
| 4 | 0100 | 0111 | 0100 | 5 |
| 5 | 0101 | 1000 | 1011 | 4 |
| 6 | 0110 | 1001 | 1100 | 3 |
| 7 | 0111 | 1010 | 1101 | 2 |
| 8 | 1000 | 1011 | 1110 | 1 |
| 9 | 1001 | 1100 | 1111 | 0 |
Measured on the simulator. Green marks 2 and 7: invert either code of 2 and you get the code of 7.
Excess-3 has one more trick: no digit is 0000, so an all-zero wire reads as a fault. Its unused patterns are 0000 to 0010 and 1101 to 1111.
Asked in interviews
Why does a BCD adder add 6?
A 4-bit binary sum above 9 is not a decimal digit. Adding 6 skips the six unused patterns. The low bits become the right digit and the carry becomes the decimal carry.
What is a self-complementing code?
One where inverting the bits of a digit gives the code of 9 minus the digit. Excess-3 and 2421 are examples; plain BCD is not.
When would you use BCD?
When numbers go to a decimal display or must round exactly like decimal arithmetic, as in clocks, meters and money.
Try It
Each row is one digit in three codes. BCD is plain binary. Excess-3 adds 3. 2421 keeps 0 to 4 and adds 6 to the digits 5 to 9.
The loop inverts each code of d and compares it with the code of 9 - d. Neither code fails on any digit.
Add a third check for BCD itself: if (~4'(d) !== 4'(9 - d)) bad++; with its own counter. It fails on every digit.
digit 8421 xs-3 2421 0 0000 0011 0000 1 0001 0100 0001 2 0010 0101 0010 3 0011 0110 0011 4 0100 0111 0100 5 0101 1000 1011 6 0110 1001 1100 7 0111 1010 1101 8 1000 1011 1110 9 1001 1100 1111 self-complement failures: excess-3 0, 2421 0Check Yourself
Common Mistakes
Wrong Add packed BCD bytes with a plain binary adder.
A digit sum above 9 lands on one of the six patterns that are not digits. 38 + 25 gives 5d.
Fix Add one digit at a time and add 6 to any digit sum above 9.
total = a + b;raw = a[3:0] + b[3:0];
low = (raw > 9) ? raw + 5'd6 : raw;Wrong Correct only when a digit sum carries out of four bits.
Sums from 10 to 15 fit four bits with no carry, but they are not digits.
Fix Correct whenever the binary sum is above 9.
Wrong Read a BCD byte as a binary number.
The byte 0100_0111 is 47 in BCD and 71 in binary.
Fix Read each nibble as one decimal digit.
Wrong Expect inverting a BCD digit to give its nines' complement.
Inverting 0010 (2) gives 1101, which is not a digit. Plain BCD is not self-complementing.
Fix Use excess-3 or 2421, or compute 9 - d with an adder.
Remember
- BCD stores each decimal digit in four bits; six patterns are never used.
- Packed BCD puts two digits in a byte, from 00 to 99.
- A binary sum of two BCD digits above 9 needs +6; the carry out is the decimal carry.
- Excess-3 (d + 3) and 2421 are self-complementing: inverting the bits gives 9 - d.