DE Notes
Complete guide to Gray code: definition, binary-to-Gray and Gray-to-binary conversion, reflected binary construction, error reduction in shaft encoders, and GATE-level problems.
What is Gray Code?
Gray code (also called Reflected Binary Code) is a binary code where consecutive numbers differ by exactly one bit. This property is called the "unit distance" property.
Notice: Each Gray code value differs from the previous by exactly 1 bit. Binary does NOT have this property (e.g., 7→8: 0111→1000, all 4 bits change simultaneously).
Gray Code to Binary Conversion
Rule:
- MSB of binary = MSB of Gray (unchanged)
- Each subsequent binary bit = XOR of the converted binary bit above and current Gray bit
Example: Convert Gray 1101 to Binary
| Gray | 1 1 0 1 |
| Binary | 1001 = 9 |
| Verify | Gray for 9 = 1101 ✓ |
Why Gray Code? — The Shaft Encoder Problem
Consider a 3-bit shaft encoder (position sensor). As the shaft rotates, it passes through angular positions. At the transition from position 3 (011) to position 4 (100) in binary, all 3 bits must change simultaneously. Due to mechanical and electrical tolerances, bits don't change at exactly the same instant — this causes glitches.
| Binary transition 3 | 4: |
| 011 | 100 |
| All bits change | transient states: 000, 001, 010, 011 possible |
| Gray code transition 3 | 4: |
| 0010 | 0110 |
Applications of Gray Code
- Shaft/Rotary encoders — Position sensors in motors, CNC machines, robots
- Linear encoders — Position measurement in machine tools
- K-map layout — Gray code ordering (00,01,11,10) ensures adjacent cells differ by 1 bit
- Error detection — Fewer bit transitions mean fewer chances for errors
- FPGA gray counters — Reduces simultaneous bit transitions in clock domain crossing
Solved Numericals
Q1: Convert decimal 13 to Gray code.
Q2: Convert Gray code 1010 to decimal.
Interview Questions
Q1: Why is Gray code preferred over binary in shaft encoders? In Gray code, consecutive values differ by exactly one bit (unit distance property). During rotation, only one encoder disk track changes state at any transition. This eliminates transient errors that occur in binary encoding when multiple bits change simultaneously at position boundaries.
Q2: What is the unit distance property? A code has the unit distance property if any two consecutive code words differ in exactly one bit position. Gray code is the classic example. This minimizes errors during state transitions.
Q3: What is the relationship between Gray code and K-maps? The cell arrangement in Karnaugh maps follows Gray code ordering (00, 01, 11, 10) on both axes. This ensures that adjacent cells (which differ by one bit in input combination) are physically adjacent on the map, making it easy to visually identify groups of 1s.
Q4: Is Gray code a weighted code? No. Gray code is a non-weighted code — the bit positions don't have fixed place values. You cannot determine the value of a Gray code number by multiplying each bit by a fixed weight. This contrasts with BCD (weighted 8421).
Q5: How many bits change in going from Gray code 1111 (=15) to 0000 (=0)? In Gray code, going from 15 (1000) to 0 (0000) involves a change in only the MSB — exactly 1 bit. This is the cyclic property of Gray code; it wraps around with a single-bit change.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Gray Code — Reflected Binary Code, Conversion and Applications.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Digital Electronics topic.
Search Terms
digital-electronics, digital electronics, digital, electronics, codes, gray, code, gray code — reflected binary code, conversion and applications
Related Digital Electronics Topics