Translate a Gray Code into Decimal ( weighted method )


To translate an n-bit bynary code into decimal we use the well known formula:

[bn-1…….b1b0(2) ](10) = bn-1*(2)n-1 + ……. + b1*(2)1 +b0*(2)0

where : (2)n-1 …….(2)1 (2)0 are called weights and bi is the i-th bit.

To convert an n-bit Gray Code into his decimal representation I modified the previous formula as follow:

[gn-1…….g1g0 (gray)](10) = bn-1*(2)n-1 + ……. + b1*(2)1 + b0*(2)0

where:

  • bis the i-th calculated bit;

  • bn-1 = gn-1

  • bi= [bi+1 – gi](MOD2) or bi= [bi+1 + gi](MOD2) with 0 =< i <= n-2;

and:

  • [bi+1 – gi](MOD2) is the two-bit subtraction ignoring the borrow;

  • [bi+1+ gi](MOD2) is the two-bit addition ignoring the carry.

Two-bit subtraction MOD2 and two-bit addition MOD2 give same result bit !

Now I will explain the conversion of a Gray code into decimal with an example.

Consider the following four-bit Gray Code :

1011(gray)

We have: n = 4. The Gray bits, starting from msb, are:

g3 =1

g2 = 0

g1 = 1

g0 = 1

and the calculated bits are:

b3 = g3 = 1

b2 = [b3 – g2](MOD2) = [1-0](MOD2) = 1

b1= [b2 – g1](MOD2) = [1-1](MOD2) = 0

b0= [b1 – g0](MOD2) = [0-1](MOD2) = 1

so, applying these calculated bits in my formula, finally we have:

[1011(gray)](10) = 1*(2)3 + 1*(2)2 + 0*(2)1 + 1*(2)0 = 13(10)

This calculation makes use of the same binary system weights and therefore proves that an n-bit Gray Code ( not a Gray Code such as that we use to code the ten digits from 0 to 9 ) may be translated into decimal by means of the weighted method shown.