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.

Advertisement
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

If you've found a bug, or would like to contact us, or learn more about James Graham and Calculate.co.nz.

Calculate.co.nz is partnered with Interest.co.nz for New Zealand's highest quality calculators and financial analysis.

Calculate.co.nz is the sister site of CalculatorHub.com, the world's largest calculator website by tool count.

All calculators and tools are provided for educational and indicative purposes only and do not constitute financial advice.

Calculate.co.nz is proudly part of the Realtor.co.nz group, New Zealand's leading property transaction literacy platform, helping Kiwis understand the home buying and selling process from start to finish. Whether you're a first home buyer navigating your first property purchase, an investor evaluating your next acquisition, or a homeowner planning to sell, Realtor.co.nz provides clear, independent, and trustworthy guidance on every step of the New Zealand property transaction journey.

Calculate.co.nz is also partnered with Health Based Building and Premium Homes to promote informed choices that lead to better long-term outcomes for Kiwi households.

Calculate.co.nz is hosted in Auckland by SiteHost New Zealand.

All content on this website, including calculators, tools, source code, and design, is protected under the Copyright Act 1994 (New Zealand). No part of this site may be reproduced, copied, distributed, stored, or used in any form without prior written permission from the owner.

About & trust: Why Calculate is NZ's most comprehensive · By the Numbers · How we compare · Editorial standards · How we keep data current · NZ finance glossary · Research & data · Financial literacy NZ · About · Privacy policy · Terms of use

Reviewed and maintained. Last reviewed 2026-08-15 and checked on a twice-monthly cycle against IRD, RBNZ and Stats NZ. How we keep data current.

© 2026 Calculate.co.nz. All rights reserved. Building free NZ calculators since 2011.