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

Core concepts

BCD (8421)

Binary-Coded Decimal encodes each decimal digit 0..90..9 as its 4-bit binary value:

DigitBCDDigitBCD
0000050101
1000160110
2001070111
3001181000
4010091001

Patterns 10101010 to 11111111 are invalid in 8421 BCD. A multi-digit decimal number concatenates digit codes: 5910=0101 1001BCD59_{10} = 0101\,1001_{\text{BCD}}.

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 nn-digit decimal number takes 4n4n bits in BCD but only ⌈nlg⁡10⌉\lceil n \lg 10 \rceil 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

Digit242184-2-1
000000000
100010111
200100110
300110101
401000100
510111011
611001010
711011001
811101000
911111111

In 84-2-1 the weights are 8,4,−2,−18, 4, -2, -1: 0111=4−2−1=10111 = 4 - 2 - 1 = 1. In 2421 some digits have two weighted forms (5 could be 01010101 or 10111011); the table uses the standard choice, which makes the code self-complementing. Both columns are self-complementing: the bitwise complement of digit dd's code is the code of 9−d9 - d.

ASCII

The 7-bit ASCII code assigns 27=1282^7 = 128 symbols. Three useful ASCII facts are: digit characters run from 0=48=3016\texttt{0} = 48 = 30_{16} to 9=57=3916\texttt{9} = 57 = 39_{16}, so the low nibble of a digit character is its BCD value; A=65=4116\texttt{A} = 65 = 41_{16}; and a=97=6116\texttt{a} = 97 = 61_{16}. Upper and lower case differ only in bit 5 (value 32).

Excess-3 (XS-3)

Each decimal digit dd is encoded as the 4-bit binary of d+3d+3:

DigitXS-3DigitXS-3
0001151000
1010061001
2010171010
3011081011
4011191100

Self-complementing. The 9's complement of a digit is the bitwise complement of its XS-3 code. Example: digit 2 is 01010101; digit 7 (9−29-2) is 1010=0101‾1010 = \overline{0101}. That property simplifies decimal subtraction hardware.

Invalid XS-3 patterns include 0000,0001,0010,1101,1110,11110000, 0001, 0010, 1101, 1110, 1111.

Gray code

A reflected Gray code of nn bits lists all 2n2^n patterns so that successive codes (including the wrap from last to first) differ in exactly one bit. The standard binary-reflected construction:

  • 1-bit: 0,10, 1.
  • To get nn bits from n−1n-1, prefix 0 to the original (n−1)(n-1)-bit list, then prefix 1 to that list in reverse order.
DecimalBinaryGray
0000000
1001001
2010011
3011010
4100110
5101111
6110101
7111100

Conversion formulas (standard binary-reflected Gray):

  • Binary bn−1…b0b_{n-1}\ldots b_0 to Gray gn−1…g0g_{n-1}\ldots g_0: gn−1=bn−1g_{n-1} = b_{n-1}, and gi=bi+1⊕big_i = b_{i+1}\oplus b_i for i=n−2..0i = n-2..0. Equivalently, as integers, gray=b⊕⌊b/2⌋\textit{gray} = b \oplus \lfloor b/2 \rfloor.
  • Gray to binary: bn−1=gn−1b_{n-1} = g_{n-1}, and bi=bi+1⊕gib_i = b_{i+1}\oplus g_i for i=n−2..0i = n-2..0.

Bit-by-bit example. Gray 10111011 to binary: b3=1b_3 = 1; b2=1⊕0=1b_2 = 1 \oplus 0 = 1; b1=1⊕1=0b_1 = 1 \oplus 1 = 0; b0=0⊕1=1b_0 = 0 \oplus 1 = 1. So Gray 10111011 is binary 1101=131101 = 13. Equivalently, each binary bit is the XOR (parity) of all Gray bits at or to its left.

Reflected Gray code construction for 3 bits
Figure 1. Building the 3-bit reflected Gray code from the 2-bit list: prefix 0 to the original list, then prefix 1 to the reversed list. Adjacent codes, including 100 and 000, differ by one bit.

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 01100110 (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 K Z8Z4Z2Z1K\,Z_8 Z_4 Z_2 Z_1 (KK the carry out of the 4-bit adder), the decimal carry and correction signal is

C=K+Z8Z4+Z8Z2.C = K + Z_8 Z_4 + Z_8 Z_2 .

The terms Z8Z4Z_8 Z_4 and Z8Z2Z_8 Z_2 are 1 exactly for the sums 10 to 15; KK covers 16 to 19 (the largest digit sum is 9+9+1=199 + 9 + 1 = 19).

Example. 58+6758 + 67 in BCD:

  • Low digits: 1000+0111=1111=15>91000 + 0111 = 1111 = 15 > 9. Add 01100110: 1 01011\,0101. Digit 5, carry 1.
  • High digits: 0101+0110+1=1100=12>90101 + 0110 + 1 = 1100 = 12 > 9. Add 01100110: 1 00101\,0010. Digit 2, carry 1.
  • Result 0001 0010 0101BCD=1250001\,0010\,0101_{\text{BCD}} = 125.

Example: carry without a large nibble. 9+9=189 + 9 = 18: 1001+1001=1 00101001 + 1001 = 1\,0010. The nibble 00100010 looks legal, but K=1K = 1, so add six: 0010+0110=10000010 + 0110 = 1000, carry 1. Result 0001 1000=180001\,1000 = 18.

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

Text
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 > 9

Costs for an nn-bit word, counting loop iterations:

  • BIN-TO-GRAY: one shift and one XOR in every case. As hardware it is n−1n - 1 XOR gates side by side, depth 1.
  • GRAY-TO-BIN: the loop runs once per bit of gg, up to its leading 1. Best case 0 iterations (g=0g = 0); worst case nn (top bit set); average n−1+2−nn - 1 + 2^{-n} over all 2n2^n words, so about n−1n - 1. As hardware it is a chain of n−1n - 1 XOR gates, depth n−1n - 1.
  • 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 2n2^n) is 1. The reflected construction yields a Hamiltonian path on the nn-dimensional hypercube that also closes into a cycle.

Self-complementing codes. For a weighted decimal code with weights w3w2w1w0w_3 w_2 w_1 w_0, complementing every bit of the code of dd gives a pattern of weight ∑wi−d\sum w_i - d. So ∑wi=9\sum w_i = 9 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 ∑wi=15\sum w_i = 15 and is not self-complementing). Excess-3 is self-complementing even though it is unweighted: the code of dd is d+3d + 3, its complement is 15−(d+3)=(9−d)+315 - (d + 3) = (9 - d) + 3, which is the code of 9−d9 - d.

Gray parity. The XOR of all bits of the Gray code of bb equals b0b_0, the LSB of bb. 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 bb flips its tt trailing ones and the preceding zero, then d=bXOR(b+1)=2t+1−1d=b\mathbin{\mathrm{XOR}}(b+1)=2^{t+1}-1. Hence g(b)XORg(b+1)=dXOR(d≫1)=2tg(b)\mathbin{\mathrm{XOR}}g(b+1)=d\mathbin{\mathrm{XOR}}(d\gg1)=2^t, exactly one set bit. The reflected construction also establishes the cyclic wrap-around.

Bit changes per cycle. Counting once round all 2n2^n values and back to 0, a Gray counter changes exactly one bit per step, 2n2^n changes in total. A binary counter changes bit ii 2n−i2^{n-i} times, 2n+1−22^{n+1} - 2 changes in total: for n=3n = 3, 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: 0101 10010101\,1001.
  • XS-3: digits 5→10005\to 1000, 9→11009\to 1100, so 1000 11001000\,1100.
  • Pure binary of fifty-nine: 1110112111011_2 (different; do not confuse).

Example 2: Gray round-trip

For b=13=11012b = 13 = 1101_2: gray=1101⊕0110=1011\textit{gray} = 1101 \oplus 0110 = 1011. Applying GRAY-TO-BIN recovers 13. Checking all 0..150..15 confirms a bijection.

Example 3: adjacent Gray distance

Gray codes of 4 and 5 are 01100110 and 01110111 (for 4-bit): XOR 00010001, Hamming weight 1. Codes of 15 and 0: 10001000 and 00000000, weight 1 (wrap-around).

Example 4: XS-3 nine's complement

Digit 4 is 01110111; complement 10001000 is digit 5, and 4+5=94+5=9. Digit 0 is 00110011; complement 11001100 is digit 9.

Example 5: 4-bit Gray code of a number

The 4-bit Gray code of 10 (101021010_2) is 1010⊕0101=11111010 \oplus 0101 = 1111. Going back, 11111111 gives b3=1b_3 = 1, b2=0b_2 = 0, b1=1b_1 = 1, b0=0b_0 = 0, that is 10.

Example 6: identifying a code from one table

A table encodes 3 as 01100110 and 6 as 10011001. In BCD 3 is 00110011; in XS-3 it is 01100110, and XS-3 of 6 is 10011001. The two codes are complements, as XS-3 self-complementation predicts for digits that sum to 9.

Important properties and observations

  • BCD uses 4n4n bits for nn decimal digits, versus ⌈nlg⁡10⌉\lceil n\lg10\rceil bits to pack the full range 00 through 10n−110^n-1 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 kk variables has kk-bit labels, including 1-bit axes.

Common misconceptions.

  • "BCD of 12 is 11001100." That is binary twelve; BCD of twelve as two digits is 0001 00100001\,0010.
  • "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 00110011.
  • "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 b⊕⌊b/2⌋b\oplus\lfloor b/2\rfloor 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 +6+6.
  • WEIGHTED CODES. Decode a 2421 or 84-2-1 codeword with its weights, including negative weights.
  • ASCII ARITHMETIC. Character minus 0\texttt{0} gives the digit; case conversion toggles bit 5.

Summary

Sources

  1. M. Morris Mano, M. D. Ciletti, Digital Design, 5th ed., Pearson, 2013: Chapter 1, Section 1.7 (binary codes, including BCD and Gray).

Next steps