Loading lessons...
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
| Class | Meaning | Examples |
|---|---|---|
| Weighted | each bit position has a fixed weight; the digit is the sum of the weights of the 1 bits | 8421 (BCD), 2421, 5421, 84-2-1 |
| Non-weighted | no positional weights | Excess-3, Gray |
| Self-complementing | the 9's complement of a digit is obtained by inverting its code bits | Excess-3, 2421, 84-2-1 |
| Reflected / unit-distance | successive codes differ in exactly one bit | Gray |
| Alphanumeric | letters, digits and symbols | ASCII, EBCDIC, Unicode |
| Error-detecting / correcting | extra bits reveal or fix errors | parity, Hamming |
2. Decimal Codes
| Decimal | BCD 8421 | 2421 | Excess-3 | 84-2-1 |
|---|---|---|---|---|
| 0 | 0000 | 0000 | 0011 | 0000 |
| 1 | 0001 | 0001 | 0100 | 0111 |
| 2 | 0010 | 0010 | 0101 | 0110 |
| 3 | 0011 | 0011 | 0110 | 0101 |
| 4 | 0100 | 0100 | 0111 | 0100 |
| 5 | 0101 | 1011 | 1000 | 1011 |
| 6 | 0110 | 1100 | 1001 | 1010 |
| 7 | 0111 | 1101 | 1010 | 1001 |
| 8 | 1000 | 1110 | 1011 | 1000 |
| 9 | 1001 | 1111 | 1100 | 1111 |
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.
| Advantages of BCD | Disadvantages 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.
Add 184 + 576 in BCD.
- Units: 0100 + 0110 = 1010 (10 > 9), add 0110: 1 0000. Write 0000, carry 1.
- Tens: 1000 + 0111 + 1 = 10000 (carry out), add 0110: 1 0110. Write 0110, carry 1.
- Hundreds: 0001 + 0101 + 1 = 0111 (no correction).
- Result: 0111 0110 0000.
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 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:
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:
Convert binary 1011 to Gray, and Gray 1101 to binary.
- Binary to Gray: g3 = 1, g2 = 1 oplus 0 = 1, g1 = 0 oplus 1 = 1, g0 = 1 oplus 1 = 0, so Gray = 1110.
- Gray to binary: b3 = 1, b2 = 1 oplus 1 = 0, b1 = 0 oplus 0 = 0, b0 = 0 oplus 1 = 1, so binary = 1001.
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.
// 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
endmoduleThe printed table is the complete 4-bit Gray code. Check that each Gray value differs from the one above it in one bit only:
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 11114. 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.
| Character | Decimal | Hex | 7-bit binary |
|---|---|---|---|
| '0' | 48 | 30 | 011 0000 |
| '9' | 57 | 39 | 011 1001 |
| 'A' | 65 | 41 | 100 0001 |
| 'Z' | 90 | 5A | 101 1010 |
| 'a' | 97 | 61 | 110 0001 |
| space | 32 | 20 | 010 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.
| Message | Even parity bit | Odd parity bit |
|---|---|---|
| 1011001 | 0 (four 1s already) | 1 |
| 1100001 | 1 | 0 |
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 bit | Checks positions |
|---|---|
| P1 | 1, 3, 5, 7 |
| P2 | 2, 3, 6, 7 |
| P4 | 4, 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 groups
Encode data 1011 with even-parity Hamming (7,4). If bit 5 is flipped in transit, show how the receiver finds it.
- Place data: D3 = 1, D5 = 0, D6 = 1, D7 = 1.
- P1 = D3 oplus D5 oplus D7 = 1 oplus 0 oplus 1 = 0.
- P2 = D3 oplus D6 oplus D7 = 1 oplus 1 oplus 1 = 1.
- P4 = D5 oplus D6 oplus D7 = 0 oplus 1 oplus 1 = 0.
- Codeword (positions 1 to 7): 0 1 1 0 0 1 1.
- Received with bit 5 flipped: 0 1 1 0 1 1 1.
- 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.
- Syndrome C4 C2 C1 = 101 = 5: bit 5 is wrong. Flip it back.
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.
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.
📝 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)