Gray code converter: binary ↔ Gray

Convert binary to Gray code and back with XOR steps.

Was this helpful?
Share Tool

Binary 0111 in Gray code is 0100: XOR the bits with the same string shifted right by one (g = b XOR (b>>1)). This page encodes and decodes Gray, and it also accepts a decimal such as 7 (binary 0111) so you can walk the 4-bit table without converting first.

Need a positional decimal instead? Binary to decimal reads 0111 as 7. For every integer base at once, keep all bases at once in the binary converter.

0111 → 0100. Decode walks prefix-XOR: b₀ = g₀, then each next bit is the running XOR, so 0100 returns 0111. Adjacent integers in Gray differ by exactly one bit — that is why rotary encoders use it: a fence-post of 7 (0100) to 8 (1100) flips only the high bit.

The 16-row table lists decimal 0–15 with binary and Gray. 0 stays 0000. 1 is 0001. 2 is 0011. 3 is 0010. Open Step-by-step working for the XOR lines. Fractions are rejected; Gray here is an integer reflected binary code, not a floating format.

Close the tab when you are done. FlashKit does not store the pattern. Packed BCD is a different encoding — that is the BCD converter.

How to convert binary and Gray code

  1. Choose Binary → Gray, Gray → Binary, or Decimal → Gray. The sample 0111 encodes to 0100.
  2. Paste 0s and 1s, or a whole decimal. Spaces, underscores and 0b are allowed on bit pastes.
  3. Read Gray, binary and decimal together. Adjacent Gray values differ by one bit.
  4. Open Step-by-step working for g = b XOR (b>>1) or the prefix-XOR decode.

Key Features

  • Binary ↔ Gray and decimal input
  • g = b XOR (b>>1) encode and prefix-XOR decode
  • 4-bit table 0–15 (binary, Gray)
  • One-bit-change property called out for rotary encoders
  • Local, no upload

XOR encode and prefix-XOR decode

  • Encode with g = b XOR (b>>1): 0111 XOR 0011 = 0100. Equivalently keep the MSB, then each next Gray bit is XOR of that binary bit and its left neighbour.
  • Decode: b₀ = g₀. bᵢ = bᵢ₋₁ XOR gᵢ. For 0100: 0, 0XOR1=1, 1XOR0=1, 1XOR0=1 → 0111.
  • The map is involutive on a fixed width only after you decode; encoding twice is not identity. Always decode with prefix-XOR, not a second encode.
  • Decimal → Gray left-pads to a multiple of four bits (at least 4): 7 → 0111 → 0100 and 2 → 0010 → 0011. Binary → Gray keeps the typed length, so 111 encodes to 100.

One-bit change and rotary encoders

  • Natural binary 7 (0111) to 8 (1000) flips all four bits. Gray 7 (0100) to 8 (1100) flips one. A noisy contact then reports at most one bit of error.
  • That is why shaft encoders print Gray on the disc. A binary disc would glitch through 0–15 while the brush crossed 7→8.
  • The 4-bit wheel is cyclic: 15 (1000) to 0 (0000) also flips one bit. Wider Gray strings follow the same XOR rule.
  • This is not two’s complement and not BCD. Signed −5 at 8 bits is 11111011 on binary to decimal, which is a different lesson.

Common mistakes

  • Encoding twice and expecting the original bits. The inverse is prefix-XOR decode, not a second XOR with a shift.
  • Dropping the MSB rule. g₀ equals b₀; it is not XOR with a phantom 0 on the left in a way that flips it.
  • Mixing Gray 0100 with BCD 0100 (digit 4). Gray of 7 is 0100; BCD of 7 is 0111.
  • Pasting a decimal with a fraction. This widget is integers only.
  • Reading the 4-bit table as hex nibbles. The Gray column is not 0–F encoding.

4-bit Gray table (0–15)

  • Decimal, natural binary, and Gray. Adjacent Gray rows differ by one bit, including 15 → 0 if you wrap.
DecimalBinaryGray
000000000
100010001
200100011
300110010
401000110
501010111
601100101
701110100
810001100
910011101
1010101111
1110111110
1211001010
1311011011
1411101001
1511111000

Frequently Asked Questions

What is binary 0111 in Gray code?

0100. XOR 0111 with 0011 (the same bits shifted right). Decimal 7.

How do I convert Gray 0100 back to binary?

Prefix-XOR: 0, then 0XOR1=1, 1XOR0=1, 1XOR0=1 → 0111.

Why do rotary encoders use Gray code?

Adjacent positions differ by one bit, so a bouncing contact cannot skip across several binary values at once.

Can I type decimal 7?

Yes. Choose Decimal → Gray. 7 is 0111 binary and 0100 Gray in four bits.

Is Gray the same as two’s complement?

No. Gray is a reflected code for counting. Two’s complement stores negatives; that is a different tool.

Does encoding twice undo Gray?

No. Use the Gray → Binary direction (prefix-XOR). A second encode produces a different string.

Expert Guide for Gray Code Converter

View Blog
What Does 1011001 Mean in Binary? Binary to Decimal

What Does 1011001 Mean in Binary? Binary to Decimal

Short binary strings like 1011001 or 01011 are usually numbers, not letters. Learn to convert binary to decimal by hand, with a free converter to check.

Read Guide

Categories

Browse all FlashKit tools