Binary Codes, Decimal Codes, Gray Code, and Error Detection / Correction
What are Binary Codes? (In Simple Words)
Computers only understand electrical 0s and 1s.
- But as humans, we need to type our names, college roll numbers, symbols (
@,#), and phone numbers. - A Binary Code is simply a agreed-upon pattern of
0s and1s used to represent a specific character or number.
💡 Real-Life Indian Student Analogy:
When you sit for a university exam, DAVV gives you a unique Roll Number.
The university database doesn’t search you by your nickname; it identifies you by that code.
Binary codes do the exact same thing for computer data!
1. Decimal Codes: BCD and Excess-3
A. BCD (Binary Coded Decimal / 8421 Code)
- A weighted code where each bit has a positional weight of 8, 4, 2, and 1.
- Each decimal digit (0 to 9) is represented using 4 bits:
- 0 =
0000, 5 =0101, 9 =1001.
- 0 =
- Invalid BCD States: Numbers from 10 (
1010₂) to 15 (1111₂) are never allowed in standard BCD.
B. Excess-3 Code (XS-3)
- A non-weighted code made by adding 3 (
0011₂) to each BCD digit.- Formula: Excess-3 = BCD + 0011₂
- Decimal 0 →
0000 + 0011 = 0011 - Decimal 9 →
1001 + 0011 = 1100
- Self-Complementing Property:
If you flip all bits (1’s complement) of an Excess-3 number, you directly get the Excess-3 code of its 9’s complement!
(For example: Excess-3 for 2 is0101. Invert all bits to get1010, which is the exact Excess-3 code for 7!).
This made building early subtraction hardware very simple.
2. Gray Code (Unit Distance Code)
The Gray Code is a non-weighted code where only one single bit changes when going from one number to the next.
Why Do We Need Gray Code?
In standard binary, going from 3 to 4 changes 3 bits at the exact same moment:
011→100(all three bits flip).- If one switch flips slightly faster than the others, the computer might read wrong intermediate data (like
010or111). - In Gray code, only 1 bit ever changes at any step. This prevents errors in robotic arms, volume knobs, and motor angle sensors.
A. Binary to Gray Code Conversion
- The first bit (MSB) stays the same:
G₃ = B₃. - For every next bit, do an XOR (⊕) between the current binary bit and the bit on its left:
Gᵢ = Bᵢ₊₁ ⊕ Bᵢ- (Remember XOR rule: Same bits give
0, different bits give1).
Example: Convert Binary (1011)₂ to Gray:
- G₃ = B₃ = 1
- G₂ = 1 ⊕ 0 = 1
- G₁ = 0 ⊕ 1 = 1
- G₀ = 1 ⊕ 1 = 0
- Answer = 1110
B. Gray to Binary Code Conversion
- The first bit (MSB) stays the same:
B₃ = G₃. - For each next bit, XOR the newly calculated binary bit with the next Gray bit:
Bᵢ = Bᵢ₊₁ ⊕ Gᵢ
3. Character Codes: ASCII vs. EBCDIC
Computers need standard tables to recognize alphabets and keyboard symbols.
| Feature | ASCII | EBCDIC |
|---|---|---|
| Full Form | American Standard Code for Information Interchange | Extended Binary Coded Decimal Interchange Code |
| Bits per Character | Originally 7 bits (128 characters); Extended is 8 bits (256 characters) | Strictly 8 bits (256 characters) |
| Developed By | ANSI (Standard for modern PCs, Web, Linux, Windows) | IBM (Made specifically for large IBM mainframes) |
| Code Sequence | Continuous (A through Z are numbers 65 to 90) |
Non-continuous (has gaps between letter blocks) |
| Usage Today | Foundation of modern internet and UTF-8 | Legacy banking mainframes |
4. Error Detecting and Correcting Codes
When data travels through cables or Wi-Fi, electrical noise can flip a 0 into a 1 or a 1 into a 0.
A. Parity Bit (Error Detection Only)
A single extra check bit is added to the data:
- Even Parity: The total number of
1s (including parity) must be Even. - Odd Parity: The total number of
1s must be Odd. - Limitation: It only catches an odd number of errors (1, 3, 5). If 2 bits flip together, parity fails to notice the error!
B. Hamming Code (Detection + Auto Correction)
Invented by Richard Hamming, this code does two things:
- Detects that an error occurred.
- Identifies the exact bit location so the receiver can immediately flip it back to fix it!
- Parity Bit Positions: Parity bits (P₁, P₂, P₄, P₈, …) sit at powers of 2 (positions 1, 2, 4, 8).
- Data Bits: Data bits (D₃, D₅, D₆, D₇, …) fill the remaining slots.
- Syndrome Word: The receiver recalculates parity bits. The resulting binary value (the Syndrome) gives the decimal number of the faulty bit. If syndrome is
000, there are no errors!
💡 DAVV Exam Preparation Tips
5-Mark Question: “What is Gray Code? Convert binary (11011)₂ into Gray Code and explain why it is called a unit-distance code.”
- Define unit-distance code: only 1 bit flips between consecutive numbers.
- Show the step-by-step XOR calculation:
- G₄ = 1
- G₃ = 1 ⊕ 1 = 0
- G₂ = 1 ⊕ 0 = 1
- G₁ = 0 ⊕ 1 = 1
- G₀ = 1 ⊕ 1 = 0
- Final Gray Code = 10110.
- Mention motor position sensors as a key practical use.
10-Mark Question: “Explain error detecting and correcting codes. How does a 7-bit Hamming code detect and correct a single-bit error? Illustrate with an example.”
- Explain why parity bits fail when 2 bits flip.
- State the Hamming parity formula:
2ᵖ ≥ m + p + 1.- Draw the 7-bit layout table (P₁, P₂, D₃, P₄, D₅, D₆, D₇).
- Show an example where bit 5 flips, and demonstrate how the syndrome bits calculate to
101₂ = 5, pointing directly to the faulty bit.