You are on page 1of 3

Gray Code Conversion - C | DaniWeb

http://www.daniweb.com/software-development/c/code/216355

950,253 Members | Top Members by Rank

DaniWeb is currently in read-only mode while we migrate to the new infrastructure. We will be back tomorrow. Sorry for the inconvenience.
Software Development > C > Gray Code Conversion

C Code Snippet

Views: 25279

Gray Code Conversion


by harshchandra on Nov 21st, 2004

This will convert a decimal number into Gray code .

C Code Snippet (Toggle Plain Text)


1. ///////////////////////////////////////////////////////////// 2. //// Coverting a decimal number to Gray code ///// 3. /////////////////////////////////////////////////////////// 4. 5. 6. 7. # include<stdio.h> 8. # include<conio.h> 9. # include<stdlib.h> 10. static int a[8],b[8],k=1,i; 11. void main() 12. { 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. } 47. } /* printing the gray code */ printf("\nThe gray code of the given number is :"); for(i=0;i<8;i++) printf("%d",b[i]); /* gray code conversion */ b[0]=a[7]; for(i=7;i>=0;i--) { if(a[i]==0 && a[i-1]==0) b[k]=0; if(a[i]==1 && a[i-1]==1) b[k]=0; if(a[i]==0 && a[i-1]==1) b[k]=1; if(a[i]==1 && a[i-1]==0) b[k]=1; k++; int n1; clrscr(); printf("Enter any Numbers :"); scanf("%d",&n1); while(n1!=0) { /* converting number to its binary equivalent */ a[i]=n1 % 2; n1/=2; i++; } /* printing binary equivalent */ printf("\nThe binary code of the given number is :"); for(i=7;i>=0;i--) printf("%d",a[i]);

Comments on this Code Snippet


cscgal
The Queen of DaniW eb Offline 13,669 posts since Feb 2002

Apr 27th, 2005

Permalink

Re: Gray Code Conversion

What Next? Check out this Code Snippet: Polynomial Addition Using Linked List

1 of 3

Gray Code Conversion - C | DaniWeb

http://www.daniweb.com/software-development/c/code/216355

What is grey code?

balajisankar
Newbie Poster Offline 2 posts since Jan 2010

Jan 11th, 2010

Permalink

Re: Gray Code Conversion

-2

C Syntax (Toggle Plain Text)


1. #include<stdio.h> 2. #include<conio.h> 3. void main() 4. {int a[10],i=0,c=0,n,b[10]; 5. printf("\n enter the binary code"); 6. scanf("%d",&n); 7. while(n!=0) 8. {a[i]=n%10; 9. n/=10; 10. i++; 11. c++; 12. } 13. for(i=c-1;i>=0;i--) 14. { 15. b[i]=a[i]; 16. } 17. for(i=c-1;i>=0;i--) 18. { 19. if(b[i]==1) 20. { 21. if(a[i-1]==1) 22. a[i-1]=0; 23. else 24. a[i-1]=1; 25. } 26. } 27. printf("\n the gray code is"); 28. for(i=c-1;i>=0;i--) 29. printf("%d",a[i]); 30. getch(); 31. }

Last edited by balajisankar; Jan 11th, 2010 at 11:26 pm.

folderol
Newbie Poster Offline 1 posts since Feb 2011

Feb 5th, 2011

Permalink

Re: Gray Code Conversion


Here is a more refined version that only requires one array, can be up to 32bit and includes a proving decode that's useful in its own right for rotary encoders.

C Syntax (Toggle Plain Text)


1. /* 2. Convert decimal number to Grey code and back again 3. */ 4. 5. # include<stdio.h> 6. 7. #define SIZE 32 8. 9. int bits[SIZE], i; // could be chars in 8 bit machine 10. unsigned long num; // could be int if less than 17 bit Grey code 11. 12. void main(){ 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. } /* printing binary */ printf("\n Enter number scanf("%lu",&num); /* converting number to binary equivalent */ /* MSB is 1st element in array */ for(i = 0; i < SIZE; i ++){ bits[SIZE-1-i] = (num >> i) & 1; "); printf("\n "); printf("%d",SIZE); printf(" bit number conversion\n");

What Next? Check out this Code Snippet: Polynomial Addition Using Linked List

2 of 3

Gray Code Conversion - C | DaniWeb

http://www.daniweb.com/software-development/c/code/216355

28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. }

for(i = 0; i < SIZE; i ++){ printf("%d",bits[i]); } /* in-line gray code conversion */ for(i = SIZE-1; i > 0; i --){ bits[i] = bits[i] ^ bits[i - 1]; } /* printing gray code */ printf("\n Gray code for(i = 0; i < SIZE; i ++){ printf("%d",bits[i]); } /* in-line conversion back to binary */ for(i = 1; i < SIZE; i++){ bits[i] = bits[i] ^ bits[i - 1]; } /* reprinting binary */ printf("\n Restored binary for(i=0;i<SIZE;i++){ printf("%d",bits[i]); } /* convert back to decimal */ num = 0; for (i = 0; i < SIZE; i ++){ num = (num<<1) | bits[i]; } /* reprinting decimal number */ printf("\n Restored decimal "); printf("%lu",num); printf("\n"); "); ");

Message:

Previous Thread in C Forum Timeline: Next Thread in C Forum Timeline:

problem reading integers from text file

Open GL

Tags
code , conversion , gray , int , string

Tag Cloud for code, conversion, gray, int, string


2d advice

array

ascii assembly basic beginner binary box c#

c++

case casting char character class clean

code

coding concatenation conversion convert count css database decimal design developer dictionary eclipse eof

error file find float format formatting forum function functions game getline gui help! hex hexadecimal html improve increment index input int integer

java

key list login loop message mips month number object output page php print

printf problem program programming python python-2.7 query random read replace return reverse save search software sort split

string

substring switch text textbox type unicode validation variable variables vb vb.net vector visual

words year

About Us | Contact Us | Advertise | Acceptable Use Policy


Forum Index | Build Custom RSS Feed

Follow us on

2011 DaniW eb LLC

What Next? Check out this Code Snippet: Polynomial Addition Using Linked List

3 of 3

You might also like