Gray code (also called Reflected Binary Code) is a special type of binary numbering system
where two successive values differ in only one bit.
Why Gray Code is Used
In normal binary counting, multiple bits can change at the same time.
Example:
0111 (7) → 1000 (8) → 4 bits change
This can cause errors in digital systems (like counters, encoders, ADCs).
Gray code avoids this by ensuring only one-bit changes at a time, reducing transition errors.
Example: 3-bit Binary vs Gray Code
Decimal Binary Gray Code
0 000 000
1 001 001
2 010 011
3 011 010
4 100 110
5 101 111
6 110 101
7 111 100
✔ Notice: Only one-bit changes between consecutive Gray codes
How to Convert Binary → Gray Code
Rule:
MSB (most significant bit) stays the same
Each next Gray bit = XOR of current binary bit and previous binary bit
Formula:
G = B ⊕ (B >> 1)
Example:
Binary = 1011
Bit Operation Gray
MSB same 1
Bit Operation Gray
2nd 1⊕0 1
3rd 0⊕1 1
4th 1⊕1 0
Gray = 1110
How to Convert Gray → Binary
Rule:
MSB stays same
Each next binary bit = XOR of previous binary bit and current Gray bit
Applications of Gray Code
Rotary/shaft encoders
Analog to Digital Converters (ADC)
Karnaugh maps (K-map)
Digital counters
Error-resistant data transmission
Key Points
Gray code is a unit-distance code
Only 1-bit changes between adjacent numbers
Reduces glitches and transition errors
Not suitable for arithmetic operations directly