1.4Codes: BCD, Gray, excess-3 and bit counting
The idea
Besides plain binary, hardware uses special codes: BCD stores a decimal digit per 4 bits, excess-3 adds 3 to it, and Gray code changes only one bit between neighbouring numbers.
Think of it like this: A rotary switch with no glitches: moving one click changes exactly one contact.
Why it exists
Gray code is the order of a K-map, and BCD addition is a classic GATE-style "adjust" question.
How it works
- BCD: write each decimal digit in 4 bits; patterns 1010 to 1111 never occur.
- BCD addition: add in binary; if the digit sum is above 9 (or a carry came out) add 0110.
- Gray code: g = b ⊕ (b >> 1), that is keep the MSB and XOR each bit with its left neighbour.
- Bits needed for N values: ⌈log₂ N⌉.
Worked example: Binary 1011 to Gray
Keep 1, then 1⊕0 = 1, 0⊕1 = 1, 1⊕1 = 0, giving 1110. Back again: 1, 1⊕1 = 0, 0⊕1 = 1, 1⊕0 = 1 gives 1011.
Now play with it
- →Scan the Gray column: neighbours differ in exactly one position, even from 15 back to 0.
- →Set N = 1000 and compare the bits for binary against 4 bits per digit in BCD.
| n | binary | Gray | BCD | excess-3 | Gray bits that changed |
|---|---|---|---|---|---|
| 0 | 0000 | 0000 | 0000 | 0011 | - |
| 1 | 0001 | 0001 | 0001 | 0100 | 1 |
| 2 | 0010 | 0011 | 0010 | 0101 | 1 |
| 3 | 0011 | 0010 | 0011 | 0110 | 1 |
| 4 | 0100 | 0110 | 0100 | 0111 | 1 |
| 5 | 0101 | 0111 | 0101 | 1000 | 1 |
| 6 | 0110 | 0101 | 0110 | 1001 | 1 |
| 7 | 0111 | 0100 | 0111 | 1010 | 1 |
| 8 | 1000 | 1100 | 1000 | 1011 | 1 |
| 9 | 1001 | 1101 | 1001 | 1100 | 1 |
| 10 | 1010 | 1111 | (invalid) | (invalid) | 1 |
| 11 | 1011 | 1110 | (invalid) | (invalid) | 1 |
| 12 | 1100 | 1010 | (invalid) | (invalid) | 1 |
| 13 | 1101 | 1011 | (invalid) | (invalid) | 1 |
| 14 | 1110 | 1001 | (invalid) | (invalid) | 1 |
| 15 | 1111 | 1000 | (invalid) | (invalid) | 1 |
Gray always changes exactly one bit per step (even from 15 back to 0). BCD and excess-3 are only valid for 0 to 9.
BCD wastes bits (4 per digit instead of 3.32) but converts digit by digit.
Check yourself
The Gray code of the binary number 1011 is
Exam notes (the dense version)
BCD uses 4 bits per decimal digit (patterns 1010 to 1111 are invalid); after adding two BCD digits, add 6 (0110) if the sum exceeds 9 or there was a carry. Excess-3 is BCD + 3 and is self-complementing. Gray code changes exactly one bit between consecutive numbers: g = b ⊕ (b ≫ 1); binary from Gray: b₀ = g₀, bᵢ = bᵢ₋₁ ⊕ gᵢ. The number of bits to represent N distinct values is ⌈log₂ N⌉; a decimal number with d digits needs about ⌈d·log₂10⌉ ≈ 3.32 d bits.
Previous year questions: Number Systems & Binary Arithmetic (5)
PYQs are grouped by topic, so this is the full set for Number Systems & Binary Arithmetic; try the ones that match this subtopic. Open all 5 on the topic page.