Guide
Gray code: counting with one changed bit at a time
In ordinary binary, counting from 7 to 8 flips four bits at once: 0111 becomes 1000. If a circuit or a sensor reads those bits at the wrong instant, it can see any mixture, and a position that jumps from 7 to 15 in one reading is a bug that is very hard to catch. Gray code fixes this by rearranging the order of the codes so that every step changes exactly one bit. This page converts between the two and prints the sequence.
The conversion rules
To turn binary into Gray code, keep the leftmost bit as it is, and for every other bit write the XOR of that bit and the bit to its left in the original number. The formula is often given as g = b XOR (b >> 1), where the shift moves every bit one place to the right. To go back, keep the first bit, and for each further bit XOR the Gray bit with the binary bit you have just computed. Both directions are on the page, and the rule is printed under the result.
What the sequence shows
Underneath the converter is a table for the width you choose, from one to six bits. For three bits it reads 000, 001, 011, 010, 110, 111, 101, 100. Each line differs from the one before it in a single place, and the last line also differs from the first in a single bit, so the code can run round in a circle. The name “reflected” comes from the way the table is built: the n-bit sequence is the (n−1)-bit one, then the same list reversed with a leading 1.
Where it is used
Absolute rotary and linear encoders print the code on the disc or strip. Karnaugh maps label their edges in Gray order. Some analogue-to-digital converters and counters that cross clock domains use it so that a value sampled in the middle of a change is still close to correct. Genetic algorithms have also used it to encode numbers so that small changes in value correspond to small changes in the bit string.
Using the page
Pick a direction and type a bit string. The page shows the converted code, the decimal value of the binary number and the rule. Leading zeros are kept, so 0011 gives 0010 and the width is unchanged. Characters other than 0 and 1 are rejected. For a length longer than six bits, the converter still works, though the sequence table does not.
A worked example
The Sample button loads the binary number 10110110, which is 182 in decimal. The leading 1 stays. Then each bit is XORed with its left neighbour: 0 XOR 1 is 1, 1 XOR 0 is 1, 1 XOR 1 is 0, 0 XOR 1 is 1, 1 XOR 0 is 1, 1 XOR 1 is 0, and 0 XOR 1 is 1. The Gray code is 11101101. Converting it back with the opposite rule returns 10110110. For a shorter check, binary 1011 becomes 1110.
Limits
Only the standard reflected binary Gray code is used. Other balanced or n-ary Gray codes, which exist for special purposes, are not supported. The page converts numbers; it does not simulate an encoder, and it does not check that a scanned code is valid in a given device. The decimal value shown applies to the binary side of the pair, not to the Gray string.
Related tools
- BCD Converter covers another way of coding numbers in bits.
- Binary to Decimal gives the value of the binary number behind a Gray code.
- Bitmask Calculator is useful when you need to inspect or combine individual bits.
- Two’s Complement Calculator shows how signed values are stored.
Checked against the current version of the Gray Code Converter on .

