Binary to Gray Code Converter
Convert between binary and Gray code with step-by-step process and reference table.
Decode resistor-style capacitor markings? Try our Capacitor Code Calculator.
Input
Result
Step-by-Step Process
4-Bit Reference Table
| Decimal | Binary | Gray Code |
|---|---|---|
| 0 | 0000 | 0000 |
| 1 | 0001 | 0001 |
| 2 | 0010 | 0011 |
| 3 | 0011 | 0010 |
| 4 | 0100 | 0110 |
| 5 | 0101 | 0111 |
| 6 | 0110 | 0101 |
| 7 | 0111 | 0100 |
| 8 | 1000 | 1100 |
| 9 | 1001 | 1101 |
| 10 | 1010 | 1111 |
| 11 | 1011 | 1110 |
| 12 | 1100 | 1010 |
| 13 | 1101 | 1011 |
| 14 | 1110 | 1001 |
| 15 | 1111 | 1000 |
Understanding Gray Code
Gray code (also known as reflected binary code) is a binary numeral system where two successive values differ in only one bit. Unlike standard binary, where multiple bits can change simultaneously between consecutive numbers, Gray code minimizes bit transitions. This property makes it invaluable in digital communication, rotary encoders, and error reduction in analog-to-digital converters.
Conversion Formulas
For Binary to Gray conversion, the most significant bit (MSB) stays the same. Each subsequent Gray bit is the XOR of the current binary bit and the binary bit to its left. For Gray to Binary, the MSB also stays the same, but each subsequent binary bit requires XORing the current Gray bit with the already-computed binary bit to its left — a sequential accumulation process.
How to Convert Binary and Gray Code
Frequently Asked Questions
What is Gray code?
Gray code (aka reflected binary code) is a binary numeral system where two successive values differ in only one bit. It reduces errors in digital communication and positioning systems.
How do I convert binary to Gray code?
Keep the MSB the same. For each subsequent bit, XOR the current binary bit with the binary bit to its left. G[i] = B[i] XOR B[i-1].
How do I convert Gray code to binary?
Keep the MSB the same. For each subsequent bit, XOR the current Gray bit with the already-computed binary bit to its left. B[i] = G[i] XOR B[i-1]. This is a sequential accumulation process.
What is Gray code used for?
Gray code is used in Karnaugh maps for logic minimization, rotary encoders to prevent glitch states, digital communication to minimize bit errors, and analog-to-digital converters for smoother transitions.
What is the Gray code for decimal 5?
Decimal 5 = binary 0101. Converting: G[3]=0, G[2]=1⊕0=1, G[1]=0⊕1=1, G[0]=1⊕0=1. Gray code = 0111.