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.

Binary 101 to Gray code — columns bit i, b_i, b_(i+1), g_i = b_i XOR b_(i+1)
bit ib_ib_(i+1)g_i = b_i XOR b_(i+1)
210 (nothing above the MSB)1
1011
0101

binary101

Gray111

whole-word formG = B XOR (B >> 1) = 101 XOR 010 = 111

Warning:

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)

Compare with

Open the Gray code

This input is entered in the tool itself — it is too rich for a link to carry.

Note:

Notation this page assumes

  • Bit strings are written most significant bit first, and bit 0 is the least significant bit.
  • A width is stated explicitly wherever it changes the answer; nothing is silently sign-extended or truncated.
  • Gray code here is the reflected binary code, generated by reflection, with the least significant bit changing first.

Sources

  • Gray, “Pulse Code Communication”, US Patent 2,632,058 (1953)