Math
Gray Code Converter
Gray code (reflected binary code) orders binary numbers so that consecutive values differ in exactly one bit, which prevents glitches in rotary encoders and Karnaugh maps.
Enter a binary or decimal value to get its Gray code, or a Gray code to decode it; every bit's XOR is listed.
Digits 0 and 1. Leading zeros are kept so the width stays fixed.
Try an example
Result
Result
—
- Binary
- —
- Gray code
- —
- Decimal
- —
Student quick launch
Grade planning, algebra checks, and formulas students reach for most.
Study path
Use this calculator with
Follow these when you want the formula behind the answer, a short lesson, or nearby tools in the same topic.
What the Gray code converter solves
In ordinary binary, going from 0111 to 1000 flips four bits at once; if a sensor reads mid-change it can report a wildly wrong value. Gray code fixes that by reordering the codes so each step changes one bit. Converting is a chain of XORs: the top bit stays, and each lower Gray bit is the XOR of two adjacent binary bits.
4-bit Gray code table
| Decimal | Binary | Gray | Decimal | Binary | Gray |
|---|---|---|---|---|---|
| 0 | 0000 | 0000 | 8 | 1000 | 1100 |
| 1 | 0001 | 0001 | 9 | 1001 | 1101 |
| 2 | 0010 | 0011 | 10 | 1010 | 1111 |
| 3 | 0011 | 0010 | 11 | 1011 | 1110 |
| 4 | 0100 | 0110 | 12 | 1100 | 1010 |
| 5 | 0101 | 0111 | 13 | 1101 | 1011 |
| 6 | 0110 | 0101 | 14 | 1110 | 1001 |
| 7 | 0111 | 0100 | 15 | 1111 | 1000 |
How to use it
- Choose the direction. Decimal input is converted to binary first.
- Type the bits (leading zeros are kept so you can work at a fixed width).
- Read the XOR table: each row shows the two bits combined and the resulting bit.
- Check the interpretation line: the next value's Gray code differs by exactly one bit.
How to read the answer
The result has the same width as the input. The Gray code is not a different number; it is a different label for the same count, so the decimal value shown belongs to both the binary and its Gray code.
Common mistakes and edge cases
- Decoding Gray to binary using Gray bits on both sides of the XOR. The left input must be the binary bit you already found.
- Dropping leading zeros. 0011 and 11 are the same number but different-width codes.
- Reading Gray code as a binary value: Gray 1110 is 11, not 14.
- Zero maps to zero in both directions.
- Negative numbers have no standard Gray code; use non-negative values.
Worked examples
Binary to Gray
1011 → Gray
Result
1110
Gray to binary
1110 → binary
Result
1011
Decimal to Gray
11 → Gray
Result
1110
Power of two
1000 → Gray
Result
1100
All ones
1111 → Gray
Result
1000
Zero
0000 → Gray
Result
0000
Invalid digit
1021
Result
—
Frequently asked questions
How do you convert binary to Gray code?+
Keep the leftmost bit. Then XOR each pair of neighbouring binary bits from left to right: 1011 → 1, 1 XOR 0 = 1, 0 XOR 1 = 1, 1 XOR 1 = 0 → 1110. The shortcut is binary XOR (binary shifted right by one).
How do you convert Gray code to binary?+
Keep the leftmost bit. Then each binary bit is the previous binary bit XOR the current Gray bit: 1110 → 1, 1 XOR 1 = 0, 0 XOR 1 = 1, 1 XOR 0 = 1 → 1011.
Why is Gray code used in rotary encoders?+
Only one bit changes between adjacent positions, so a reading taken while the encoder moves is at worst one step off instead of a completely wrong value.
Why is it called reflected binary code?+
The n-bit sequence is built by writing the (n−1)-bit sequence, then its mirror image below it, and prefixing 0 to the first half and 1 to the second.
Is Gray code a different number system?+
No. It is a different ordering of the same binary patterns, so a Gray code has no place values; you must decode it to read its value.
About this calculator
- Written by
- mathcheck editorial team
- Last reviewed
- September 4, 2026
Method
- Uses the values entered by the user and stable formulas documented on the page.
Related calculators
Bitwise Calculator
AND, OR, XOR, NOT, NAND, and NOR on binary, decimal, octal, or hex inputs at 4 to 64 bits, with an aligned bit grid, a bit-by-bit truth table, and the result in every base.
Binary to Decimal Converter
Convert binary to decimal with the positional-weight table (bit × 2^n), fractional bits after the binary point, and an option to read the bits as a signed two's complement number.
Decimal to Binary Converter
Convert decimal to binary with the repeated-division steps, fractional parts by repeated multiplication (0.625 → 0.101), repeating-fraction detection, and negatives as a sign or two's complement.
Bit Shift Calculator
Left, logical right, and arithmetic right shifts at 4 to 64 bits with the before/after bit rows, bits shifted out, overflow detection, and the ×2^n or ÷2^n meaning of the shift.
Two's Complement Calculator
Convert decimal to two's complement and back at 4, 8, 16, 32, or 64 bits with the invert-and-add-1 steps, the sign bit highlighted, overflow detection, and the unsigned reading of the same bits.
Binary Calculator
Add, subtract, multiply, or divide binary numbers with the column method shown: carries, borrows, partial products, and a long-division tableau, plus decimal, hex, and octal equivalents.
Last updated: September 4, 2026