Gray code converter

Enter a decimal, binary or Gray code number and get the other two forms. Gray code changes only one bit between neighbouring numbers, which is why rotary encoders and Karnaugh maps use it.

Decimal digits, or bits (0 and 1). Leading zeros in binary and Gray input set the width of the output.
Gray code1111
Binary
1010
Decimal
10
Gray code as a decimal number
15
Width in bits
4

Runs in your browser: nothing you enter is sent anywhere.

How to use it

  1. Choose whether your input is Decimal, Binary or Gray code.
  2. Type the value. The Gray code, binary and decimal forms update at once.
  3. To keep a fixed width such as 8 bits, type the leading zeros in binary or Gray input.

How it works

The conversion to Gray code is g = n XOR (n >> 1): each Gray bit is the XOR of the binary bit and the next higher one. The reverse repeatedly XORs the shifted value: n = g XOR (g >> 1) XOR (g >> 2) .... Values are big integers, so long bit strings are exact (up to 256 bits).

Example: decimal 10 is binary 1010 and Gray code 1111. Binary 0101 (5) gives Gray code 0111.

Tips and common mistakes

  • Gray code of consecutive numbers differs in one bit, but the Gray code is not the same as the binary number: do not read it as a decimal value.
  • The “Gray code as a decimal number” row is only useful if you need the code as an integer.
  • Negative numbers are not supported; convert the magnitude or use a fixed width of your choice.

Frequently asked questions

What is Gray code?

It is an ordering of binary numbers in which two consecutive values differ in exactly one bit. For three bits the sequence is 000, 001, 011, 010, 110, 111, 101, 100.

How do I convert binary to Gray code by hand?

Keep the leftmost bit, then XOR each bit with the bit to its left. Equivalently, Gray = n XOR (n >> 1). For 1010 (10) this gives 1111.

How do I convert Gray code back to binary?

Keep the leftmost bit, then XOR each Gray bit with the binary bit just obtained to its left. In other words each binary bit is the XOR of all Gray bits at or above its position. The Gray code 1000 becomes 1111 (15).

Why is Gray code used?

When a shaft encoder or other sensor changes position, several bits flipping at slightly different times can give a wrong reading with ordinary binary. With Gray code only one bit changes, so the error is at most one step.

Which Gray code is this?

The standard reflected binary Gray code. Other single-bit-change orderings exist and are not covered.

More in Developer

All tools in this category: Developer ·Updated November 3, 2026

Privacy choices

Google Analytics shows us which tools are used and whether they work. It may set cookies. Your choice is kept in this browser.