AZ Tools

Gray Code Converter

Convert

Gray code (reflected binary) is an ordering of binary numbers where two successive values differ in exactly one bit. That single-bit-change property makes it valuable for rotary encoders, Karnaugh maps and error-resistant position sensing, because no transient intermediate values appear when several bits would otherwise flip at once. Binary converts to Gray with g = b XOR (b >> 1); decoding walks the bits back with a running XOR. This tool converts in all directions — type a decimal number, a plain binary value, or a Gray-coded value, and it shows the other representations. Pick a fixed bit width to zero-pad the output for fixed-register displays.

Decimal
42
Binary
101010
Gray code
111111
Reference (0-7)
DecimalBinaryGray code
0000000
1001001
2010011
3011010
4100110
5101111
6110101
7111100

Binary → Gray: g = b XOR (b >> 1). Consecutive Gray values differ by exactly one bit. Bit width zero-pads the output only.

How to use

  1. Choose whether your input is decimal, binary, or Gray code.
  2. Type the value; the decimal, binary and Gray forms update instantly.
  3. Optionally set a bit width (4/8/16/32) to zero-pad the binary and Gray output.

Frequently asked questions

What is Gray code used for?
Rotary and linear position encoders, Karnaugh-map ordering, and any system where only one bit should change between adjacent states to avoid glitches during transitions.
How is binary converted to Gray code?
XOR the number with itself shifted right by one bit: gray = n ^ (n >> 1). Decoding reverses it with a cascading XOR from the most significant bit down.
Why do binary 2 (10) and 3 (11) become Gray 11 and 10?
Gray code guarantees a single-bit difference between consecutive values. From 2 to 3 only the last bit flips in binary, but the Gray sequence reflects to keep every step a one-bit change.
Does this handle large numbers?
Yes — conversion uses arbitrary-precision integers, so values well beyond 32 or 64 bits convert exactly. The bit-width option only pads the display.

Related tools