Lesson 3 +35 XP

Binary Codes: BCD, Excess-3, Gray, ASCII, Parity & Hamming Code

Binary Codes: BCD, Excess-3, Gray, ASCII, Parity & Hamming Code

A binary code is a rule that assigns a bit pattern to each item of some set: decimal digits, letters, shaft positions or error-check bits. A code with n bits can name at most 2n items, so 10 decimal digits need at least 4 bits (23 = 8 < 10 ≤ 16 = 24). Converting a number to binary is not the same as coding it: 13 in binary is 1101, but in BCD it is 0001 0011.

1. Classification of Codes

ClassMeaningExamples
Weightedeach bit position has a fixed weight; the digit is the sum of the weights of the 1 bits8421 (BCD), 2421, 5421, 84-2-1
Non-weightedno positional weightsExcess-3, Gray
Self-complementingthe 9's complement of a digit is obtained by inverting its code bitsExcess-3, 2421, 84-2-1
Reflected / unit-distancesuccessive codes differ in exactly one bitGray
Alphanumericletters, digits and symbolsASCII, EBCDIC, Unicode
Error-detecting / correctingextra bits reveal or fix errorsparity, Hamming

2. Decimal Codes

DecimalBCD 84212421Excess-384-2-1
00000000000110000
10001000101000111
20010001001010110
30011001101100101
40100010001110100
50101101110001011
60110110010011010
70111110110101001
81000111010111000
91001111111001111

BCD (8421 code)

Binary Coded Decimal writes each decimal digit as its own 4-bit binary number. The six patterns 1010 to 1111 are invalid (unused) in BCD.

(396)10 = 0011 1001 0110_{BCD} qquad but qquad (396)10 = (110001100)2
Advantages of BCDDisadvantages of BCD
Easy conversion to and from decimal (displays, keypads, calculators)Needs more bits: 12 bits for 396 instead of 9
No rounding of decimal fractions (used in money arithmetic)Arithmetic needs correction (below)

BCD addition

Add two BCD digits in binary. If the 4-bit sum is greater than 9 or produces a carry, add 6 (0110) to skip the six invalid codes; the correction produces the decimal carry.

SolvedExample 1 · BCD addition

Add 184 + 576 in BCD.

  1. Units: 0100 + 0110 = 1010 (10 > 9), add 0110: 1 0000. Write 0000, carry 1.
  2. Tens: 1000 + 0111 + 1 = 10000 (carry out), add 0110: 1 0110. Write 0110, carry 1.
  3. Hundreds: 0001 + 0101 + 1 = 0111 (no correction).
  4. Result: 0111 0110 0000.
Answer0111 0110 0000_{BCD} = 760

The hardware that does this, the BCD adder, appears in the adders lesson.

Excess-3 code

Excess-3 (XS-3) is BCD plus 3: digit d is coded as the binary value of d + 3. For example (395)10 = 0110 1100 1000_{XS-3}.

It is self-complementing: inverting the bits of a digit gives the code of its 9's complement. The code for 2 is 0101; inverted it is 1010, which is the code for 7, and 9 - 2 = 7. This made decimal subtraction by 9's complement easy in early machines.

2421 and 84-2-1 codes

Both are weighted and self-complementing. In 2421, digit 7 is 1101 = 2 + 4 + 0 + 1. In 84-2-1 a weight can be negative: 0111 is 0 + 4 - 2 - 1 = 1.

3. Gray Code

In the Gray code (reflected binary code) consecutive numbers differ in exactly one bit. When a value changes from 7 to 8 in plain binary, all four bits flip (0111 to 1000); if they do not flip at exactly the same moment, the circuit may briefly read a wrong value such as 1111. In Gray code only one bit changes, so no false intermediate value can appear.

Uses: shaft encoders (rotary position sensors), K-map row and column labels, asynchronous FIFO pointers, and error minimization in analog-to-digital conversion.

Building the Gray code by reflection, and conversion rulesBuilding the Gray code by reflection, and conversion rules

Binary to Gray

The MSB stays the same; each other Gray bit is the XOR of the binary bit and the binary bit to its left:

gn-1 = bn-1, qquad gi = bi+1 oplus bi

Gray to binary

The MSB stays the same; each other binary bit is the XOR of the previous binary bit (just found) and the current Gray bit:

bn-1 = gn-1, qquad bi = bi+1 oplus gi
SolvedExample 2 · Gray conversions

Convert binary 1011 to Gray, and Gray 1101 to binary.

  1. Binary to Gray: g3 = 1, g2 = 1 oplus 0 = 1, g1 = 0 oplus 1 = 1, g0 = 1 oplus 1 = 0, so Gray = 1110.
  2. Gray to binary: b3 = 1, b2 = 1 oplus 1 = 0, b1 = 0 oplus 0 = 0, b0 = 0 oplus 1 = 1, so binary = 1001.
Answer10112 → 1110_{Gray} and 1101_{Gray} → 10012

Gray conversion in Verilog

Binary to Gray is one line, b ^ (b >> 1). Gray to binary is a chain, because each bit needs the binary bit found before it. The testbench runs through all 16 values and converts each one back.

VERILOG
// 4-bit binary to Gray and Gray to binary converters
module bin2gray (
    input  [3:0] b,
    output [3:0] g
);
    assign g = b ^ (b >> 1);
endmodule

module gray2bin (
    input  [3:0] g,
    output [3:0] b
);
    assign b[3] = g[3];
    assign b[2] = b[3] ^ g[2];
    assign b[1] = b[2] ^ g[1];
    assign b[0] = b[1] ^ g[0];
endmodule

module tb;
    reg  [3:0] bin;
    wire [3:0] gray, back;
    integer i;

    bin2gray u1 (.b(bin), .g(gray));
    gray2bin u2 (.g(gray), .b(back));

    initial begin
        $display("dec  binary  gray  back");
        for (i = 0; i < 16; i = i + 1) begin
            bin = i;
            #1 $display("%3d   %b    %b  %b", i, bin, gray, back);
        end
    end
endmodule

The printed table is the complete 4-bit Gray code. Check that each Gray value differs from the one above it in one bit only:

OUTPUT
dec  binary  gray  back
  0   0000    0000  0000
  1   0001    0001  0001
  2   0010    0011  0010
  3   0011    0010  0011
  4   0100    0110  0100
  5   0101    0111  0101
  6   0110    0101  0110
  7   0111    0100  0111
  8   1000    1100  1000
  9   1001    1101  1001
 10   1010    1111  1010
 11   1011    1110  1011
 12   1100    1010  1100
 13   1101    1011  1101
 14   1110    1001  1110
 15   1111    1000  1111

4. Alphanumeric Codes

ASCII (American Standard Code for Information Interchange) is a 7-bit code for 128 characters: 95 printable characters and 33 control characters (such as LF = 10, CR = 13). Often an 8th bit is added for parity or to form an 8-bit byte.

CharacterDecimalHex7-bit binary
'0'4830011 0000
'9'5739011 1001
'A'6541100 0001
'Z'905A101 1010
'a'9761110 0001
space3220010 0000

Upper and lower case letters differ only in bit 5 (value 32), so changing case is one bit flip. EBCDIC is IBM's 8-bit code, and Unicode (UTF-8, UTF-16) extends ASCII to all world scripts.

5. Error-Detecting Codes: Parity

A parity bit is added so that the total number of 1s is even (even parity) or odd (odd parity). The receiver counts the 1s again; a mismatch means an error.

MessageEven parity bitOdd parity bit
10110010 (four 1s already)1
110000110

Parity detects any odd number of bit errors but misses an even number (two flipped bits leave the count's parity unchanged) and cannot tell which bit is wrong. The parity generator and checker circuits are built from XOR gates (Lesson 14).

6. Error-Correcting Code: Hamming Code

Hamming's idea is to use several parity bits, each checking a different group of positions, so that the pattern of failed checks spells out the position of a single wrong bit.

Rules for the (7,4) Hamming code:

  • Number the bit positions from 1. Parity bits sit at the powers of two: positions 1, 2, 4. Data bits fill positions 3, 5, 6, 7.
  • The parity bit at position p checks every position whose binary number has the bit of value p set:
Parity bitChecks positions
P11, 3, 5, 7
P22, 3, 6, 7
P44, 5, 6, 7
  • For m data bits, the number of parity bits k is the smallest value with 2k ≥ m + k + 1. For m = 4, k = 3; for m = 8, k = 4 (a 12-bit code).

Hamming (7,4) code positions and check groupsHamming (7,4) code positions and check groups

SolvedExample 3 · Encode, corrupt and correct

Encode data 1011 with even-parity Hamming (7,4). If bit 5 is flipped in transit, show how the receiver finds it.

  1. Place data: D3 = 1, D5 = 0, D6 = 1, D7 = 1.
  2. P1 = D3 oplus D5 oplus D7 = 1 oplus 0 oplus 1 = 0.
  3. P2 = D3 oplus D6 oplus D7 = 1 oplus 1 oplus 1 = 1.
  4. P4 = D5 oplus D6 oplus D7 = 0 oplus 1 oplus 1 = 0.
  5. Codeword (positions 1 to 7): 0 1 1 0 0 1 1.
  6. Received with bit 5 flipped: 0 1 1 0 1 1 1.
  7. Checks: C1 = bits 1,3,5,7 = 0 oplus 1 oplus 1 oplus 1 = 1; C2 = bits 2,3,6,7 = 1 oplus 1 oplus 1 oplus 1 = 0; C4 = bits 4,5,6,7 = 0 oplus 1 oplus 1 oplus 1 = 1.
  8. Syndrome C4 C2 C1 = 101 = 5: bit 5 is wrong. Flip it back.
Answercodeword 0110011; syndrome 101 points to position 5

A syndrome of 000 means no single-bit error. Adding one more overall parity bit gives SEC-DED (single error correction, double error detection), which is what ECC memory uses.

💡 Tip

Key points to remember

  • BCD codes each decimal digit in 4 bits; 1010 to 1111 are invalid; BCD addition adds 0110 when the sum exceeds 9 or carries.
  • Excess-3 = BCD + 3; it is self-complementing, as are 2421 and 84-2-1.
  • Gray code: one bit changes between neighbours; gi = bi+1 oplus bi, and bi = bi+1 oplus gi.
  • ASCII is a 7-bit code; 'A' = 65, 'a' = 97, '0' = 48.
  • Parity detects odd numbers of errors; Hamming code with 2k ≥ m + k + 1 corrects a single error, and the syndrome gives its position.
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. What are weighted and non-weighted codes? Give the BCD, 2421 and Excess-3 codes for decimal digits 0 to 9. Which of them are self-complementing? (5 Marks)
  • Q2. Explain Gray code and its application. Convert (1011)2 to Gray code and (1101)_{Gray} to binary. (5 Marks)
  • Write: the one-bit-change property, the shaft encoder use, the XOR rules and both conversions step by step.
  • Q3. Add 184 and 576 in BCD. Explain why 6 is added. (5 Marks)
  • Q4. Construct the even-parity Hamming code for data 1011. Show how a single error in bit 5 is detected and corrected. (10 Marks)
  • Write: the position table, the three parity equations, the codeword, and the syndrome calculation.
  • Q5. Write short notes on ASCII and the parity method of error detection. (5 Marks)