Gray Code, Excess-3 and 2421 Code Converters
Convert between binary, Gray, excess-3 and 2421 codes. Learn the XOR rules, a 74283 excess-3 converter, self complementing codes and 9's complement.
Why more than one code
The same number can be written in different binary codes, and each code is good at something different. Plain binary is best for arithmetic. BCD suits decimal displays. Gray code suits position sensors. Excess-3 and 2421 make decimal subtraction easier. A code converter is a small circuit that turns one code into another.
Gray code
In Gray code, two consecutive numbers differ in exactly one bit. In ordinary binary, going from 7 to 8 changes four bits at once (0111 to 1000). If the bits of a position sensor do not change at exactly the same moment, a wrong value can be read in between. Gray code cannot have this problem.
A 4 bit Gray code sequence is: 0000, 0001, 0011, 0010, 0110, 0111, 0101, 0100, 1100, 1101, 1111, 1110, 1010, 1011, 1001, 1000.
Converting binary and Gray code
Let the binary bits be B3 B2 B1 B0, with B3 the most significant, and the Gray bits G3 G2 G1 G0.
G3 = B3 G2 = B3 ⊕ B2 G1 = B2 ⊕ B1 G0 = B1 ⊕ B0 B3 = G3 B2 = B3 ⊕ G2 B1 = B2 ⊕ G1 B0 = B1 ⊕ G0
Binary to Gray needs three XOR gates, one between each pair of neighbouring bits. Gray to binary also needs three XOR gates, but each one uses the result of the previous stage, so it is a chain. Example: binary 0110 (6) gives G3 = 0, G2 = 0 ⊕ 1 = 1, G1 = 1 ⊕ 1 = 0, G0 = 1 ⊕ 0 = 1, so Gray is 0101. Going back: B3 = 0, B2 = 0 ⊕ 1 = 1, B1 = 1 ⊕ 0 = 1, B0 = 1 ⊕ 1 = 0, which is 0110 again.
Excess-3 code
Excess-3 is BCD with 3 added to every digit. The digit 0 is 0011, the digit 4 is 0111 and the digit 9 is 1100. The codes 0000 to 0010 and 1101 to 1111 are unused.
With a 74283 the BCD to excess-3 converter is just an adder: connect the BCD digit to A1 to A4, tie B to 0011 and C0 to 0. The sum is the excess-3 code. To go back, add 1101 (which is −3 in 4 bit two's complement) and ignore the carry. For excess-3 0110 (the digit 3), 0110 + 1101 = 1 0011, and the lower four bits 0011 are BCD 3.
Self complementing codes and 9's complement
Excess-3 has a useful feature. The 9's complement of a digit d is 9 − d. In excess-3 you get it by inverting all four bits. Check: the digit 2 is 0101. Inverting gives 1010, which is the digit 7, and 9 − 2 = 7. A code with this property is called self complementing.
The 2421 code is also self complementing. Its bits have weights 2, 4, 2 and 1. The digits 0 to 4 are 0000, 0001, 0010, 0011 and 0100. The digits 5 to 9 are 1011, 1100, 1101, 1110 and 1111. The digit 3 is 0011, and its inverse 1100 is the digit 6, and 9 − 3 = 6.
| Digit | BCD | Excess-3 | 2421 | Digit (9's complement) |
|---|---|---|---|---|
| 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 |
The 9's complement is used for decimal subtraction in the way the 1's complement is used in binary. Self complementing codes make that circuit very simple, just four inverters.
Designing any converter
For a converter that is not just an adder, write a truth table with the input code on the left and each output bit on the right. Mark the unused input codes as don't cares. Draw one Karnaugh map for each output bit and write the simplest equation. Then build it with gates, or with a decoder and a few NAND gates. Always test every valid input on the bench and write down the result.
Try it in the virtual lab
SinBench has free 3D benches and interactive demos for this topic. Open one to try the ideas above with real instruments, no sign up needed.
Excess-3, 2421 and 9's Complement Code Converters
Questions and answers
How do you convert binary to Gray code?
Keep the top bit as it is. Each other Gray bit is the XOR of that binary bit and the binary bit above it.
Why is Gray code used?
Only one bit changes between neighbouring values, so a reading taken while bits are changing can never be wrong by more than one step. It is used in position encoders.
How do you get excess-3 from BCD?
Add 3 (0011) to the BCD digit. A 74283 adder can do this with B tied to 0011.
What is a self complementing code?
It is a code where inverting all the bits gives the 9's complement of the digit. Excess-3 and 2421 are two examples.
Related topics
BCD to Seven Segment Decoder: The 7447 and Common Anode Displays
Learn how a 7447 drives a common anode seven segment display: BCD input, active low outputs, segment patterns, resistors, the lamp test and ripple blanking.
Digital Logic DesignKarnaugh Maps: Simplify Boolean Functions Step by Step
A step by step guide to Karnaugh maps: how to fill a 4 variable map, group cells, use don't cares and write the simplest sum of products, with a worked example.
Digital Logic DesignHalf Adder, Full Adder and the 74283 4-Bit Adder
Learn how half and full adders add binary numbers, how to build a ripple carry adder, how the 74283 works with its carry in and out, with worked sums.