Gray Code
Written by: maffelu , 2011-01-25
| Decimal | Binary | Gray code |
|---|---|---|
| 0 | 0000 | 0000 |
| 1 | 0001 | 0001 |
| 2 | 0010 | 0011 |
| 3 | 0011 | 0010 |
| 4 | 0100 | 0110 |
| 5 | 0101 | 0111 |
| 6 | 0110 | 0101 |
| 7 | 0111 | 0100 |
| 8 | 1000 | 1100 |
| 9 | 1001 | 1101 |
| 10 | 1010 | 1111 |
| 11 | 1011 | 1110 |
| 12 | 1100 | 1010 |
| 13 | 1101 | 1011 |
| 14 | 1110 | 1001 |
| 15 | 1111 | 1000 |
| Value 1 | Value 2 | Answer |
|---|---|---|
| True | False | True |
| True | True | False |
| False | True | True |
| False | False | False |
string[] binaries = new string[16];
string[] grays = new string[16];
//=========CREATE BINARY ARRAY===========
binaries[0] = ""; //We need something to index from
for (int i = 0;i <= 15 ;i++ )
{
int currentNumber = i;
//Convert iterator to binary
while (currentNumber != 0)
{
binaries[italic] = (currentNumber % 2).ToString() + binaries[italic];
currentNumber = currentNumber / 2;
}
//Add missing zeroes
while (binaries[italic].Length != 4)
{
binaries[italic] = "0" + binaries[italic];
}
}
//==========CREATE GRAY ARRAY=============
for (int i = 0;i <= 15 ;i++ ) //Outer loop, for each binary
{
for (int j = 3;j >= 0 ;j-- ) //Inner loop, for each binary bit
{
//Run the XOR check
if(j != 0)
{
if (binaries[i][j]=='0' && binaries[i][j-1]=='0')
grays[i] = "0" + grays[i];
if (binaries[i][j]=='1' && binaries[i][j-1]=='1')
grays[i] = "0" + grays[i];
if (binaries[i][j]=='0' && binaries[i][j-1]=='1')
grays[i] = 1 + grays[i];
if (binaries[i][j]=='1' && binaries[i][j-1]=='0')
grays[i] = 1 + grays[i];
}
else
{
if (binaries[i][j]=='0')
grays[i] = "0" + grays[i];
if (binaries[i][j]=='1')
grays[i] = 1 + grays[i];
}
}
}
//===============OUTPUT========================
Console.WriteLine("# Binary Gray code " + Environment.NewLine);
for (int k = 0;k < binaries.Length ;k++ )
Console.WriteLine("{0}: {1} {2} ",k, binaries[k], grays[k]);
Console.Read();
There are 43 comments on this article.
raj
2010-09-01 11:39:57
it was nice explanation but try to give some examples so that learners can try them to understand better
noel
2010-09-12 20:22:06
thanks
piya
2010-10-06 23:01:42
tell me how we can understnd properly how to know the sequence of digits in gr code
Samuel
2010-10-07 09:08:45
I beg that i be assisted how i can convert binary to Gray code
Maffelu
2010-10-07 13:22:00
Samuel: The article actually explains how you convert binaries to Gray Code. The example is written in C# but is extremly general.
Is there something in the code that is unclear?
rajesh
2010-10-25 09:42:14
thanks.........
2010-11-07 01:17:14
plz tell me the method of decimal to gray code conversion
jkarretero
2010-11-10 00:04:29
In case that could be useful for someone, this is a function to convert binary to Gray code.
/*
The purpose of this function is to convert an unsigned
binary number to reflected binary Gray code.
*/
unsigned short binaryToGray(unsigned short num)
{
return (num>>1) ^ num;
}
Khan
2010-11-29 10:23:31
Few more examples could have served the purpose more usefully.Nice though!
2011-01-23 07:52:51
some how detailed explanation is needed
2011-02-10 21:19:15
where do i xor the number what value
Banahene A. Dic
2011-02-12 08:15:33
Infant for man and God I have really admire you guys site
Hop
2011-03-08 13:52:05
Now that we know how XOR works we can convert regular binary string 0011 (decimal 2):
We check each bit, starting from the right, with the next one:
1 XOR 1 = 0
1 XOR 0 = 1
0 XOR 0 = 0
0 XOR 0 = 0
------------
two things :
1) regular binary string 0011 != 2 but == 3
2) where did the 0001 came from in the boolean xor table, i assume the 0011 comes for the graycode 2, and the 0010 for the binary 2. But where do this 0001 come from ? thx !
Rachi
2011-03-29 00:31:30
You are perfect...!
Adetoun
2011-04-05 01:55:49
Great work
dinesh kumar
2011-04-10 01:06:24
I'M CAN UNDERSTAND IN GRAY CODE NICE EXAMPLE THAT..
Junias
2011-04-12 01:19:43
convert 10101011 to gray code pls
gaurav salvi
2011-04-24 01:13:37
what is the use of gray code in any field???
(general, electronics, etc uses)
Maffelu
2011-04-24 05:51:57
gaurav salvi: The use of gray code in computers lies in the development of hard discs. Using gray code reduces errors when there is a misread on the hard disc.
You can read some more about it here actually:
http://www.duxcw.com/digest/guides/hd/hd6.htm
asem mohamed
2011-05-20 08:15:49
thx
like to learn more
albert
2011-06-14 07:22:45
i like this artical
priya
2011-07-06 23:49:40
easy to larn
priyanka
2011-07-07 00:00:27
Thaks...................
ashutosh
2011-07-22 09:50:44
most understandable
Mohammed
2011-09-03 03:59:49
understandable
Vasanth
2011-09-06 07:13:29
nice one..pls give more eg.
hari
2011-09-07 00:27:10
Interesting, the hard disk example is nice.
parth
2011-09-12 10:18:53
good...
2011-10-07 02:03:05
why we use gray code
kedir
2011-10-07 08:24:20
i like it it's easy and clear thanks!!!!
sid bloodsaw
2011-10-12 16:24:44
only thing i have to say is that the decimal number for the conversion to gray code was 3 not 2 ^.^ other than that thanks for this! i understood it now :D
younes
2011-10-16 15:08:01
thanks very good ......
azhar
2011-10-27 11:50:10
hi
this lesson is very good to understand
Albert
2011-11-15 07:04:06
hallo,
wollte eigentlich fragen, wie kann man sich einen Algorithmus überlegen damit eine zahl kodiert in gray-code ins dezimalzahl übertragen kann ?!!
mfg, Albert
Manal
2011-12-01 07:36:41
Really nice explanation there...
I really understand how it woks...
pinky
2011-12-14 19:07:22
what is gray code
priyanka
2011-12-14 19:13:46
what is binary number system &what is gray code & fixeb /floating point representation
EL-tayib
2011-12-29 18:59:20
good job!
tanushree karma
2012-01-09 20:36:40
thanks!!!!!!!!!!!
Ali Mohammed
2012-01-15 10:27:09
Hi, thanks a lot for this explanation. it really helped me understand Gray code and its use practically.
Aniekeme
2012-01-27 10:24:45
Pls i want to know how to convert from binary base to gray code
Mes
2012-02-01 13:33:18
it was shown somewhere up there, but still
x = (i/2)^i;
and after that you can made x in binary base, i is in decimal base too
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.
satya
2010-08-21 00:37:23
nice one