Convert weighted binary to reflected Gray code with g = b XOR (b >> 1) – in a Gray sequence, consecutive values differ in exactly one bit, which kills glitches in rotary encoders.
Show calculation steps
Processed privately in your browser β nothing you paste is uploaded, logged or stored.
One-bit steps
Gray code reorders counting so that consecutive values differ in exactly one bit. Encoding from weighted binary is a single XOR pass β g = b β (b >> 1) β keeping the top bit and XORing each adjacent pair below it.
The property exists for physics: real signals cannot flip multiple bits simultaneously, so a binary 011β100 transition can be misread as anything mid-flight. In Gray sequence the worst mid-read is the neighbouring value.
Worked example
Binary: 1011 (decimal 11) g1 = b1 = 1 g2 = b1βb2 = 1β0 = 1 g3 = b2βb3 = 0β1 = 1 g4 = b3βb4 = 1β1 = 0 Gray: 1110
| Decimal | Binary | Gray |
|---|---|---|
| 0 | 0000 | 0000 |
| 1 | 0001 | 0001 |
| 2 | 0010 | 0011 |
| 3 | 0011 | 0010 |
| 4 | 0100 | 0110 |
| 5 | 0101 | 0111 |
| 6 | 0110 | 0101 |
| 7 | 0111 | 0100 |
| 8 | 1000 | 1100 |
| 9 | 1001 | 1101 |
| 10 | 1010 | 1111 |
| 11 | 1011 | 1110 |
| 12 | 1100 | 1010 |
| 13 | 1101 | 1011 |
| 14 | 1110 | 1001 |
| 15 | 1111 | 1000 |
Where Gray code works
Rotary and linear position encoders, FIFO pointers crossing clock domains inside chips (a mis-sampled Gray pointer is off by at most one slot), Karnaugh-map orderings, and error-resistant channel designs. Wherever a changing count is sampled asynchronously, Gray code removes the garbage-read hazard.
Not for arithmetic
Gray code has no positional weights β adding two Gray values digit-wise is meaningless. The pipeline is decode β compute β re-encode, each step exact. The paired decoder page performs the return trip with its cascade rule.
Privacy
The XOR pass executes in your browser; patterns stay on-device.
How to use the Binary to Gray Code Converter
- Enter the binary number (bit pattern).
- Click "Convert to Gray".
- The steps show the XOR of each adjacent bit pair – the whole algorithm is one XOR pass.
- Check with the table generator: consecutive values always differ by exactly one Gray bit.
Frequently asked questions
What is Gray code and what makes it special?
A reordering of binary counting in which consecutive values differ in exactly ONE bit. Binary 3β4 flips three bits (011β100); Gray 3β4 flips one (010β110). That single-bit property eliminates transient mis-reads in changing signals.
How is binary converted to Gray?
Keep the top bit; every other Gray bit is the XOR of neighbouring binary bits: g[i] = b[i-1] β b[i] – equivalently g = b β (b >> 1). For 1011: keep 1, then 1β0=1, 0β1=1, 1β1=0 β 1110. The steps show your bits.
Where is Gray code used in practice?
Rotary and linear encoders (a mechanical read during a transition can only be off by one position), asynchronous FIFO pointers crossing clock domains in chips, Karnaugh map orderings, and some ADC designs.
Why does a multi-bit flip cause real problems?
Physical sensors never switch bits at the same instant: reading 011β100 mid-transition can yield ANY of the 8 patterns momentarily. With Gray code the mid-read is at worst the neighbouring value – an off-by-one instead of garbage.
Is Gray code still positional – can I do arithmetic on it?
No – place values do not apply, and adding Gray codes directly is meaningless. Convert back to binary first (one XOR cascade), operate, convert again. It is an ORDERING code, not an arithmetic one.
Does the conversion preserve the number of bits?
Yes, exactly – Gray code is a permutation of the same 2βΏ patterns. The decimal equivalents shown confirm the value correspondence rather than the pattern.
Is this computed on my device?
Yes – a single XOR pass in the page. Nothing is uploaded.