Binary to Gray Code Converter
Convert between binary and Gray code (reflected binary). Gray code changes only one bit between consecutive values, making it ideal for error detection, rotary encoders, and digital circuits.
What is Gray Code?
Gray code (reflected binary) is a binary system where only one bit changes between consecutive values. This single-bit change property eliminates race conditions and errors in hardware, making it essential for rotary encoders, position sensors, error detection, and digital circuit design.
Conversion Options
Groups of 4 bits
XOR operation breakdown
Recommended Settings
Gray Code Best Practices
- •Gray code changes exactly one bit between consecutive values
- •Ideal for rotary encoders and position sensors (eliminates reading errors)
- •Use in state machines to prevent race conditions and hazards
- •Perfect for error detection in analog-to-digital converters
- •Enable 'Show Steps' to see the XOR operation breakdown
Common Use Cases
- •Rotary encoders: Angular position sensing with error-free transitions
- •Hardware design: State machines, counters, and sequential logic circuits
- •A/D conversion: Minimize errors during analog-to-digital conversion
- •Error detection: Communications and data transmission systems
- •Mechanical systems: Shaft encoders, position sensors, motor control
Pro Tips
- •Live conversion updates automatically as you type for instant feedback
- •Swap button reverses direction and uses current output as new input
- •MSB (most significant bit) always stays the same in both conversions
- •XOR operation: 0 XOR 0 = 0, 1 XOR 1 = 0, 0 XOR 1 = 1, 1 XOR 0 = 1
Most Popular
Most users convert binary to Gray code for hardware and encoder applications
When to Use This Tool
Gray code is essential for error detection because only one bit changes between consecutive values. This minimizes errors in mechanical or electrical systems where values transition sequentially. Critical for rotary encoders, position sensors, analog-to-digital converters, and communication systems where single-bit transitions reduce noise and glitches during state changes.
Use Gray code in state machines, counters, and sequential logic circuits to prevent hazards and race conditions. When multiple bits change simultaneously in binary, intermediate states can cause glitches. Gray code ensures only one bit changes at a time, making transitions cleaner and more reliable. Essential for FPGA design, ASIC development, and digital hardware synchronization.
Rotary encoders and shaft position sensors use Gray code patterns to read angular positions. Since only one bit changes between adjacent positions, reading errors are minimized even if the sensor reads during a transition. Perfect for robotics, CNC machines, motor control systems, and any application requiring precise position or rotation measurement.
How It Works
Choose conversion direction: Binary → Gray Code or Gray Code → Binary
Optionally select output format (grouped by 4 bits or continuous)
Enable 'Show Conversion Steps' to see the XOR operation breakdown
Enter your binary or Gray code string in the input field
The tool automatically converts as you type with live preview
For Binary → Gray Code: The MSB (leftmost bit) stays the same
Each subsequent Gray bit = current binary bit XOR previous binary bit
Example: Binary 1011 → Gray: MSB=1, then 1⊕0=1, 0⊕1=1, 1⊕1=0 = 1110
For Gray Code → Binary: The MSB (leftmost bit) stays the same
Each subsequent binary bit = previous binary bit XOR current Gray bit
This is a cumulative XOR process from left to right
Example: Gray 1110 → Binary: MSB=1, then 1⊕1=0, 0⊕1=1, 1⊕0=1 = 1011
The conversion is fully reversible: Binary → Gray → Binary produces original
Output is formatted with spaces (grouped) or continuous based on settings
Use swap button to reverse direction and verify the conversion
All processing happens in browser - your data never leaves your device
100% Private
Files never leave your device. All processing happens locally in your browser.
Lightning Fast
Powered by Client-side JavaScript with XOR-based Gray code conversion algorithms for optimal performance on modern browsers.
Open Source
Built with verified, open-source libraries. Fully transparent.
Frequently Asked Questions
What is Gray code and how is it different from binary?
Gray code (also called reflected binary code) is a binary numeral system where two successive values differ in only one bit. In standard binary, multiple bits can change simultaneously (e.g., 0111→1000 changes 4 bits). In Gray code, transitions change exactly one bit (0100→1100→1101→1111). This single-bit change property makes Gray code ideal for error detection and hardware applications.
How do you convert binary to Gray code?
Binary to Gray code conversion: (1) The most significant bit (MSB) stays the same. (2) Each subsequent Gray code bit is the XOR of the current binary bit and the previous binary bit. For example, binary 1011: MSB=1 (stays 1), then 1 XOR 0 = 1, then 0 XOR 1 = 1, then 1 XOR 1 = 0, giving Gray code 1110.
How do you convert Gray code to binary?
Gray code to binary conversion: (1) The MSB stays the same. (2) Each subsequent binary bit is the XOR of the previous binary bit and the current Gray code bit. This is a cumulative XOR process. For example, Gray 1110: MSB=1 (stays 1), then 1 XOR 1 = 0, then 0 XOR 1 = 1, then 1 XOR 0 = 1, giving binary 1011.
Why is Gray code used in rotary encoders?
Rotary encoders use Gray code because only one bit changes between adjacent positions. If the encoder is read during a transition, you either get the old value or the new value—never an intermediate nonsense value. With binary encoding, multiple bits changing simultaneously could produce completely wrong readings during transitions. Gray code eliminates these ambiguous states.
What are the advantages of Gray code over binary?
Gray code advantages: (1) Only one bit changes between consecutive values, preventing race conditions and glitches. (2) Minimizes errors in analog-to-digital conversion and position sensing. (3) Reduces power consumption in hardware (fewer bit flips). (4) Simplifies error detection in sequential systems. (5) Prevents hazards in state machines and asynchronous circuits.
Can Gray code represent the same range as binary?
Yes! An n-bit Gray code can represent exactly the same 2^n values as n-bit binary (0 to 2^n-1). The values are just encoded differently. For example, 4-bit Gray code and 4-bit binary both represent 16 values (0-15), just with different bit patterns. The conversion is lossless and fully reversible in both directions.
What is 'reflected' about reflected binary code?
Gray code is called 'reflected binary code' because the pattern mirrors itself around the midpoint. When you list Gray codes in order, the second half is the reverse of the first half with the MSB flipped. For 3-bit: 000,001,011,010 (first half) reflects to 110,111,101,100 (second half). This reflection property makes Gray code generation recursive and elegant.
Is my data secure using this tool?
Yes! All conversions happen entirely in your browser using JavaScript. Your binary data and Gray code never leave your device, are never uploaded to any server, or stored remotely. The tool operates 100% client-side for complete privacy. All calculations are performed locally in real-time.