Binary to Gray Code Converter

Converts a binary string to Gray code using the standard formula gray = binary XOR (binary >> 1), computed with BigInt so the conversion stays exact regardless of input length. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-27

Overview

Introduction

Gray code reorders the binary numbers so that any two consecutive values differ in exactly one bit position, a property that makes it valuable anywhere a physical or noisy transition between values needs to be unambiguous.

This tool converts a binary string directly into its Gray code equivalent using the standard XOR-based formula.

What Is Binary to Gray Code Converter?

A converter that takes an ordinary binary string and produces its Gray code (reflected binary code) equivalent, also expressed as binary digits.

Gray code numbers use the same 0/1 alphabet as ordinary binary, but the sequence is reordered so consecutive values never differ by more than one bit.

How Binary to Gray Code Converter Works

The binary input is validated and parsed as an exact BigInt value, avoiding the 32-bit limits of JavaScript's native bitwise operators.

Gray code is then computed with the standard formula gray = binary XOR (binary >> 1): the value is XORed with itself shifted one bit to the right, which is exactly the operation that guarantees a one-bit difference between consecutive integers.

When To Use Binary to Gray Code Converter

Use it when implementing or studying rotary/absolute position encoders, Karnaugh map bit orderings, or any protocol that specifically calls for Gray-coded values, starting directly from a binary bit pattern.

It's also useful as a teaching tool for seeing exactly how the XOR-based Gray code formula transforms a given binary value.

Features

Advantages

  • Uses BigInt internally, so it isn't limited to 32-bit integers and can handle arbitrarily long binary inputs.
  • Applies the standard, well-known Gray code formula directly, with no approximation.
  • Instant, deterministic, fully client-side.

Limitations

  • Only encodes non-negative binary magnitudes; there is no single standard convention for encoding negative numbers in Gray code.
  • The output isn't zero-padded to a fixed width; pad it yourself if your application expects a specific bit width.

Examples

Converting binary 1010

Input

1010

Output

1111

1010 shifted right one bit is 0101. XORing 1010 with 0101 gives 1111.

Converting binary 101

Input

101

Output

111

101 shifted right one bit is 010. XORing 101 with 010 gives 111.

Best Practices & Notes

Best Practices

  • Feed the output into the Gray Code to Binary Converter to confirm it round-trips back to your original binary value.
  • Zero-pad the result yourself first if you need every Gray code value in a sequence to share the same fixed bit width.

Developer Notes

Implemented with JavaScript BigInt: gray = value ^ (value >> BigInt(1)), using BigInt(1) rather than the 1n literal syntax since this repo's ES2017 TypeScript target doesn't support BigInt literals.

Binary to Gray Code Converter Use Cases

  • Encoding position values for rotary or absolute encoders that rely on Gray code
  • Building or checking Karnaugh map bit orderings
  • Teaching how the XOR-based Gray code formula works on concrete binary values

Common Mistakes

  • Assuming Gray code and binary look the same for small numbers; they diverge starting at binary 11 (Gray code 10).
  • Forgetting the output isn't zero-padded, which can make consecutive Gray code values look like they have different bit widths.

Tips

  • Try consecutive binary inputs (e.g. 110, 111, 1000) and compare the outputs to see the one-bit-at-a-time change property in action.
  • Use the Gray Code to Binary Converter to verify the encoding round-trips correctly.

References

Frequently Asked Questions