Given that we have discussed Binary to Gray code conversion, let us discuss the Gray to BInary conversion.

## Conversion from Gray code to natural Binary

Let be the equivalent Gray code for an bit binary number with representing the index of the bit.

**1.** For ,

i.e, the most significant bit (MSB) of the Gray code is same as the MSB of original binary number.

**2.** For ,

i.e, bit of the Binary number is the exclusive-OR (XOR) of of the bit of the Gray code and of the bit of the binary number.

## Simulation model

**Look Up Table based Matlab/Octave mode for Gray to Binary conversion**

clear

% binary to gray code conversion

ipBin = [0:15] ; % decimal equivalent for a 4-bit binary

opGray = bitxor(ipBin,floor(ipBin/2))

% Gray to Binary conversion

[tt ind] = sort(opGray); % sorting Gray code elements to form the lookup table

opBin = ind(opGray+1)-1; % picking elements from the array

**The Trick from DSP-Guru to do Gray to Binary conversion, thanks to Jerry Avins.**

clear

% binary to Gray code

ipBin = [0:15]

opGray = bitxor(ipBin,floor(ipBin/2))

% Gray code to binary

opBin = bitxor(opGray,floor(opGray/2^8)) ;

opBin = bitxor(opBin,floor(opBin/2^4)) ;

opBin = bitxor(opBin,floor(opBin/2^2)) ;

opBin = bitxor(opBin,floor(opBin/2^1)) ;

## Concluding thoughts

1. As on now, I do not understand how the DSP Guru trick for Gray code to Binary works. Of course, if I figure out, I will update this post.

2. Having discussed Binary to Gray code conversion and Gray to Binary conversion, we are now armed to discuss the bit-error rate probabilities for various modulation schemes (Recall: We have been discussing symbol error rate in Additive White Gaussian noise till date).

Thanks, KrishnaD id you like this article? Make sure that you do not miss a new article
by subscribing to RSS feed OR subscribing to e-mail newsletter.
* Note: Subscribing via e-mail entitles you to download the free e-Book on BER of BPSK/QPSK/16QAM/16PSK in AWGN.*

{ 18 comments… read them below or add one }

how can we convert decimal to 2421 code

@M.Ramya: I have not tried it, but can the following link be of help to you?

http://mathforum.org/library/drmath/view/67281.html

i want a c program for converting a digital signal into binary format??????? pls help me

@shanu: you might want to check dec2bin() function in matlab

i want conversion of gray code to binary code using kmaps???????if this is thereplz tel me???not there den tel me d reason

@sneha: What is kmaps?

Since you ask how it works, here is an explanation:

Notation: A single letter represents an 8 bit value

A single letter followed by a digit represents one bit in that 8 bit value.

Bits are numbered from right to left, so bit n has the value 2^n.

Start with the binary value b.

b=b7;b6;b5;b4;b3;b2;b1;b0

Compute its gray code, g = b ^ ( b >> 1 )

g=g7;g6;g5;g4;g3;g2;g1;g0

[0] g7=b7; g6=b7^b6; g5=b6^b5; g4=b5^b4; g3=b4^b3; g2=b3^b2; g1=b2^b1; g0=b1^b0

Now recover the original binary value from g in three steps

[1] x = g ^ (g >> 4)

x=x7;x6;x5;x4;x3;x2;x1;x0

x7=g7; x6=g6; x5=g5; x4=g4 x3=g3^g7 x2=g2^g6 x1=g1^g5; x0=g0^g4

[2] y = x ^ (x >> 2)

y=y7;y6;y5;y4;y3;y2;y1;y0

y7=x7; y6=x6; y5=x5^x7; y4=x4^x6; y3=x3^x5; y2=x2^x4; y1=x1^x3; y0=x0^x2

substitute eqn [1] into eqn [2]

[2a] y7=g7; y6=g6; y5=g5^g7; y4=g4^g6; y3=g3^g7^g5; y2=g2^g6^g4; y1=g1^g5^g3^g7; y0=g0^g4^g2^g6

[3] z = y ^ (y >> 1)

z7=y7; z6=y6^y7; z5=y5^y6; z4=y4^y5; z3=y3^y4; z2=y2^y3; z1=y1^y2; z0=y0^y1

Substitute eqn [2a] into eqn [3]

[3a] z7=g7; z6=g6^g7; z5=g5^g7^g6; z4=g4^g6^g5^g7; z3=g3^g7^g5^g4^g6; z2=g2^g6^g4^g3^g7^g5; z1=g1^g5^g3^g7^g2^g6^g4; z0=g0^g4^g2^g6^g1^g5^g3^g7

Substitute eqn [0] into eqn [3a]

[3b] z7=b7;

z6=b7^b6^b7;

z5=b6^b5^b7^b7^b6;

z4=b5^b4^b7^b6^b6^b5^b7;

z3=b4^b3^b7^b6^b5^b5^b4^b7^b6;

z2=b3^b2^b7^b6^b5^b4^b4^b3^b7^b6^b5;

z1=b2^b1^b6^b5^b4^b3^b7^b3^b2^b7^b6^b5^b4;

z0=b1^b0^b5^b4^b3^b2^b7^b6^b2^b1^b6^b5^b4^b3^b7

Rearrange the bits on the right

[3c] z7=b7;

z6=b6^b7^b7;

z5=b5^b6^b6^b7^b7;

z4=b4^b5^b5^b6^b6^b7^b7;

z3=b3^b4^b4^b5^b5^b6^b6^b7^b7;

z2=b2^b3^b3^b4^b4^b5^b5^b6^b6^b7^b7;

z1=b1^b3^b3^b2^b2^b4^b4^b5^b5^b6^b6^b7^b7;

z0=b0^b1^b1^b2^b2^b3^b3^b4^b4^b5^b5^b6^b6^b7^b7

Now, because x^x=0 and y^0=y, we can just remove each identical pair

[3c] z7=b7;

z6=b6;

z5=b5;

z4=b4;

z3=b3;

z2=b2;

z1=b1;

z0=b0;

And that’s how it works.

@Bob: Thanks much.

I will try to digest and get back, for any clarifications…

use this formula

G=(N XOR 2*N)/2

@rayan2: It does the same thing? How does that work?

Hello!

I need convert 10-bits number from gray to binary. Could anybody help me?

@jerraz: Hope the Lookup Table based Matlab code provided in the post helps.

iam a student of heritage of 2nd yera I.T

I cant understand this line particualr line:

[tt ind] = sort(opGray); % sorting Gray code elements to form the lookup table

here ind refers to ???

@R.Ramya:

The variable ind refers to the index of the elements prior to sorting.

as a quick example,

a = [3 1 4];

[tt ind] = sort(a);

tt =

1 3 4

ind =

2 1 3

Hope this helps.

thanks a lot sir:)

hi krishna,

there is a typo in the article

it shuld be written as

1. For j=n-1;

b[n-1]=g[n-1];

instead of b[n-1]=j[n-1];

i.e, the most significant bit (MSB) of the Gray code is same as the MSB of original binary number.

cheers.

@R.Ramya: Thanks for noticing the typo. I corrected.