Gray Code Encoder and Decoder
Convert binary numbers to Gray code, where adjacent values differ by only one bit, or convert Gray code back to binary. This encoding is used in rotary encoders and error correction.
Gray Code Encoder/Decoder
Convert between binary/decimal and Gray code (reflected binary code)
About Gray Code
Gray code (also known as reflected binary code) is a binary numeral system where two successive values differ in only one bit. This property makes it useful for error correction, digital communications, and rotary encoders.
Named after Frank Gray, this code minimizes errors in digital systems by ensuring only one bit changes at a time during transitions.
How the Gray Code Encoder/Decoder Works
Enter a binary number to convert to Gray code, or input Gray code to convert back to binary. Select the bit length for your conversion. The tool processes the conversion instantly.
Gray code ensures adjacent values differ by only one bit. To encode: XOR each bit with the bit to its left. To decode: XOR each bit with all decoded bits to its left.
The converter shows both the binary and Gray code representations side by side. Visual bit indicators show which bits change between consecutive values.
When You'd Actually Use This
Reading rotary encoders
Optical rotary encoders output Gray code. Prevents errors when transitioning between positions. Convert Gray code to binary to get the actual angle value.
Designing Karnaugh maps
K-maps use Gray code ordering for axes. Adjacent cells differ by one variable. This enables visual grouping for logic minimization.
Building error correction systems
Gray code minimizes errors in state transitions. Single-bit errors are easier to detect and correct. Used in some communication protocols.
Creating genetic algorithms
Gray code encoding in GA prevents large value jumps from single bit mutations. Small mutations produce small value changes. Improves convergence.
Interfacing with analog sensors
Some ADCs output Gray code. Reduces errors during conversion transitions. Convert to binary for processing in microcontrollers.
Teaching digital logic
Gray code demonstrates important digital design concepts. Shows why binary isn't always best. Great educational tool for engineering students.
What to Know Before Using
Gray code isn't for arithmetic.You can't add or subtract Gray codes directly. Convert to binary first, do math, convert back. Gray code is for representation, not calculation.
Multiple Gray code variants exist.This tool uses standard binary-reflected Gray code. Other variants exist for specific applications. Most common use is binary-reflected.
Bit length must be consistent.When working with hardware, know your bit width. 4-bit Gray code differs from 8-bit. Match your system's bit length.
Zero is always all zeros.In Gray code, zero is represented as all 0 bits. This is consistent across all bit lengths. First value in the sequence.
Pro tip: For rotary encoders, the key benefit is during transitions. When moving from position 3 to 4, only one bit changes. In binary, multiple bits might change, causing momentary incorrect readings.
Common Questions
Why is it called Gray code?
Named after Frank Gray, a Bell Labs physicist. He patented the code in 1953 for use in shaft encoders. Also called reflected binary code.
How do I convert binary to Gray code?
Keep the MSB the same. XOR each bit with the bit to its left. For binary 1011: MSB=1, then 1⊕0=1, 0⊕1=1, 1⊕1=0. Result: 1110.
How do I convert Gray code to binary?
Keep the MSB the same. XOR each Gray bit with the previous binary result. For Gray 1110: MSB=1, then 1⊕1=0, 0⊕1=1, 1⊕0=1. Result: 1011.
What's the main advantage?
Adjacent values differ by exactly one bit. Prevents errors during transitions. Critical for position encoders and state machines.
Can Gray code detect errors?
Single-bit errors are detectable if the result is an invalid transition. But Gray code isn't primarily an error-correcting code. Use Hamming code for that.
Is Gray code unique?
Binary-reflected Gray code is the standard form. Other Gray codes exist for specific purposes. This tool uses the standard binary-reflected version.
What bit lengths are supported?
Any practical bit length works. Common sizes: 4-bit (0-15), 8-bit (0-255), 10-bit (0-1023), 12-bit (0-4095). Match your application's requirements.
Other Free Tools
Base64 Encoder/Decoder
Base64 Encode and Decode Online
UTF-8 Encoder/Decoder
UTF-8 Encoder and Decoder
Binary Encoder/Decoder
Binary Encoder and Decoder
Hex Encoder/Decoder
Hexadecimal Encoder and Decoder
ASCII to Hex Converter
ASCII to Hex Converter: Text to Hexadecimal Translator
Barcode Generator
Free Barcode Generator
Binary to Text Converter
Binary to Text Converter
Free Printable Calendar Maker
Create & Print Your Custom Calendar
Pie Chart Maker
Free Pie Chart Maker Online