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).

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 to 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 — XORed with shifted one bit to the right:
where is the bitwise XOR and is a one-position right bit shift. For this gives, in order: — 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 . 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 , and it is used in encoders and everywhere robustness against errors matters.
Adjacent values differ by exactly one bit (Hamming distance equal to ).
where is XOR and is a one-position right bit shift.
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: a number, its Gray code, XOR, right shift.
By exactly one bit (Hamming distance = 1).
To avoid errors in position encoders and switches — a simultaneous change of several bits could give wrong intermediate states.
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.