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.
42101010111111| Decimal | Binary | Gray code |
|---|---|---|
| 0 | 000 | 000 |
| 1 | 001 | 001 |
| 2 | 010 | 011 |
| 3 | 011 | 010 |
| 4 | 100 | 110 |
| 5 | 101 | 111 |
| 6 | 110 | 101 |
| 7 | 111 | 100 |
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
- Choose whether your input is decimal, binary, or Gray code.
- Type the value; the decimal, binary and Gray forms update instantly.
- 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
BCD Converter (Binary-Coded Decimal)
Convert decimal to and from 8421 binary-coded decimal — per-digit 4-bit nibbles, packed BCD hex bytes, and BCD validity checking.
Two's Complement Converter (8–64 bit)
Enter a decimal, hex, binary, or octal value and see its 8/16/32/64-bit two's complement bit pattern, plus its signed and unsigned readings, hex, octal, and one's complement — in your browser.
IEEE 754 Float Converter (32 & 64-bit)
See the exact IEEE 754 bit layout of any number in float32 and float64 — sign, exponent, and mantissa bits, hex, the stored value, and rounding — or decode hex bits back to a number, all in your browser.
Roman Numeral Converter
Convert between Arabic numbers and Roman numerals from 1 to 3999.
Number Base Converter
Convert numbers between binary, octal, decimal, and hexadecimal.
Unit Converter
Convert length, weight, temperature, area, volume, speed, and time.