# Gray Code, weighted or non-weighted: that is the question!

To build the Gray Code we may use the mirror method, i.e. :

0                               0   0

1                               0   1

——-     ==>          ————    mirror line

1                               1   1

0                               1   0

A “mirror”  represented by a line is placed below the two initial bits ( 0 and 1 ), giving rise to the reflection indicated. Thereafter above the mirror line we add “0” and below the mirror line we add “1”. The following is a 2D version of the two bits Code: Using this figure we can easily build eight different  2-bits Gray Code: 00,01,11,10 – 01,11,10,00 – 11,10,00,01 – 10, 00,01,11 —  00,10,11,01 – 10,11,01,00 – 11,01,00,10 – 01,00,10,11. By iterating the riflective process allow us to extend the Gray Code representation. Here, I show two possible 3D version of 3-bits Gray Code.  Note the two different colored paths:  they give rise to sixteen possible different  3-bits  Gray Code  each!  ( moving clockwise and counterclockwise ).  So, the sixteen representation for the standard four bits Gray Code are:

1. 0000
2. 0001
3. 0011
4. 0010
5. 0110
6. 0111
7. 0101
8. 0100
9. 1100
10. 1101
11. 1111
12. 1110
13. 1010
14. 1011
15. 1001
16. 1000

Some Gray Code features:

•  Representation of successive word  code differ one another in only a single bit.
• The last representation differ from the first one just for a single bit, so the code is cyclic ( see the 2D and 3D cubes shown above ).

If we need to use just ten representation to code decimal digit from 0 to 9, in Gray, we can estract two subset of Gray Code having both the features mentioned above.

Excess-3 Gray Code:

We extract the ten representations starting from  previous “four bits word”  N. 4:

Gray           Dec

0010             0

0110             1

0111             2

0101             3

0100             4

1100             5

1101             6

1111             7

1110             8

1010             9

A code like this is clearly non-weighted ( the weight of second bit in the representation 0010  corrisponding to value 0 decimal should be zero  and that is impossible !)

Second SubSet of  Gray Code:

We can extract the first five representations and the last five representations to obtain such a code:

Gray              Dec

0000                0
0001                1
0011                2
0010                3
0110                4

1110                5
1010                6
1011                7
1001                8
1000                9

A Code like this is non-weighted. See ” Fundamentals of Logic Design”  – Sixty Edition – By Charles H Roth,Jr and Larry L Kynney.

In both last two cases, the argument that Gray is non-weighted is based only on the property that consecutive values differ in one bit position.

Gray Code as “weighted code”:

Given that I have not invented it, consider the four-bit gray code: b3, b2, b1, b0.

The weight of i-th bit is given by:  (2 ^(i + 1)) – 1.

Thus, the weight of the bits b2, being i = 2, is: (2^3)  – 1 = 7.

The weights for the four bits are then: 15, 7, 3, 1, starting from the MSB.

For the conversion into  decimal of a binary Gray code is necessary to consider the sign toggling on weights of “ones” . The weight of “one” in the most significant position assumes the sign “+” and the weight of the next ‘”one” takes on the “-” sign and so on..

For example, the decimal conversion of the Gray code 1011 is:

+15   7    -3    +1    <—– weigths

1      0      1       1 (gray) ——> + 1 * (15) + 0 * (7) – 1* (3) + 1 * (1) = 13 (dec)

where in brackets are the weights.

Another example for a 5 bits Gray code:

+31 -15    7    3     +1 <—– weigths

1       1     0     0      1 (gray) ——> + 1 * (31)   -1* (15) +1* (1) = 17 (dec)

Thas, just using bit weights, It is possible to write the following table:

Gray    Dec

0000    0

0001    1

0010    3

0100    7

1000   15

and  so on…

This Gray Code approach is totally different from the  previous two. It apply to the Gray reflected ( simple mirror construction ) and It is valid for any number of bits. Finally, the book Fundamentals of Logic Design by Roth and Kenney states:

“In general, the decimal value of a coded digit cannot be computed by a simple formula when a non-weighted code is used.”

simple formula” :  this is certainly a subjective definition, based on each person’s opinion of what is simple.

Imho  the Gray Code, as I showed, is a “weighted code”, if nothing else, at least in accordance with the “simple formula” approach.

To show the simple approach I am attaching:

My algorithm ( In Italian ) to translate a gray code in decimal based on weights.

My C program ( comments are in Italian ) to translate a gray code in decimal based on weights.

Note: in the  document,  at Brandeis University (USA), the Gray Code is defined as ” variable weighted code”, using an approach different of mine.

Acknowledgment:  to Pete Sanderson for his opinions on weigthed  and  non-weigthed Gray Code sent to me recently via e-mail. I used, in this post, some of  his considerations.