Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Code converters: BCD, Excess-3 and Gray code

How to represent and convert between BCD, Excess-3 and Gray codes, plus circuits and typical exam tasks.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Engineers use binary-coded decimal (BCD), Excess-3 and Gray codes when digital systems must represent decimal digits, bias numeric ranges, or avoid large switching glitches. These codes are common in digital displays, decimal arithmetic units, position encoders and ADC interfaces where single-bit change behaviour or simple decimal biasing is useful.

Key ideas

Start from definitions and simple properties.

  • Code: a mapping from a set of symbols (for example decimal digits) to a set of binary bit patterns. A code is a function from symbols → bit patterns.

  • Binary-coded decimal (BCD): a 4-bit code that represents decimal digits 0–9 using their binary values. BCD uses the 4-bit binary representation of the decimal digit, i.e. 0 → 0000, 1 → 0001, …, 9 → 1001. (Codes 1010–1111 are unused/invalid for pure BCD.) This is the standard 8421-weighted BCD code.

  • Excess-3 (XS-3): a 4-bit biased binary code where the stored value = decimal digit + 3. Formally, Excess-3 code for decimal digit d is the 4-bit binary representation of (d + 3). Example: decimal 0 → 0011, decimal 7 → 1010. Excess-3 is self-complementing: bitwise 1's-complement of the Excess-3 code for d equals the Excess-3 code for (9 − d). Show by arithmetic: bitwise complement of (d+3) in 4 bits = 15 − (d+3) = 12 − d = (9 − d) + 3.

  • Gray code (binary-reflected Gray code, BRGC): an n-bit code that maps integers 0..2^n−1 to bit patterns such that successive integers differ in exactly one bit (Hamming distance 1). This is called the single-bit-change property. Gray code is often used where mechanical or analog encoders produce transient errors if multiple bits change simultaneously.

  • Binary → Gray conversion rule: for an n-bit binary number B = B_{n−1} B_{n−2} … B_1 B_0 (B_{n−1} is MSB), the Gray code G = G_{n−1} ... G_0 is

    • G_{n−1} = B_{n−1}
    • G_i = B_{i+1} XOR B_i for i = n−2 down to 0 This gives one-bit change between consecutive integers.
  • Gray → Binary inverse rule: reconstruct binary bits from Gray bits by

    • B_{n−1} = G_{n−1}
    • B_i = B_{i+1} XOR G_i for i = n−2 down to 0 In practice, compute MSB = MSB of Gray, then XOR downwards.
  • Implementation notes and diagrams to draw in an answer:

    • To show a 4-bit Binary→Gray circuit, draw four input wires labelled B3, B2, B1, B0 (left to right MSB to LSB). Draw an equality wire from B3 to output G3. Draw three XOR gates: inputs (B3,B2) → output G2; inputs (B2,B1) → G1; inputs (B1,B0) → G0. Label each gate as XOR.
    • To show 4-bit Gray→Binary, draw G3 as input to output B3 directly. Then cascade XOR gates: B2 = G3 XOR G2; B1 = B2 XOR G1; B0 = B1 XOR G0. Show the feedback/cascade of outputs to next XOR input.
  • Converting BCD ↔ Excess‑3 is arithmetic: add or subtract binary 3 (0011). A hardware converter can be a 4-bit adder with constant 0011 (for BCD → XS‑3) or subtract 0011 (for XS‑3 → BCD) using adders/2's-complement if needed. For decimal-aware arithmetic (BCD adders) additional correction logic is required; here we only convert the digit code itself.

Derivations universities commonly ask for:

  1. Prove Binary→Gray produces single-bit changes for consecutive binary integers. Sketch: represent integer n in binary B(n) and Gray G(n)=B(n) XOR (B(n)>>1). Increment n→n+1 flips some low-order contiguous bits due to carry propagation. The XOR with right-shift cancels these multi-bit flips leaving a single bit difference — formal proof uses carry-run length but exams accept the standard argument and demonstration on a few values.

  2. Show Excess-3 is self-complementing: complement of (d+3) in 4 bits equals (9−d)+3; algebraic steps as above.

Formulas

G_{n-1} = B_{n-1} — Most significant Gray bit equals most significant binary bit. Symbols: G_i, B_i are bits (no units). Applies for any n-bit binary→Gray.

G_i = B_{i+1} XOR B_i for i = n-2 .. 0 — Each lower Gray bit is XOR of two adjacent binary bits. Equivalently, at word level: G = B XOR (B >> 1) for an n-bit B (BRGC).

B_{n-1} = G_{n-1} — MSB recovery when converting Gray→Binary.

B_i = B_{i+1} XOR G_i for i = n-2 .. 0 — Recover remaining binary bits by cascading XOR.

XS3(d) = BCD(d) + 3 — Excess-3 code = 4-bit binary of (decimal d + 3). Symbols: XS3(d) is 4‑bit code, BCD(d) is binary for d. Applies for decimal digits 0..9.

XS3'(d) = XS3(9 - d) — bitwise complement relation for Excess-3 (where ' denotes bitwise 1's complement in 4 bits).

Note: XOR and complement (') are bitwise logic; + denotes arithmetic on the integer value represented in 4 bits (binary addition; modulo 16 if overflowed); no SI units apply.

Worked examples

Example 1 — Convert decimal 7 to BCD, Excess-3 and 4-bit Gray (from binary of 7).

Step 1: Decimal 7 in BCD = binary 7 = 0111. (Show digits: B3=0, B2=1, B1=1, B0=1.) Step 2: Excess-3 = BCD + 3 = 7 + 3 = 10 decimal → binary 1010. So Excess-3 = 1010. Step 3: Binary→Gray for 0111. Use rules:

  • G3 = B3 = 0
  • G2 = B3 XOR B2 = 0 XOR 1 = 1
  • G1 = B2 XOR B1 = 1 XOR 1 = 0
  • G0 = B1 XOR B0 = 1 XOR 1 = 0 So Gray = 0100. Final: BCD=0111, XS-3=1010, Gray=0100.

Example 2 — Convert 4-bit Gray 1101 to binary (and state decimal).

Given G3 G2 G1 G0 = 1 1 0 1. Step 1: B3 = G3 = 1. Step 2: B2 = B3 XOR G2 = 1 XOR 1 = 0. Step 3: B1 = B2 XOR G1 = 0 XOR 0 = 0. Step 4: B0 = B1 XOR G0 = 0 XOR 1 = 1. So binary = 1001 = decimal 9. Check: Gray(9) = 9 XOR (9>>1) = 1001 XOR 0100 = 1101, correct. Final answer: binary 1001 (decimal 9).

Example 3 — Derive logic for 4-bit Binary→Gray and show gate count.

From formulas: G3 = B3 (wire), G2 = B3 XOR B2 (1 XOR gate), G1 = B2 XOR B1 (1 XOR), G0 = B1 XOR B0 (1 XOR). Total XOR gates = 3 and one direct wire for MSB; total inputs 4, outputs 4. Draw as described in Key ideas.

Common mistakes

  • When converting Gray→Binary, students sometimes XOR adjacent Gray bits instead of cascading the recovered binary bits (wrong: B_i = B_{i+1} XOR G_i, not G_{i+1} XOR G_i).
  • In proofs of single-bit-change for Gray, asserting “carry cancels out” without showing the stepwise XOR reconstruction — give a small numeric demonstration if unsure.
  • For circuit diagrams: drawing XORs between non-adjacent bits or swapping MSB/LSB positions; always label MSB and LSB clearly.

For your exam

Question types you will see:

  • Definitions and properties (define BCD, Excess-3, Gray; state self‑complementing property).
  • Short notes and comparisons (advantages of Gray over binary for encoders).
  • Derivations (derive Binary→Gray formula; prove single-bit-change for successive integers).
  • Numericals / conversions (convert decimal to BCD/XS-3/Gray and vice versa).
  • Design / circuit diagrams (draw Binary→Gray and Gray→Binary circuits; implement BCD↔XS‑3 with adders).

What a full-marks answer includes:

  • Precise definition with bold term and domain (e.g., BCD represents decimal 0–9 using 4 bits).
  • Correct formulas with indices and explanation of symbols.
  • If asked to draw a circuit, label input bits MSB→LSB, show gates and outputs and note gate types and counts.
  • For proofs, show small numeric examples plus the algebraic step (e.g., use XOR identity and carry-run argument) and conclude why Hamming distance = 1 for successive codes.

Quick check

  1. What is the Excess-3 code for decimal 4?
  2. Convert binary 1010 to 4-bit Gray.
  3. Given Gray 0110, what is the binary value (4-bit) and decimal?

Answers: 1) 0111. 2) Binary 1010 → G3=1, G2=1 XOR 0=1, G1=0 XOR 1=1, G0=1 XOR 0=1 → Gray = 1111. 3) Gray 0110 → B3=0, B2=0 XOR 1=1, B1=1 XOR 1=0, B0=0 XOR 0=0 → binary 0100 decimal 4.

(If anything above is unclear or you want hand-drawable circuit ASCII, tell me; I can add step-by-step gate-level boolean expressions.)

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?