Gray Code to Binary Converter

Decodes a Gray code string back to ordinary binary using the standard iterative XOR-prefix scan, the exact inverse of Binary to Gray Code Converter. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-27

Overview

Introduction

Gray-coded values from rotary encoders or Karnaugh map exercises often need to be translated back into ordinary binary before they're useful in normal arithmetic.

This tool decodes a Gray code string back to standard binary using the classic iterative XOR-prefix decoding scan.

What Is Gray Code to Binary Converter?

A decoder that reads a reflected binary Gray code string and outputs the equivalent ordinary binary value.

It is the exact inverse of the Binary to Gray Code Converter in this same category.

How Gray Code to Binary Converter Works

The Gray code input is validated and split into individual bits.

The first binary output bit is copied directly from the first Gray code bit. Each subsequent binary bit is computed as the previously decoded binary bit XORed with the current Gray code bit — an iterative scan that reconstructs the original binary value one position at a time.

When To Use Gray Code to Binary Converter

Use it whenever a device, protocol, or exercise gives you a Gray-coded bit pattern and you need the equivalent ordinary binary value for arithmetic or further processing.

It's also a useful teaching companion to the Binary to Gray Code Converter for understanding the encode/decode relationship.

Features

Advantages

  • Implements the standard, well-known iterative Gray-code decoding algorithm with no approximation.
  • Works on inputs of any length since it processes bits sequentially rather than relying on fixed-width native operators.
  • Instant, deterministic, fully client-side.

Limitations

  • Assumes the input is already valid Gray code; it has no way to detect if a binary-looking string was never actually Gray-coded to begin with.
  • Output isn't zero-padded to a fixed width beyond the input's own length.

Examples

Decoding Gray code 1111

Input

1111

Output

1010

Bit 1 stays 1; bit 2 = 1 XOR 1 = 0; bit 3 = 0 XOR 1 = 1; bit 4 = 1 XOR 1 = 0, giving 1010.

Decoding Gray code 111

Input

111

Output

101

Bit 1 stays 1; bit 2 = 1 XOR 1 = 0; bit 3 = 0 XOR 1 = 1, giving 101.

Best Practices & Notes

Best Practices

  • Round-trip your result through the Binary to Gray Code Converter to confirm the decoding is correct.
  • Keep the input's bit width consistent with how it was originally encoded, since Gray code decoding is position-sensitive.

Developer Notes

Implemented with a plain string/array iteration (no BigInt bitwise ops needed) since the algorithm processes one character at a time: binaryBits[i] = binaryBits[i-1] === grayBits[i] ? '0' : '1'.

Gray Code to Binary Converter Use Cases

  • Converting a rotary or absolute encoder's raw Gray-coded reading back into ordinary binary for arithmetic
  • Checking Karnaugh map or digital logic coursework that involves Gray code sequences
  • Teaching how the XOR-prefix decoding scan reverses the Gray code encoding formula

Common Mistakes

  • Trying to decode with a single XOR operation, which doesn't work — decoding requires the sequential bit-by-bit scan, not a direct inverse formula.
  • Feeding in a plain binary value expecting it to already be Gray code — the tool will still decode it, but the result won't be meaningful unless the input really is Gray-coded.

Tips

  • Use the Binary to Gray Code Converter to generate valid Gray code test inputs for this tool.
  • For a decimal result instead of binary digits, use the Gray Code to Decimal Converter on the same input.

References

Frequently Asked Questions