Gray Code

An extension of the topic of the binary system and positional number systems. Gray code is an ordering of binary numbers such that every two consecutive values differ by exactly one bit (formula G(k)=k XOR (k>>1)). This avoids errors in encoders and switches, where a simultaneous change of several bits could produce wrong intermediate states. It was devised by Frank Gray (patent 1947).

Gray Code
Mathematics · Number theory · Gray code (1947)
Number of bits nn (gives 2n2^n consecutive values)3 bits
In Gray code, adjacent values differ by…
number of bits n
3
values 2ⁿ
8
Gray: change / step
1 bit
binary: max / step
3 bits
🎯 Challenge: count the bit changes
streak: 0

I will give you a transition k−1 → k. Count how many bits PLAIN binary flips (Gray always changes exactly 1).

What is Gray code?

Gray code (also called reflected binary code) is a clever ordering of binary numbers in which every two adjacent values differ by exactly one bit. It was devised by the physicist Frank Gray at Bell Labs — the patent was filed in 1947 and granted in 1953.

Why, when we already have plain binary?

In plain binary, going e.g. from 01110111 to 10001000 requires four bits to change at once. If they did not switch perfectly simultaneously (which happens in mechanical devices), momentary wrong intermediate values would appear. Gray code removes this problem — only one bit ever changes, so no spurious intermediate state can arise.

How is it generated?

Elegantly, with a single formula — kk XORed with kk shifted one bit to the right:

G(k)=k⊕(k≫1)G(k) = k \oplus (k \gg 1)

where ⊕\oplus is the bitwise XOR and ≫1\gg 1 is a one-position right bit shift. For n=3n = 3 this gives, in order: 000, 001, 011, 010, 110, 111, 101, 100000,\,001,\,011,\,010,\,110,\,111,\,101,\,100 — and indeed every two adjacent values differ by one bit.

Invariant: Hamming distance = 1

The Hamming distance counts the positions at which two bit strings differ. For consecutive Gray-code values it is always exactly 11. It is this guarantee that makes Gray code robust against read errors.

Where is it used?

In position encoders (rotation-angle sensors — the dial on the right), switches, digital communication and anywhere we want to minimise errors during a change of state. Without Gray code, a rotating encoder could briefly report a completely wrong position.

Why this matters for your exams:

Gray code is a beautiful example at the boundary of mathematics and computer science: it shows how a well-thought-out mathematical representation of numbers solves a concrete engineering problem. Bitwise operations (XOR, shift) and positional number systems are a recurring element of computer-science and discrete-mathematics problems.

Remember: Gray code orders numbers so that neighbours differ by one bit (Hamming = 1). It is generated by the formula G(k)=k⊕(k≫1)G(k) = k \oplus (k \gg 1), and it is used in encoders and everywhere robustness against errors matters.

Illustrative model · NexTutor Virtual Laboratory

Enjoyed the experiment? Share it.

It's free — send it to friends from school.

Theory & formulas

Property

Adjacent values differ by exactly one bit (Hamming distance equal to 11).

Formula

G(k)=k⊕(k≫1)G(k)=k\oplus(k\gg 1) where ⊕\oplus is XOR and ≫1\gg 1 is a one-position right bit shift.

Why

In position encoders and switches a simultaneous change of several bits could give a wrong intermediate state; Gray code always changes only one bit, which eliminates this.

Notation: kk a number, G(k)G(k) its Gray code, ⊕\oplus XOR, ≫\gg right shift.

Frequently asked questions

›

By how much do adjacent values in Gray code differ?

By exactly one bit (Hamming distance = 1).

›

Why is Gray code used?

To avoid errors in position encoders and switches — a simultaneous change of several bits could give wrong intermediate states.

›

How is Gray code generated?

By the formula G(k) = k XOR (k >> 1) — a number XORed with itself shifted one bit to the right.

Teaching? Embed this experiment on your site.

Free widget — paste it on your school site, blog or notes. It links to the full version.

Teaching a class or tutoring? Guide students in NexTutor →