Gray Code Calculator

This Gray code calculator converts any whole number into its reflected binary Gray code, the binary ordering used throughout digital electronics because each successive value differs from the last by exactly one bit. Ordinary binary counting can flip several bits at once between neighbouring values, for example going from 3 to 4, and if those bits are read a fraction of a second apart by a sensor or circuit, the result can be a wildly wrong reading. Gray code removes that risk, which is why it turns up in rotary position encoders, analogue-to-digital converters, error correction schemes and the Karnaugh maps used to simplify logic circuits. To use the tool, enter a non-negative decimal number into the input field. The calculator returns three figures: the Gray code written out in binary, the same Gray code value converted back to decimal, and the plain binary representation of the number you entered, so you can compare the two encodings side by side. The conversion itself is a simple exclusive-or of the number with itself shifted one place to the right, and reversing it means XOR-ing each bit with all the more significant Gray bits above it. A worked example below shows the number 5 converting to Gray code 111 in binary, which you can confirm by entering 5. The tool suits students, programmers and electronics designers who need a fast, accurate conversion.

Calculate.co.nz is proud to be partnered with Health Based Building, a leader in sustainable and health-conscious building innovation. With over a century of experience, they develop high-performance systems like Foreverbreathe Specification, Magnum Board, and Foreverbreathe Paints to support energy-efficient, non-toxic living environments. Their commitment to healthier homes aligns with our belief that informed choices lead to better outcomes for Kiwi households.
Calculate.co.nz partner: Health Based Building
Advertise on this page
Gray = 111 (binary)
Gray code (decimal)7
Plain binary of n101

The formula

The Gray code of n is n XOR (n shifted right by one bit). To go back, you XOR each Gray bit with all the more significant Gray bits. Consecutive Gray codes differ in exactly one bit.

Worked example

For 5 (binary 101): shift right gives 2 (binary 10), and 5 XOR 2 = 7, which is 111 in binary. Enter 5 to confirm.

Frequently asked questions

What is Gray code?

A binary numbering where each step changes only one bit, also called reflected binary code.

Why is it useful?

Single bit changes avoid glitches in position encoders and simplify Karnaugh maps in logic design.

How is it computed?

Gray code equals the number XOR the number shifted right by one bit.

Who this calculator is for

This calculator is for students, programmers and anyone needing a quick, reliable result.

What this calculator assumes

  • You enter valid input.
  • The standard algorithm is applied.
  • Results are rounded for display.

Formula and sources

Related calculators