1. Home
  2. Developer tools
  3. Gray Code Converter

Gray Code Converter

Gray code is a way of counting where each step changes exactly one bit, which avoids glitches in rotary encoders and helps in Karnaugh maps. Convert in either direction or print the sequence for any width from one to six bits.

Free, no sign-up. Runs in your browser: nothing is uploaded.

How to use Gray Code Converter

  1. Choose binary to Gray, Gray to binary or the sequence.
  2. Enter bits or a decimal number.
  3. Read the result with the XOR working.

Features

  • Binary to Gray by XOR with the number shifted right by one
  • Gray to binary by running XOR from the top bit
  • Full sequence table for 1 to 6 bits
  • Shows decimal values next to the codes
  • Handles bit strings of any length
  • Decimal value of the input shown

Supported formats

Input

  • Binary or Gray bits
  • Decimal number

You get

  • Gray code
  • Binary
  • Sequence table

Private by design

Everything you enter is processed by your browser on this device. Nothing is sent to openWorkstand or anyone else, so the Gray Code Converter is safe for private and work data. After you have used it once while online, it also works without an internet connection. How local processing works

Good to know

  • The sequence table covers 1 to 6 bits; the converters accept longer values.
  • Only the standard reflected binary Gray code is used.

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.

Checked against the current version of the Gray Code Converter on .

Frequently asked questions

How does a rotary encoder benefit from Gray code?

The sensor pads under the disc change one at a time as it turns. If a reader samples at the moment of a change, it sees either the old or the new position. With ordinary binary, several bits flip together and a mid-change sample can be wildly wrong, for example 0111 to 1000 passing through 1111.

Is Gray code the same as the order of cells in a Karnaugh map?

Yes. Rows and columns of a Karnaugh map are labelled 00, 01, 11, 10, which is the two-bit Gray sequence, so neighbouring cells differ in one variable and can be grouped to simplify a Boolean expression.

How do I convert binary to Gray code by hand?

Keep the top bit, then XOR each bit with the bit to its left. Binary 1011 gives 1, 1 xor 0 = 1, 0 xor 1 = 1, 1 xor 1 = 0, so Gray 1110.

Why is Gray code used in encoders?

Because only one bit changes between positions, a reading taken mid-transition is wrong by at most one step instead of a random value.

Keep working with the result

openWorkstand also includes a free workspace with documents, spreadsheets, tasks, projects and a calendar that save to a folder on your computer.