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.

The value 27 written four ways: binary 0011011 with weights 64 to 1, Gray 0010110, BCD 0010 0111 with one nibble per decimal digit 2 and 7, and Excess-3 0101 1010 with each digit plus 3
The value 27 in four codes. BCD and Excess-3 code each decimal digit on its own; binary and Gray code the whole number.

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

digitBCD 8421Excess-3digitBCD 8421Excess-3
000000011501011000
100010100601101001
200100101701111010
300110110810001011
401000111910011100

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.

Two columns for the values 0 to 7: 3-bit binary on the left with the changed bits of each step shaded, up to three per step at 3 to 4; 3-bit Gray on the right, 000 001 011 010 110 111 101 100, with exactly one shaded bit per step
Counting 0 to 7: binary changes up to 3 bits per step (3 → 4), Gray always exactly one.

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.

Block diagram of the BCD adder for 7 + 5: A 0111 and B 0101 enter adder 1, which gives K Z3 Z2 Z1 Z0 = 0 1100; the detector computes C = K + Z3 Z2 + Z3 Z1 = 1; the correction 0110 and 1100 enter adder 2, giving 0010; the decimal carry C = 1 and the digit 0010 read 12
7 + 5: the first adder gives 1100 (12), which is not a digit; C = 1, so 0110 is added and the digit is 0010 with carry 1: 12.
A + BK Z3Z2Z1Z0Ccorrectiondigit Sresult
4 + 30 01110000001117
7 + 50 110010110001012
9 + 91 001010110100018

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.

digitabcdefgdigitabcdefg
0111111051011011
1011000061011111
2110110171110000
3111100181111111
4011001191111011

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

Open the Computer Architecture mind mapComputerArchitectureAbstraction and numbers: layers and bit patternsAbstraction and numbersCombinational logic: output depends only on inputsCombinational logicSequential logic: state held by a clockSequential logicMIPS software: how C becomes machine wordsMIPS softwareThe processor: datapath plus controlThe processorPipelines and parallelism: more instructions at oncePipelines and parallelismMemory and I/O: small fast near, big slow farMemory and I/OLogic gatesLogic gatestruth table row picks the outputAdders, muxes, ALUAdders, muxes, ALUcritical path sets the delayMux, decoder, encoderMux, decoder, encoderSelect bits pick one input; decoder one lineBubble pushingBubble pushingDe Morgan: AND-OR becomes NAND-NANDAlgebraic factoringAlgebraic factoringShare kernels: 17 → 9 literalsTechnology mappingTechnology mappingCheapest pattern per node, reused upwardFast arithmeticFast arithmeticlookahead carry beats ripple carryBCD and Gray CodesBCD adder: sum > 9 or carry → add 6Multiply/divide hardwareMultiply/divide hardwareadd or subtract, then shift, once per bitVerilog to hardwareVerilog to hardwareassign gives gates, always_ff gives flopsVerilog testbenchVerilog testbenchGolden model + check task say PASS/FAILASIC design flowASIC design flowRTL→netlist→DEF→GDSII; slack shrinksPlace and RoutePlace and RouteAnneal by HPWL, then Lee wave routesOpen the Computer Architecture mind mapComputer Architectureoutput depends only on inputsCombinational logicLogic gatesLogic gatestruth table row picks the outputAdders, muxes, ALUAdders, muxes, ALUcritical path sets the delayMux, decoder, encoderMux, decoder, encoderSelect bits pick one input; decoder one lineBubble pushingBubble pushingDe Morgan: AND-OR becomes NAND-NANDAlgebraic factoringAlgebraic factoringShare kernels: 17 → 9 literalsTechnology mappingTechnology mappingCheapest pattern per node, reused upwardFast arithmeticFast arithmeticlookahead carry beats ripple carryBCD and Gray CodesBCD adder: sum > 9 or carry → add 6Multiply/divide hardwareMultiply/divide hardwareadd or subtract, then shift, once per bitVerilog to hardwareVerilog to hardwareassign gives gates, always_ff gives flopsVerilog testbenchVerilog testbenchGolden model + check task say PASS/FAILASIC design flowASIC design flowRTL→netlist→DEF→GDSII; slack shrinksPlace and RoutePlace and RouteAnneal by HPWL, then Lee wave routes
This page on the Computer Architecture mind map · 76 pages in 7 branches · open it to see the whole map and test yourself in recall mode.