%3C/text%3E%3C/svg%3E">
💻
💻 Tech & IT

Gray Code ⇄ Binary Converter

Results update as you type.

Your numbers

Understanding the Gray code converter — concept and principle

Gray code (reflected binary code) orders numbers so that consecutive values differ in exactly one bit. Ordinary binary does not have this property: going from 7 (0111) to 8 (1000) changes all four bits at once, and a sensor read during that transition can return any wrong value. Gray code is therefore used in rotary and linear position encoders, Karnaugh maps, genetic algorithms and counters that cross clock domains in digital hardware. This converter turns a decimal number into binary and Gray code, and also decodes the number's bits as if they were already Gray code.

Binary to Gray code and back1101 XOR 0110 = 1011 is the Gray code of 13; reading 1101 as Gray code decodes to 1001, which is 9.Gray = n XOR (n >> 1)13 = 1101110113 >> 1 = 60110XORGray 10111011Back: read 1101 as Gray codeGray1101binary = 91001each bit = left bit XOR Gray bit above
Encoding is one XOR with the shifted copy; decoding runs a XOR from the left, bit by bit.

How it is calculated

The principle

The reflected construction explains the one-bit property. Take the Gray list for k bits, write it again in reverse order underneath, then prefix the first half with 0 and the second half with 1. Inside each half neighbours already differ in one bit, and at the seam the two middle entries are identical apart from the new prefix bit. The XOR formula is a shortcut for this construction: a Gray bit is 1 exactly where the binary number changes from one bit to the next, so a step of +1, which flips a run of trailing bits in binary, changes only one of those "transitions".

Worked example

  1. Input: 13, binary 1101.
  2. Shift right by one: 13 >> 1 = 6 = 0110.
  3. Gray: 1101 XOR 0110 = 1011 (decimal 11).
  4. Now treat the input bits 1101 as Gray code: b3 = 1; b2 = 1 XOR 1 = 0; b1 = 0 XOR 0 = 0; b0 = 1 XOR 0 = 1, giving 1001.
  5. Check with shifts: 13 ^ 6 ^ 3 ^ 1 = 9, so the decoded value is 1001 (9).
How Binary changes with Decimal numberHow Binary changes with Decimal number02,5005,0007,50010,00012,5003.256.59.751316.219.526Example: 1101Decimal number
Each point is the calculator re-run with only “Decimal number” changed; the gold point is the default example. Change the inputs above and this chart follows your numbers.
Decimal numberBinary
3.2511.01
6.5110.1
9.751001.11
131101
16.210000.01
19.510011.1
2611010

How to read the result

The Gray output is the bit pattern an encoder would show at position 13. The third line answers the opposite question: if a device reports the pattern 1101, the actual position is 9. Encoding then decoding always returns the original number, so Gray(13) = 1011 decodes back to 1101.

Common mistakes

Keep in mind: The converter works on non-negative integers within JavaScript's 32-bit bitwise range. Real encoders may also use other single-distance codes (such as balanced or excess Gray codes), so check the device's datasheet.

Frequently asked questions

Why are rotary encoders Gray-coded?

Mechanical contacts never switch at exactly the same moment. With only one bit changing per step, a reading taken mid-transition is off by at most one position.

How are Gray codes used in Karnaugh maps?

Rows and columns are labelled in Gray order (00, 01, 11, 10) so that neighbouring cells differ in one variable, which makes groups easy to see.

Sources: F. Gray, Pulse Code Communication, U.S. Patent 2,632,058 (1953) · M. Morris Mano and Michael D. Ciletti, Digital Design, Pearson

Checked 8 Nov 2026. Standard formulas and RFC/IEEE definitions, computed locally in your browser — nothing you type is sent anywhere.

💻 All tech & IT tools →🌍 Calculators by country →