Gray Code Converter
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
ASCII Code Table
Browse and search the 256-row ASCII / extended-ASCII table — decimal, hex, octal, binary, and the named control characters.
Number Base Converter
Convert numbers between binary, octal, decimal, and hexadecimal.
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.
Byte Size Converter (KB / KiB / MB / MiB / ...)
Convert between decimal (KB, MB, GB — SI base 1000) and binary (KiB, MiB, GiB — IEC base 1024) byte units, plus bits.
IPv4 Address Converter
Convert an IPv4 address between dotted-decimal, 32-bit integer, hex and binary — and see whether it's private, public, loopback and more.
Bitwise Calculator
AND, OR, XOR, NOT and bit shifts on two integers — shown in binary, hex, unsigned and signed decimal, with selectable 8, 16, 32 or 64-bit width.