The idea: one value, several bit patterns
A code is a rule that gives each value a bit pattern. Plain binary is one rule, but not the only one. The same number 27 is 0011011 in binary, 0010110 in Gray code, 0010 0111 in BCD and 0101 1010 in Excess-3. Each code is good at one job: binary for arithmetic, BCD for decimal digits and displays, Gray for sensors that must never read a wrong value mid-change.
On the canvas the value's four codes are top left and a 7-segment display is top right. The BCD adder is below them. Pick a Value to see its codes; the bits that changed from the last value turn orange. To Gray and From Gray convert bit by bit. BCD Add adds the digits A and B. For how decimal becomes binary in the first place, see Number Bases and Two's Complement.
BCD: four bits per decimal digit
Binary-coded decimal (BCD, also called 8421 code) writes each decimal digit in its own 4 bits, with weights 8, 4, 2, 1. So 27 is 0010 (2) then 0111 (7). Four bits can hold 16 patterns, but a digit needs only 10, so 1010 to 1111 are never used. BCD therefore needs more bits than binary: 99 takes 8 bits in BCD (1001 1001) but only 7 in binary (1100011).
BCD is worth the waste when numbers are shown or kept as decimal digits: clocks, calculators, meters, and money, where 0.10 must stay exactly 0.10. Each digit can go straight to its own display digit with no division by 10 (Demo: 27 in four codes).
| digit | BCD 8421 | Excess-3 | digit | BCD 8421 | Excess-3 |
|---|---|---|---|---|---|
| 0 | 0000 | 0011 | 5 | 0101 | 1000 |
| 1 | 0001 | 0100 | 6 | 0110 | 1001 |
| 2 | 0010 | 0101 | 7 | 0111 | 1010 |
| 3 | 0011 | 0110 | 8 | 1000 | 1011 |
| 4 | 0100 | 0111 | 9 | 1001 | 1100 |
Excess-3: BCD plus 3
Excess-3 codes each digit d as the 4-bit binary of d + 3. It is not weighted: you cannot read it with fixed place values. Its trick is that it is self-complementing. Invert all four bits and you get the code of 9 − d: 2 = 0101, inverted 1010 = 7. Old decimal machines used this to subtract with the 9's complement using only NOT gates. Small steps can still flip many bits: 9 → 10 changes 0011 1100 into 0100 0011, seven bits (Demo: count 9 → 10).
Gray code: one bit per step
Counting in binary, 7 → 8 is 0000111 → 0001000: four bits change at once. A real sensor never switches them at exactly the same moment, so for an instant it may read 0001111 = 15 or 0000000 = 0. Gray code (reflected binary code) orders the patterns so that each step changes exactly one bit: 7 = 0000100 and 8 = 0001100 (Demo: count 7 → 8). A reading taken mid-change is then either the old value or the new one, never a far-off one. Rotary encoders, Karnaugh map rows and columns, and the pointers of FIFOs that cross clock domains all use it.
Binary to Gray: keep the top bit, then each Gray bit is the XOR of two neighbouring binary bits: gi = bi+1 XOR bi. In one line, g = b XOR (b >> 1). A Gray bit is 1 exactly where two binary neighbours differ.
Gray to binary: go from the top bit down. Copy g6, then bi = bi+1 XOR gi: each bit needs the binary bit just found above it, so this direction is a chain, not one parallel step. For 45: binary 0101101 → Gray 0111011 → binary 0101101 again (Demo: binary to Gray and back).
Gray code is not for arithmetic: adding two Gray numbers bit by bit gives nonsense, so circuits convert to binary first.
The BCD adder: add, check, add 6
To add two BCD digits, a circuit first uses an ordinary 4-bit binary adder. The 5-bit sum K Z3Z2Z1Z0 runs from 0 to 18 (9 + 9). It is a correct BCD digit only for 0 to 9. It is wrong in two cases:
- The sum is 10 to 15: the pattern is one of the six unused codes. That happens when Z3Z2 = 1 (12–15) or Z3Z1 = 1 (10, 11, 14, 15).
- The sum is 16 to 18: the adder carried, K = 1, and Z3..Z0 is 0000 to 0010, which looks like a small digit.
So the decimal carry is C = K + Z3·Z2 + Z3·Z1. When C = 1, a second 4-bit adder adds 0110 (6) to Z3..Z0. Adding 6 jumps over the six unused codes, so the result wraps into the next decimal digit. The second adder's own carry is dropped; C is the carry to the next digit.
| A + B | K Z3Z2Z1Z0 | C | correction | digit S | result |
|---|---|---|---|---|---|
| 4 + 3 | 0 0111 | 0 | 0000 | 0111 | 7 |
| 7 + 5 | 0 1100 | 1 | 0110 | 0010 | 12 |
| 9 + 9 | 1 0010 | 1 | 0110 | 1000 | 18 |
The 9 + 9 case shows why K is in the formula: Z3..Z0 = 0010 looks like a fine digit, but the true sum is 18, so 6 must still be added (Demo: BCD add 9 + 9). For how the 4-bit adders themselves work and how to make them fast, see Arithmetic Circuits.
More digits: chain the carry
A multi-digit BCD adder is one digit adder per decimal digit, with each C wired into the carry-in of the next digit's first adder. For 27 + 45: units 7 + 5 = 12 gives digit 2 and C = 1; tens 2 + 4 + 1 = 7 gives digit 7 and C = 0; the answer is 72. The page shows a single digit with carry-in 0.
From a BCD digit to a 7-segment display
A 7-segment display has seven bars named a to g: a on top, b and c on the right, d at the bottom, e and f on the left, g in the middle. A BCD-to-7-segment decoder takes the 4 bits of one digit and drives seven outputs, one per bar. Each output is its own Boolean function of the 4 inputs, and the six unused codes are don't cares, which makes the functions small (see Karnaugh Maps). For decoders in general, see Multiplexers, Decoders and Encoders.
| digit | abcdefg | digit | abcdefg |
|---|---|---|---|
| 0 | 1111110 | 5 | 1011011 |
| 1 | 0110000 | 6 | 1011111 |
| 2 | 1101101 | 7 | 1110000 |
| 3 | 1111001 | 8 | 1111111 |
| 4 | 0110011 | 9 | 1111011 |
Codes need not only carry values; some add check bits to catch errors. See Hamming Code for that kind of code.
See also Asynchronous FIFO: Gray Pointers Across Two Clock Domains, where Gray-coded pointers let a FIFO pass data safely between two clocks.
What the page leaves out
Other decimal codes (2421, 5421, biquinary, the ASCII digits 0x30–0x39), packed versus unpacked BCD, BCD subtraction with the 9's or 10's complement, the carry-in of a digit adder and multi-digit adders on the canvas, n-bit Gray codes beyond 7 bits and their reflected construction step by step, the gate-level circuit of the 7-segment decoder, common-anode versus common-cathode displays, and the odd glyphs that real decoder chips show for the unused codes.
On the mind map
References
M. Morris Mano and Michael D. Ciletti, Digital Design, 5th ed., sections 1.7 (Binary Codes) and 4.6 (Decimal Adder)
F. Gray, "Pulse code communication", US patent 2,632,058, 1953