Introduction
A binary code assigns a bit pattern to each symbol of a set (decimal digits, angles of a shaft encoder, and so on). Pure binary is efficient for arithmetic, but other codes are built for decimal I/O, for single-bit-change sequencing, or for self-complementing digit arithmetic. This note covers three widely used codes: BCD (8421), Gray code, and Excess-3.
Binary codes are not named as a separate item in the GATE CS syllabus; they sit under number representation because decimal and positional encodings appear inside representation and combinational-design questions.
Prerequisites
- Number Systems and Base Conversion: binary and decimal digits, weighted sums.
Core concepts
BCD (8421)
Binary-Coded Decimal encodes each decimal digit as its 4-bit binary value:
| Digit | BCD | Digit | BCD |
|---|---|---|---|
| 0 | 0000 | 5 | 0101 |
| 1 | 0001 | 6 | 0110 |
| 2 | 0010 | 7 | 0111 |
| 3 | 0011 | 8 | 1000 |
| 4 | 0100 | 9 | 1001 |
Patterns to are invalid in 8421 BCD. A multi-digit decimal number concatenates digit codes: .
Weighted. BCD is a weighted code with weights 8, 4, 2, 1. Other decimal codes (2421, 84-2-1) exist; unless a question names them, "BCD" means 8421.
Cost of BCD. An -digit decimal number takes bits in BCD but only bits in pure binary: three digits (0 to 999) need 12 BCD bits against 10 binary bits. Only 10 of the 16 nibble patterns are used.
Other weighted decimal codes
| Digit | 2421 | 84-2-1 |
|---|---|---|
| 0 | 0000 | 0000 |
| 1 | 0001 | 0111 |
| 2 | 0010 | 0110 |
| 3 | 0011 | 0101 |
| 4 | 0100 | 0100 |
| 5 | 1011 | 1011 |
| 6 | 1100 | 1010 |
| 7 | 1101 | 1001 |
| 8 | 1110 | 1000 |
| 9 | 1111 | 1111 |
In 84-2-1 the weights are : . In 2421 some digits have two weighted forms (5 could be or ); the table uses the standard choice, which makes the code self-complementing. Both columns are self-complementing: the bitwise complement of digit 's code is the code of .
ASCII
The 7-bit ASCII code assigns symbols. Three useful ASCII facts are: digit characters run from to , so the low nibble of a digit character is its BCD value; ; and . Upper and lower case differ only in bit 5 (value 32).
Excess-3 (XS-3)
Each decimal digit is encoded as the 4-bit binary of :
| Digit | XS-3 | Digit | XS-3 |
|---|---|---|---|
| 0 | 0011 | 5 | 1000 |
| 1 | 0100 | 6 | 1001 |
| 2 | 0101 | 7 | 1010 |
| 3 | 0110 | 8 | 1011 |
| 4 | 0111 | 9 | 1100 |
Self-complementing. The 9's complement of a digit is the bitwise complement of its XS-3 code. Example: digit 2 is ; digit 7 () is . That property simplifies decimal subtraction hardware.
Invalid XS-3 patterns include .
Gray code
A reflected Gray code of bits lists all patterns so that successive codes (including the wrap from last to first) differ in exactly one bit. The standard binary-reflected construction:
- 1-bit: .
- To get bits from , prefix 0 to the original -bit list, then prefix 1 to that list in reverse order.
| Decimal | Binary | Gray |
|---|---|---|
| 0 | 000 | 000 |
| 1 | 001 | 001 |
| 2 | 010 | 011 |
| 3 | 011 | 010 |
| 4 | 100 | 110 |
| 5 | 101 | 111 |
| 6 | 110 | 101 |
| 7 | 111 | 100 |
Conversion formulas (standard binary-reflected Gray):
- Binary to Gray : , and for . Equivalently, as integers, .
- Gray to binary: , and for .
Bit-by-bit example. Gray to binary: ; ; ; . So Gray is binary . Equivalently, each binary bit is the XOR (parity) of all Gray bits at or to its left.

How it works
BCD addition (sketch)
Add BCD digits as binary. If the 4-bit sum exceeds 9, or a carry out of the nibble occurs, add correction (six) and propagate the carry to the next digit. Full BCD adder design is a combinational exercise; the correction rule is what exams test.
Why six? A nibble holds 16 values but a decimal digit only 10. Adding 6 skips the six unused codes, so the nibble wraps exactly when the decimal digit should carry.
Correction as a Boolean function. With binary digit sum ( the carry out of the 4-bit adder), the decimal carry and correction signal is
The terms and are 1 exactly for the sums 10 to 15; covers 16 to 19 (the largest digit sum is ).
Example. in BCD:
- Low digits: . Add : . Digit 5, carry 1.
- High digits: . Add : . Digit 2, carry 1.
- Result .
Example: carry without a large nibble. : . The nibble looks legal, but , so add six: , carry 1. Result .
Why Gray code
In a shaft encoder, adjacent Gray positions differ in one bit, reducing ambiguous transitions caused by skew when several binary bits would change (for example, binary 7 to 8 changes all four bits). Karnaugh-map axes use Gray order for a different reason: adjacent cells then differ in exactly one Boolean variable, permitting that variable to be eliminated when cells are grouped. See Karnaugh Maps.
Algorithms and pseudocode
BIN-TO-GRAY(b) // non-negative integer
1 return b XOR floor(b / 2)
GRAY-TO-BIN(g)
1 b <- 0
2 while g != 0
3 b <- b XOR g
4 g <- floor(g / 2)
5 return b
DIGIT-TO-XS3(d) // 0 <= d <= 9
1 return binary4(d + 3)
DIGIT-TO-BCD(d)
1 return binary4(d) // illegal if d > 9Costs for an -bit word, counting loop iterations:
- BIN-TO-GRAY: one shift and one XOR in every case. As hardware it is XOR gates side by side, depth 1.
- GRAY-TO-BIN: the loop runs once per bit of , up to its leading 1. Best case 0 iterations (); worst case (top bit set); average over all words, so about . As hardware it is a chain of XOR gates, depth .
- DIGIT-TO-XS3 and DIGIT-TO-BCD: constant time in every case (one 4-bit addition or a table lookup).
Mathematical explanation
Unit-distance property. A code is a Gray code when the Hamming distance between codes of consecutive integers (mod ) is 1. The reflected construction yields a Hamiltonian path on the -dimensional hypercube that also closes into a cycle.
Self-complementing codes. For a weighted decimal code with weights , complementing every bit of the code of gives a pattern of weight . So is necessary for a weighted code to be self-complementing; it is sufficient only when the codeword choice is consistent, as in the standard 2421 and 84-2-1 tables (8421 has and is not self-complementing). Excess-3 is self-complementing even though it is unweighted: the code of is , its complement is , which is the code of .
Gray parity. The XOR of all bits of the Gray code of equals , the LSB of . So Gray codes of even numbers have even parity and those of odd numbers have odd parity, which is a useful parity consistency check, not a proof of unit distance. For the latter, if incrementing binary flips its trailing ones and the preceding zero, then . Hence , exactly one set bit. The reflected construction also establishes the cyclic wrap-around.
Bit changes per cycle. Counting once round all values and back to 0, a Gray counter changes exactly one bit per step, changes in total. A binary counter changes bit times, changes in total: for , 8 changes in Gray against 14 in binary. Fewer simultaneous changes means fewer glitches and lower switching power.
Worked examples
Example 1: encode 59
- BCD: .
- XS-3: digits , , so .
- Pure binary of fifty-nine: (different; do not confuse).
Example 2: Gray round-trip
For : . Applying GRAY-TO-BIN recovers 13. Checking all confirms a bijection.
Example 3: adjacent Gray distance
Gray codes of 4 and 5 are and (for 4-bit): XOR , Hamming weight 1. Codes of 15 and 0: and , weight 1 (wrap-around).
Example 4: XS-3 nine's complement
Digit 4 is ; complement is digit 5, and . Digit 0 is ; complement is digit 9.
Example 5: 4-bit Gray code of a number
The 4-bit Gray code of 10 () is . Going back, gives , , , , that is 10.
Example 6: identifying a code from one table
A table encodes 3 as and 6 as . In BCD 3 is ; in XS-3 it is , and XS-3 of 6 is . The two codes are complements, as XS-3 self-complementation predicts for digits that sum to 9.
Important properties and observations
- BCD uses bits for decimal digits, versus bits to pack the full range through as an integer. One digit needs 4 bits in either representation. BCD leaves 6 of each nibble's 16 patterns unused but simplifies digit-wise I/O.
- Packed BCD stores two digits per byte; unpacked BCD stores one digit per byte (often in the low nibble).
- Gray code is not a weighted positional number system; do not treat a Gray pattern as a binary integer when computing magnitude.
- K-map axes use Gray order; an axis with variables has -bit labels, including 1-bit axes.
Common misconceptions.
- "BCD of 12 is ." That is binary twelve; BCD of twelve as two digits is .
- "Gray code equals binary shifted." The formula is XOR with the right shift, not a plain shift.
- "Excess-3 of 0 is 0000." It is .
- "Any weighted code whose weights sum to 9 is self-complementing." The sum is necessary, not sufficient; the codeword choice matters.
- "BCD addition needs correction only when the nibble exceeds 9." Also when the 4-bit addition itself carries out (sums 16 to 19).
GATE examination perspective
- CODE IDENTIFICATION. Given a table, decide whether it is BCD, XS-3, Gray, or pure binary.
- INVALID PATTERN. Spot illegal BCD or XS-3 nibbles.
- GRAY CONVERSION. Apply or the bit-wise recurrence both ways.
- SELF-COMPLEMENT. Use XS-3 bitwise complement as nine's complement of a digit.
- BCD CORRECTION. After binary addition of digits, add 6 when the sum exceeds 9.
- BCD CARRY CASES. Sums 16 to 19 carry out of the nibble and leave a legal-looking nibble; they still need the .
- WEIGHTED CODES. Decode a 2421 or 84-2-1 codeword with its weights, including negative weights.
- ASCII ARITHMETIC. Character minus gives the digit; case conversion toggles bit 5.
Summary
Sources
- M. Morris Mano, M. D. Ciletti, Digital Design, 5th ed., Pearson, 2013: Chapter 1, Section 1.7 (binary codes, including BCD and Gray).