← 🔢 Number Systems & Binary Arithmetic
Subtopic 4 of 4

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

  1. BCD: write each decimal digit in 4 bits; patterns 1010 to 1111 never occur.
  2. BCD addition: add in binary; if the digit sum is above 9 (or a carry came out) add 0110.
  3. Gray code: g = b ⊕ (b >> 1), that is keep the MSB and XOR each bit with its left neighbour.
  4. 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.
nbinaryGrayBCDexcess-3Gray bits that changed
00000000000000011-
100010001000101001
200100011001001011
300110010001101101
401000110010001111
501010111010110001
601100101011010011
701110100011110101
810001100100010111
910011101100111001
1010101111(invalid)(invalid)1
1110111110(invalid)(invalid)1
1211001010(invalid)(invalid)1
1311011011(invalid)(invalid)1
1411101001(invalid)(invalid)1
1511111000(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.

Bits for N values10
Bits for d decimal digits (binary)20
Bits for d digits in BCD24
Largest unsigned value in that many bits1023

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

Remember: Gray: one bit changes per step; BCD digits stop at 9, add 6 to fix a sum over 9.
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.