Gray Code

Convert between binary and Gray code representations.

Gray code: the answer in one line

Gray code writes numbers so that each step to the next number changes exactly one bit. To convert binary to Gray, keep the first bit and XOR every other bit with the bit to its left (for a whole number, n ^ (n >> 1)). Binary 1011 becomes 1110. To go back, keep the first bit and XOR each Gray bit with the binary bit you just recovered: 1110 returns 1011.

By Hommer Zhao

How to Use Gray Code

  1. Enter a value (text, decimal, or binary depending on the mode).
  2. It is converted to Gray code, where consecutive values differ by exactly one bit.
  3. Switch mode to convert Gray code back to binary/decimal.
  4. Copy the result. The conversion runs in your browser.

Gray code keeps only one bit changing between adjacent numbers, which prevents glitches in rotary encoders and reduces errors when signals change.

Examples

Decimal 2 → Gray — input:

010 (binary of 2)

Output:

011

Decimal 3 → Gray — input:

011 (binary of 3)

Output:

010

Decimal 4 → Gray — input:

100 (binary of 4)

Output:

110

The first bit is copied; each following Gray bit is the XOR of two adjacent binary bits.

4-bit Gray code table

Counting from 0 to 15 changes 26 bits in ordinary binary but exactly 15 in Gray code, one per step. The last step is cyclic too: from 15 back to 0 is 1000 to 0000, one bit. Every value in this table was computed by the same converter as the tool above when the page was built.

DecimalBinaryGrayBits changed (binary)Bits changed (Gray)
000000000--
10001000111
20010001121
30011001011
40100011031
50101011111
60110010121
70111010011
81000110041
91001110111
101010111121
111011111011
121100101031
131101101111
141110100121
151111100011

Convert by hand: 1011 to 1110 and back

Binary to Gray: copy the first bit, then each Gray bit is the binary bit XOR the binary bit to its left.

PositionBinary bitBit to its leftGray bit
11none1
2011
3101
4110

Gray to binary: copy the first bit, then each binary bit is the Gray bit XOR the binary bit you have just worked out to its left.

PositionGray bitBinary bit to its leftBinary bit
11none1
2110
3101
4011

Why encoders use it: the 7 to 8 example

A position encoder reads one bit per sensor track, and the tracks never switch at exactly the same instant. In plain binary, 7 to 8 is 0111 to 1000: all four bits change. While they change, a reader can see any mix of old and new bits, which is 16 possible patterns, including 1111 (15) and 0000 (0). In Gray code the same step is 0100 to 1100: one bit, so the only patterns a reader can see are those two. That is why Gray code is used in absolute and quadrature position encoders in preference to weighted binary. The patterns above are listed by the same function the tests recompute independently; no real encoder was measured.

Edge cases with the exact output

Each result below was produced by the same conversion code as the tool above, when this page was built. Text is shown in quotes so spaces stay visible. Several cases below are not rejected with an error, so a wrong-looking result can come from the tool silently skipping part of your input.

Decimal 0 to 7 to Gray

To Gray · decimal input

Input: "0 1 2 3 4 5 6 7"

Output: "00000000 00000001 00000011 00000010 00000110 00000111 00000101 00000100"

Decimal mode pads every value to at least 8 bits, so the leading zeros are part of the output. Read the last three bits of each result: 000, 001, 011, 010, 110, 111, 101, 100 is the 3-bit Gray sequence, and each step changes exactly one bit.

Binary 1011 to Gray

To Gray · binary input

Input: "1011"

Output: "1110"

Keep the first bit, then XOR each bit with the bit to its left: 1, 1 xor 0, 0 xor 1, 1 xor 1.

Gray 1110 back to binary

From Gray · binary input

Input: "1110"

Output: "1011"

Keep the first bit, then XOR each Gray bit with the binary bit just recovered to its left. This is the reverse of the example above.

An 8-bit Gray value to decimal

From Gray · decimal input

Input: "01101001"

Output: "78"

The input is read as Gray code, not as a decimal number. The result is the ordinary number that has this Gray code.

The letter A as Gray code

To Gray · text input

Input: "A"

Output: "01100001"

Text mode takes each character’s code (A is 65), writes it as 8 bits, then converts those bits.

Same number, two widths

To Gray · decimal input

Input: "5"

Output: "00000111"

Decimal mode pads to 8 bits. Binary mode keeps exactly the digits you type, so 101 gives a 3-bit result (next example).

Binary 101 keeps its 3-bit width

To Gray · binary input

Input: "101"

Output: "111"

Gray code has the same length as the input. The Gray value of 5 is 111 in 3 bits and 00000111 in 8 bits; both are the same number written with a different width.

Leading zeros are kept

To Gray · binary input

Input: "0011"

Output: "0010"

The first bit is copied, so a leading zero stays a leading zero. Strip it yourself if you want the shortest form.

255 and 256 in decimal mode

To Gray · decimal input

Input: "255 256"

Output: "10000000 110000000"

The width stays 8 bits up to 255 and grows to 9 bits at 256, because 256 does not fit in 8 bits.

Not rejected: decimal input that is not a whole number

To Gray · decimal input

Input: "3.7 -5 12abc abc"

Output: "00000010 00001010"

Nothing here raises an error. 3.7 is read as 3 and 12abc as 12; -5 and abc are skipped without a message. Count the results against your inputs.

Not rejected: a token that is not binary

To Gray · binary input

Input: "102 11"

Output: "10"

The token 102 contains a 2, so it is skipped silently and only 11 is converted. The output has fewer values than the input.

A number past 2^53

To Gray · decimal input

Input: "9007199254740993"

Output: "110000000000000000000000000000000000000000000000000000"

Decimal mode uses ordinary JavaScript numbers, which are exact only up to 9007199254740992 (2^53). The input is rounded to that value before conversion, so the output is the Gray code of 9007199254740992, not of the number typed.

Text beyond one byte: the euro sign

To Gray · text input

Input: "€"

Output: "11000011111010"

The euro sign has code 8364, which needs 14 bits, so the output is not one 8-bit group. Decoding it again is the next example.

Decoding that group back to text

From Gray · text input

Input: "11000011111010"

Output: ""

The decoder only accepts character codes from 1 to 255 and silently drops anything else, so the round trip returns an empty string.

All-zero byte in text mode

From Gray · text input

Input: "00000000"

Output: ""

Code 0 is dropped as well, so the output is empty rather than a character.

Where the name comes from

The name comes from Frank Gray of Bell Labs, whose 1947 patent application (granted in 1953) introduced the term “reflected binary code”. George Stibitz had described a code of this kind in a 1941 patent application, and others later called it “Gray code”. Source: the Wikipedia article on Gray code, which we read for this page; we did not read the patents themselves. Gray code is an encoding, not a cipher: there is no key, and the conversion above reverses it exactly.

Frequently Asked Questions

What is Gray code?

Gray code, also called reflected binary code, is a way of writing numbers in binary so that consecutive numbers differ in exactly one bit. In 4 bits, counting from 0 to 15 changes 26 bits in ordinary binary but only 15 in Gray code, one per step.

How do you convert binary to Gray code?

Keep the first bit, then XOR each remaining bit with the bit to its left. Binary 1011 becomes 1110. For a whole number n the same rule is n XOR (n shifted right by one bit).

How do you convert Gray code back to binary?

Keep the first bit, then XOR each Gray bit with the binary bit you have just recovered to its left, working left to right. Gray 1110 gives 1011.

Why is Gray code used in rotary encoders?

Physical sensors never change at exactly the same instant. In plain binary, 7 to 8 (0111 to 1000) changes all four bits, so a reader could briefly see any of 16 patterns. In Gray code the same step (0100 to 1100) changes one bit, so the reader sees either the old or the new value, never a wrong one in between.

Is Gray code the same as reflected binary code?

Yes, in everyday use. The standard Gray code is the reflected binary Gray code: the n-bit list is the (n-1)-bit list with 0 in front, followed by the same list in reverse order with 1 in front. Other single-bit-change orderings exist, but this is the one this converter uses.

Why does decimal mode give 8 bits for small numbers?

Decimal mode pads every result to at least 8 bits, and grows the width only when a number needs more (256 gives 9 bits). Binary mode does not pad: the result has exactly as many bits as the input, so 101 gives 111 and 00000101 gives 00000111. Both describe the same number.

What happens to invalid input?

It is skipped without an error message. In decimal mode a token such as abc or -5 is dropped and 3.7 is read as 3; in binary mode a token containing anything but 0 and 1 is dropped. If your output has fewer values than your input, check for a skipped token.

Why does the euro sign disappear when I convert Gray code back to text?

Text mode converts each character's code to Gray code, but the reverse direction only accepts codes from 1 to 255. The euro sign has code 8364, so its Gray code decodes to nothing. Printable ASCII and the Latin-1 range round-trip; larger characters do not.

Is Gray code a cipher?

No. It is an encoding with no key: anyone can convert it back with the formula above, so it provides no secrecy. It exists to reduce errors when a value changes, not to hide it.

Features

Privacy Focused

All processing happens in your browser. No data is sent to servers.

Free Forever

No registration, no limits, completely free to use.