1

Possible Duplicate:
Gray code in .NET

I want to get Gray code of a number from its Decimal equivalent.

Example:

Dec  Gray   Binary
 0   000    000
 1   001    001
 2   011    010
 3   010    011
 4   110    100
 5   111    101
 6   101    110
 7   100    111
Community
  • 1
  • 1
Javed Akram
  • 15,024
  • 26
  • 81
  • 118

3 Answers3

4

Assuming you only want to do this on non-negative integers:

static uint BinaryToGray(uint num)
{
    return (num>>1) ^ num;
}

You might also like to read this blog post which provides methods for conversions in both directions, though the author chose to represent the code as an array of int containing either one or zero at each position. Personally I would think a BitArray might be a better choice.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
1

The MSB stays the same. For the rest of the bits, you right shift one bit and xor. If you do a logical right shift so you know the bit shifted into the MSB is a 0, then you can just shift and xor.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0
#include<iostream.h>
#include<conio.h>
int deci,bits=0,deci_x3,bits_x3;
int array[100][20];
int array1[100][20];
int array2[100][20];
void binary(int [100][20],int,int,int);
void gray(int);
void display(int);
int find_bits(int);

void main()
{
textcolor(GREEN);
textbackground(BLUE);
clrscr();
cout<<"Enter the no. upto which gray code and excess 3 code is to be
generated : ";
cin>>deci;
bits=find_bits(deci);
deci_x3=deci+3;
bits_x3=find_bits(deci_x3);
for(int i2=0;i2<=deci;i2++)
{
for(int j2=0;j2<=bits;j2++)
{
array[i2][j2]=0;
array1[i2][j2]=0;
}
}
array2[0][bits_x3-1]=1;
array2[0][bits_x3-2]=1;
for( i2=1;i2<=deci_x3;i2++)
{
for(int j2=0;j2<=bits_x3;j2++)
{
array2[i2][j2]=0;
}
}
cout<<" DECI NO. BINARY EQ. GRAY CODE EXCESS 3";
cout<<endl<<"-------------------------------------------------------------
--";
display(0);
getch();
for(int i=1;i<=deci;i++)
{
binary(array,i,bits,0);
gray(i);
binary(array2,i,bits_x3,1);
display(i);
getch();
}
}

void binary(int array_bin[20][20],int i,int bits2,int flag)
{
int ix;
if(flag==1)
ix=i+3;
else
ix=i;
array_bin[i][bits2-1]=(array_bin[i-1][bits2-1]==0?1:0);
for(int k1=2,i1=2;k1<=bits2;k1++,i1*=2)
{
if((ix/i1)%2==0)
array_bin[i][bits2-k1]=0;
else
array_bin[i][bits2-k1]=1;
}
}



void gray(int i)
{
//FIXING THE HIGHEST ORDER BIT FIRST
array1[i][0]=array[i][0];
for(int j=1;j<bits;j++)
{
if((array[i][j-1]==0&&array[i][j]==0)||(array[i][j-1]==1&&array[i][j]==1))
array1[i][j]=0;
else
array1[i][j]=1;
}
}

void display(int m)
{
cout<<endl;
if(m<=9)
cout<<" "<<m<<" " ;
else
cout<<" "<<m<<" " ;
for(int j=0;j<bits;j++)
cout<<array[m][j];
cout<<" " ;
for( j=0;j<bits;j++)
cout<<array1[m][j];
cout<<" ";
for( j=0;j<bits_x3;j++)
cout<<array2[m][j];
}



int find_bits(int m)
{
int k,bits1=0;
do
{
bits1++;
k=1;
for(int i=1;i<=bits1;i++)
k=k*2;
}
while(k<=m);
return(bits1);
}
Sujit Agarwal
  • 12,348
  • 11
  • 48
  • 79