Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Unit 1: Digital Codes & Error Detection / Correction

Lesson 2 of 20 in the free Digital Electronics-II notes on Siksha Sarovar, written by Rohit Jangra.

1.1 Why do we need codes at all?

Inside a digital system, everything is ultimately a string of 0s and 1s. A code is just a rule that tells us what each pattern of 0s and 1s means. Different jobs need different codes, so a single number like decimal 9 can be represented in several different ways:

CodeRepresentation of decimal 9Typical use
Pure binary1001Arithmetic inside the CPU
BCD (8-4-2-1)1001Driving 7-segment displays
Excess-31100Self-complementing arithmetic
Gray code1101Rotary encoders, K-maps
ASCII0011 1001Storing the character '9'

1.2 The code classification map

1.3 Weighted vs Non-weighted codes

A code is weighted if each bit position has a fixed numeric weight. The decimal value is then Σ (bit × weight).

  • 8-4-2-1 BCD: Standard BCD. Weights are 8, 4, 2, 1. So 1001 = 8 + 0 + 0 + 1 = 9.
  • 2-4-2-1 (Aiken) code: Self-complementing; weights chosen so that the 9's-complement of any digit is found by flipping every bit.

A code is non-weighted when bit positions have no fixed numeric weight. The two most important non-weighted codes are:

  • Excess-3 code: Formed by adding 3 to the BCD value of the digit. Decimal 0 → 0011, 5 → 1000, 9 → 1100. It is self-complementing, so subtraction can be done by simple complement-and-add.
  • Gray code: Successive numbers differ in exactly one bit. This is what makes it perfect for rotary shaft encoders — a small position error never produces a large numerical jump.

1.4 Complete code-conversion table

This is the single most useful table to memorise for Unit I — every code-converter problem in the syllabus reduces to one of these rows.

DecimalBCD (8-4-2-1)Excess-32-4-2-1 (Aiken)GrayBinary
000000011000000000000
100010100000100010001
200100101001000110010
300110110001100100011
401000111010001100100
501011000101101110101
601101001110001010110
701111010110101000111
810001011111011001000
910011100111111011001
1011111010
1111101011
1210101100
1310111101
1410011110
1510001111

How to read this: every row shows the same decimal number rendered in five different codes. Notice three things:

  1. BCD only goes 0000–1001. Codes 1010–1111 in 4-bit BCD are invalid.
  2. Excess-3 is BCD + 3. Decimal 0 in Excess-3 is 0011 (not 0000), so the all-zero word can be used as a "no digit" signal.
  3. Gray adjacent rows differ in exactly one bit. Look at the Gray column going 4 → 5 (01100111) or 7 → 8 (01001100). Only one bit changes between every pair of consecutive entries — that is the defining property of Gray code.

Binary → Gray conversion

    Gray[MSB]   = Binary[MSB]
    Gray[i]     = Binary[i+1] XOR Binary[i]

Example: Binary 1011 → Gray 1 (1⊕0) (0⊕1) (1⊕1) = 1110.

Gray → Binary conversion

    Binary[MSB] = Gray[MSB]
    Binary[i]   = Binary[i+1] XOR Gray[i]

1.5 Alphanumeric codes

When the message contains letters, punctuation and control signals — not just numbers — we need a longer code:

CodeBits per characterCapacityNotes
ASCII7 (often stored as 8)128 charsStandard for English text and serial communication
Extended ASCII8256 charsAdds graphic and accented characters
EBCDIC8256 charsIBM mainframes
Unicode (UTF-8)8 to 32 (variable)1.1 million+Modern web/text storage; superset of ASCII

1.6 Why errors happen

Inside any real digital system, bits are not perfect logical 0s and 1s. They are voltage levels travelling down a wire — and noise, EMI, ground bounce or a marginal supply can flip a 0 into a 1 or vice versa. We classify these flips as:

  • Single-bit error: Exactly one bit is wrong (most common in short transmissions).
  • Burst error: A contiguous group of bits is corrupted (typical on radio links or scratched disks).

1.7 Error-detecting codes — parity

A parity bit is one extra bit added to a word so the total number of 1s is always even (even parity) or always odd (odd parity).

Original wordEven parity bitTransmitted
1011001 (four 1s)010110010
1010100 (three 1s)110101001
1111111 (seven 1s)111111111
0000000 (zero 1s)000000000

Properties.

  • Detects any odd number of bit errors (1, 3, 5 …) — these always change the parity.
  • Cannot detect an even number of errors — two flips cancel each other.
  • Cannot locate the bad bit; you only know a word is bad.

Parity generator / checker circuit

The same XOR tree is used at both ends — generator side computes the parity bit; receiver side recomputes parity over data and the received parity bit. A non-zero result means at least one bit flipped during transmission.

1.8 Error-correcting codes — Hamming code

To correct a bit error (not just detect it), we need redundancy. The Hamming code does this by inserting parity bits at bit positions 1, 2, 4, 8, … (the powers of two). Each parity bit covers a specific group of data bits.

Encoding a 4-bit data word into a (7,4) Hamming code

Position layout:

    Bit position : 1   2   3   4   5   6   7
    Contents     : P1  P2  D1  P3  D2  D3  D4

Each parity bit Pk is computed over every position whose binary index has the bit k set:

ParityCovers positionsCoverage in binary
P11, 3, 5, 7All positions with bit-0 of index = 1
P22, 3, 6, 7All positions with bit-1 of index = 1
P34, 5, 6, 7All positions with bit-2 of index = 1

Set each Pk so its group has even parity.

Worked example — encode the data word D = 1011

We have D1 = 1, D2 = 0, D3 = 1, D4 = 1.

Position1234567
ContentsP1P2D1 = 1P3D2 = 0D3 = 1D4 = 1
  • P1 covers positions 1, 3, 5, 7 → bits at 3, 5, 7 = 1, 0, 1 → sum = 2 → P1 = 0 (already even).
  • P2 covers positions 2, 3, 6, 7 → bits at 3, 6, 7 = 1, 1, 1 → sum = 3 → P2 = 1 (make even).
  • P3 covers positions 4, 5, 6, 7 → bits at 5, 6, 7 = 0, 1, 1 → sum = 2 → P3 = 0 (already even).

Transmitted codeword: 0 1 1 0 0 1 1.

Decoding — locating an error using the syndrome

At the receiver, recompute each parity check. Treat each "parity failed" result as a 1 and "parity OK" as a 0. The combined bits, read from C3 C2 C1, form a syndrome that is the binary position number of the bad bit.

Syndrome interpretation table

Syndrome (C3 C2 C1)DecimalMeaning
0000No error
0011Bit 1 (P1) is wrong
0102Bit 2 (P2) is wrong
0113Bit 3 (D1) is wrong
1004Bit 4 (P3) is wrong
1015Bit 5 (D2) is wrong
1106Bit 6 (D3) is wrong
1117Bit 7 (D4) is wrong

The (7,4) Hamming code corrects any single-bit error and detects (but cannot correct) two-bit errors. Extended Hamming codes (8,4) add one overall parity bit and gain SECDED — Single Error Correction, Double Error Detection — which is the scheme used by ECC RAM on servers.

1.9 At-a-glance summary

TopicKey idea
BCD, Excess-3, GrayDifferent rules to encode decimal digits — choose by use case
ASCII / UnicodeAlphanumeric codes for text
ParityAdds one bit; detects an odd number of errors
HammingAdds log₂(n) parity bits; corrects one-bit errors
SyndromeBinary index of the bad bit; 0 means no error