Skip to main content

Binary to Gray Code Converter

Convert between binary and Gray code (reflected binary), where two successive values differ in only one bit. Supports both directions plus the decimal equivalent.

Input

Digits 0 and 1 only, up to 64 bits.

Output

Decimal value of the converted (output) code.

Was this helpful?

Guides

What is Gray code?

Gray code — also called reflected binary code — is a way of ordering the binary numeral system so that any two successive values differ in exactly one bit. Standard binary counting doesn't have this property: going from 0111 to 1000 flips all four bits at once. Gray code eliminates that "multi-bit jump" so a value only ever changes one position at a time as it counts up or down.

Why a single-bit change matters

In physical and digital systems, bits rarely switch at the exact same instant. If several bits of a binary counter change simultaneously, a reader can briefly see an invalid intermediate value — a glitch. Because Gray code only ever changes one bit between adjacent values, that race condition disappears, which is why it shows up in several places:

  • Rotary encoders and absolute position sensors — the shaft position is read directly off a Gray-coded track, so mechanical misalignment between sensor bits can't produce a wildly wrong reading, only an off-by-one.
  • Karnaugh maps — the row and column headers of a K-map are ordered in Gray code so that logically adjacent cells (differing by one input variable) are also physically adjacent, making groups of 1s easy to spot by eye.
  • Error correction and digital communications — because adjacent code values differ by a single bit, a one-bit transmission error tends to produce a nearby, rather than a wildly different, decoded value, which some coding schemes rely on to bound error impact.
  • Analog-to-digital converters — sampling near a transition boundary is less likely to produce a large, multi-bit output error.

How to use this tool

  1. Enter a value made only of the digits 0 and 1 (up to 64 bits) into the Value field.
  2. Choose a Direction: Binary → Gray Code to encode, or Gray Code → Binary to decode.
  3. The Converted Value and its Decimal Equivalent update automatically as you type.

The conversion formulas

Binary → Gray: the top (most significant) bit is copied unchanged; every other Gray bit is the XOR of the current and previous binary bit. This is the bit-string form of the well-known formula gray = binary XOR (binary >> 1).

Gray → Binary: the top bit is copied unchanged; every other binary bit is the XOR of the previously decoded binary bit and the current Gray bit. Unlike the encode direction, decoding is sequential — each output bit depends on the one before it.

Frequently asked questions

Does Gray code represent a different number than binary? No — a Gray code word and a binary word of the same length can each represent the same underlying integer; they're just two different bit patterns for counting through the same range. This tool shows the decimal value of whichever code it just produced, so you can see how it maps to an ordinary number.

Is Gray code always the same length as the input? Yes, both directions preserve bit length — no bits are added or dropped, so leading structure (and any leading zeros you type) carries through to the result.

Can I convert a decimal number directly? Convert it to binary first (for example with this site's binary/decimal converters), then paste the binary string in here.

Privacy

This tool runs entirely in your browser. Your input is never uploaded to a server.

binarygray codereflected binarybitwiseencoding

Love the tools? Lose the ads.

One payment clears every ad from your account, for good. No subscription, no tracking.