Decimal 5 → 3-bit Gray — Gray code
coredecimal to Gray in two steps
Answer
111
Why this example is worth doing
Going from decimal to Gray requires converting to binary first; there is no direct positional rule, because Gray code is not a positional number system. That is the most important structural fact on the page and it is easy to miss when the codes are shown in a table that looks like a counting sequence. Gray code has an order, but it does not have place values.
Try your own input in the Gray code. Convert binary to reflected Gray code and back, and build the sequence by reflection.
How the answer is reached
Binary 101 to Gray code
Each Gray bit is the XOR of the binary bit and the binary bit above it.
| bit i | b_i | b_(i+1) | g_i = b_i XOR b_(i+1) |
|---|---|---|---|
| 2 | 1 | 0 (nothing above the MSB) | 1 |
| 1 | 0 | 1 | 1 |
| 0 | 1 | 0 | 1 |
binary101
Gray111
whole-word formG = B XOR (B >> 1) = 101 XOR 010 = 111
This is the binary reflected Gray code. It is one Gray code among many: any single-bit-change ordering of the codewords is a Gray code, and other constructions give different tables.
Source: Frank Gray, US Patent 2,632,058, "Pulse Code Communication" (filed 1947, granted 1953)