Working Through DIGITAL ELECTRONICS NOTES (3. Gray Code)

I'm working through the following digital circuit problems. Continued.

lancamentomerlo.my.canva.site

  • Q: Convert the Gray code 11001 to binary code.

The method is to first keep the MSB 1, then XOR each bit with the next bit, working down from the upper bits.

4 3 2 1 0  // bit-position
1 1 0 0 1  // original Gray code
1 0 0 0 1  // bit[4]^bit[3] = 0 
1 0 0 0 1  // bit[3]^bit[2] = 0
1 0 0 0 1  // bit[2]^bit[1] = 0
1 0 0 0 1  // bit[1]^bit[1] = 1

This is how the conversion works. The reason it holds is that Gray code itself has to obey the rule that at most one bit changes between consecutive values.

Conversely, to convert from binary to Gray code,

ja.wikipedia.org

To convert an ordinary binary representation to Gray code, take the exclusive OR of the binary representation and the same value shifted right by one bit with a 0 prepended.

This amounts to detecting changes relative to the neighboring bit. I can't explain it theoretically, but I can intuitively sense why it works.

Binary
0 0 0 0 0 : 00000 ^ 00000 = 00000
0 0 0 0 1 : 00001 ^ 00000 = 00001
0 0 0 1 0 : 00010 ^ 00001 = 00011
0 0 0 1 1 : 00011 ^ 00001 = 00010
0 0 1 0 0 : 00100 ^ 00010 = 00110
0 0 1 0 1 : 00101 ^ 00010 = 00111

This is an English translation of the original Japanese post: DIGITAL ELECTRONICS NOTESをやってみる (3. グレイコード)