Types of binary codes, with a converter
A binary code is a rule for writing numbers or characters as bits. For decimal digits the common ones are 8421 BCD (each digit in plain 4-bit binary), the weighted codes 2421 and 84-2-1, excess-3 (digit plus 3) and Gray code (one bit changes per step); letters and symbols use alphanumeric codes such as ASCII. Type a number below to see it in each code, next to the table of all ten digits and each code's properties, all computed by the page.
Classification of binary codes
| Group | Codes | What defines them |
|---|---|---|
| Weighted | 8421 BCD, 2421, 84-2-1 | Each bit position has a weight; a digit is the sum of the weights of its 1 bits. |
| Non-weighted | Excess-3, Gray | No position weights: excess-3 adds 3 to each digit, Gray changes one bit per step. |
| Self-complementing | 2421, 84-2-1, excess-3 | Inverting every bit of a digit gives its 9's complement. |
| Unit distance | Gray | Consecutive values differ in exactly one bit. |
| Alphanumeric | ASCII, Unicode | Letters, digits and symbols: '7' is 0110111 in ASCII. |
| Error detecting and correcting | Parity, Hamming | Extra bits that reveal, or repair, flipped bits. |
Weighted codes, checked
In a weighted code the value of a word is the sum of the weights of its 1 bits. In 8421, 0111 is 4 + 2 + 1 = 7. In 2421, 1011 is 2 + 2 + 1 = 5; the digits 0 to 4 keep the first bit at 0, which is what makes the code self-complementing. In 84-2-1 two weights are negative: 0111 is 4 - 2 - 1 = 1. The converter's tables are tested against these weights for every digit. 8421 is the BCD that most chips and the BCD converter use, and the live BCD to excess-3 converter, BCD to Gray converter and BCD to 7-segment decoder show the gate circuits that translate between them.
Why decimal codes waste bits
Four bits have 16 patterns but a decimal digit needs only 10, so every 4-bit decimal code leaves 6 patterns unused (in 8421, 1010 to 1111). A number in BCD therefore takes more bits than in plain binary: 47 is 0100 0111 in BCD (8 bits) but 101111 in binary (6 bits). Decimal codes are still used where numbers are shown or entered as digits, such as clocks, calculators and displays, because each digit can be decoded on its own. For other bases see the base converter, and for Gray code of any number, the Gray code converter.
Frequently asked
What are the types of binary codes?
The usual classification is: weighted codes, where each bit has a fixed value (8421 BCD, 2421, 84-2-1); non-weighted codes (excess-3, Gray); alphanumeric codes for letters and symbols (ASCII, Unicode); and error-detecting and correcting codes (parity, Hamming).
What is the difference between weighted and non-weighted codes?
In a weighted code each bit position has a weight and a digit is the sum of the weights of its 1 bits: in 2421, 1011 is 2 + 2 + 1 = 5. A non-weighted code has no such weights: excess-3 is the digit plus 3, and Gray is chosen so that neighbouring values differ in one bit.
What is a self-complementing code?
A code where the word for 9 - d is the bitwise complement of the word for d, so inverting all bits gives the 9's complement. 2421, 84-2-1 and excess-3 are self-complementing; 8421 BCD is not (1's complement of 0000 is 1111, which is not a BCD digit).
What is the 2421 code?
A weighted decimal code with bit weights 2, 4, 2 and 1. The digits 0 to 4 use the leading weight-2 bit as 0 and 5 to 9 use it as 1 (5 = 1011, 9 = 1111), which makes the code self-complementing. It is also called the Aiken code.
What is excess-3 code?
Each decimal digit is written as its binary value plus 3: 0 is 0011 and 9 is 1100. It is non-weighted and self-complementing, which once made decimal subtraction simpler in hardware.
Why is Gray code called a unit distance code?
Because consecutive values differ in exactly one bit, so a sensor moving from one value to the next can never be read as a value far away. That is why it is used in rotary encoders and between clock domains.
Error-detecting codes have their own tools: the Hamming code calculator and the parity generator and checker. Character codes: the ASCII table and text to binary.
Open the lab →