Binary to Gray Code Converter

This binary to Gray code converter turns a binary number into its reflected Gray code, or decodes Gray code back to binary. Each bit of the working is shown, so you can follow the XOR steps.

Updated
Runs in your browser. Your data is not uploaded.
Convert
Result (binary)
–
Result (decimal)
–
Result (hex)
–
Step by step

    

How to use the Binary to Gray Code Converter

  1. Choose Binary → Gray code or Gray code → binary.
  2. Type the value and pick whether it is written in binary, decimal, or hex.
  3. Read the result in binary, decimal, and hex, with the steps underneath.

How it works

gray = binary XOR (binary >> 1)

The first (leftmost) bit stays the same. Every other Gray bit is the XOR of a binary bit and the bit to its left.

To go back, keep the first bit, then XOR each Gray bit with the binary bit you just produced:

binary[i] = binary[i − 1] XOR gray[i]

Examples

  • 1010 → 1111.
  • 0111 (7) → 0100.
  • Gray 1111 → binary 1010.
  • 1000 (8) → 1100.

Gray code for 0 to 15

DecimalBinaryGray code
000000000
100010001
200100011
300110010
401000110
501010111
601100101
701110100
810001100
910011101
1010101111
1110111110
1211001010
1311011011
1411101001
1511111000

Each row differs from the next in exactly one bit, including the wrap from 15 back to 0.

What Gray code is for

In Gray code, two neighbouring numbers differ in only one bit. That matters when a value is read by a sensor. A rotary encoder moving from 7 (0111) to 8 (1000) in plain binary flips four bits at once, and if they don't all change at the same instant the reading can jump to any value. In Gray code the same step flips one bit, so there is no false reading. Gray code is also used in Karnaugh maps and in some error-correction schemes.

Limitations

  • Only non-negative whole numbers.
  • This is the standard binary-reflected Gray code; other Gray sequences are not covered.

Frequently asked questions

How do I convert binary to Gray code?

Keep the first bit, then write the XOR of each pair of neighbouring bits. 1010 becomes 1, 1⊕0 = 1, 0⊕1 = 1, 1⊕0 = 1: 1111.

How do I convert Gray code to binary?

Keep the first bit, then XOR each Gray bit with the previous binary bit.

Why is it called reflected binary code?

The sequence for n bits is built by writing the (n−1)-bit sequence, then the same list in reverse (reflected), with a 0 in front of the first half and a 1 in front of the second.

Often used together with the Binary to Gray Code Converter.