What would you like to do?

How do you convert binary to gray code?

already exists.

Would you like to merge this question into it?

already exists as an alternate of this question.

Would you like to make it the primary and merge this question into it?

exists and is an alternate of .

To convert from standard binary to Gray code you need to be familiar with the logical XOR operator (eXclusive OR). The XOR operator outputs a 1 bit if either of two input bits is 1, but not both. The truth table for all possible inputs p and q is as follows:

p q output
0 0 0
0 1 1
1 0 1
1 1 0

Armed with this knowledge, we can convert from standard binary code to Gray code using the following algorithm:

Step 1: Fix the most-significant bit, MSB, which is always the same for both. If there is only one bit, then we're done. Otherwise proceed to step 2.

Step 2: XOR the first two bits of the binary code to get the next Gray bit.

Step 3: Discard the first bit of the binary code.

Step 4: If there are two or more bits left in the binary code, goto step 2, otherwise we're done.

Example: Binary = 11001010

(Fix MSB) = 11001010
(1 XOR 1 = 0) 001010

(1 XOR 0 = 1) 01010
(0 XOR 0 = 0) 1010
(0 XOR 1 = 1) 010

(1 XOR 0 = 1) 10

(0 XOR 1 = 1) 0

(1 XOR 0 = 1)

Thus: Gray = 10101111

Note that we XOR pairs of bits in the binary code from left to right until there are no pairs left. The bold bits are the fixed Gray bits reading from top to bottom.




To reverse the process, we need another algorithm, but we still use XOR:


Step 1: Fix the most-significant bit, MSB, which is always the same for both. If there is only one bit, then we're done. Otherwise proceed to step 2.

Step 2: XOR the binary bit we just set with the next Gray bit to fix the next binary bit.

Step 3: If there are more Gray bits, repeat step 2, otherwise we're done.


Example: Gray = 10101111

(Fix MSB) = 10101111
1 XOR 0 = 1
1 XOR 1 = 0
0 XOR 0 = 0
0 XOR 1 = 1
1 XOR 1 = 0
0 XOR 1 = 1
1 XOR 1 = 0

Thus: Binary= 11001010

Note that we carry the fixed bit (in bold) from the previous line into the next line and XOR with the next Gray bit to get the next fixed bit. The bold bits are the fixed binary bits read from top to bottom. We could also write this out as follows, reading left to right:

Gray = 10101111
Binary = 1 XOR 0 = 1 XOR 1 = 0 XOR 0 = 0 XOR 1 = 1 XOR 1 = 0 XOR 1 = 1 XOR 1 = 0



Previous Answer:

[Edit: The previous answer uses arithmetic ADD rather than logical XOR. Apart from anything else, it makes no logical sense for 1 ADD 1 = 0 (it is actually 10 binary, or decimal 2). But so long as you only consider the LSB (least-significant bit) the result will be the same. However 1 XOR 1 is always 0 and is therefore more efficient.]

How do you convert binary to Gray code?
Very Simple Example: Convert natural binary 11001010 to gray code.
Step 1) Copy most significant bit (on very left)
Answer: 1
Step 2) Add from left to right bit by bit of your binary number:
(1+1) 001010 [1+1 = 0]
Answer: 10
Step 3) Continue:
1(1+0)01010
Answer: 101
-------------------------
11(0+0)1010
Answer: 1010
------------------------
110(0+1)010
Answer: 10101
-------------------------
until you get to the end
110010(1+0)
Answer: 10101111 <- Answer in Gray Code
8 people found this useful
Thanks for the feedback!

What is the algorithm for converting Gillham Gray code into binary?

Gillham code is Gray code implemented as a binary-quinary combination. When a Gillham value is converted as Gray to binary in the ordinary way, the least significant 3 bits wi

How do you convert binary code?

Binary to what? Binary to Hex is easy. Binary Hex decimal 0000 =0 =0 0001 =1 =1 0010 =2 =2 0011 =3 =3 0100 =4 =4 0101 =5 =5 0110 =6 =6 0111 =7 =7 1000 =8

How do you convert gray code to binary code?

To convert from Gray code to binary code, proceed as follows: Step 1: Fix the most-significant bit (always the same for both). Step 2: XOR the most recent binary bit with the

Convert the gray code 10101111 to binary?

To convert Gray code to binary code you must be familiar with the logical XOR operator. XOR outputs a 1 bit if either of two input bits is 1, but not both. The truth table for

Write an assembly language program to convert gray code to binary?

As you did not specify, I will assume you are talking about three-bit binary-reflected gray code. The procedure for other possible codes and lengths would be similar.  

C program to convert gray code to binary code?

#include#includevoid main(){int a[10],i=0,c=0,n;printf("\\n enter the gray code");scanf("%d",&n);while(n!=0){a[i]=n%10;n/=10;i++;c++;}for(i=c-1;i>=0;i--){if(a[i]1){if(a[i-1]1)

Applications of binary to GRAY code converter?

gray code is one which changes one bit at a time but binary code is one which changes one or more bit at a time. for example three bit binary and gray code the left one is bin

How do you convert gray to binary code?

To convert a gray code to binary   1.write the the MSB bit as the MSB of the binary. 2.if the 2nd grey bit is 0,then the 2nd binary bit is the same as the first binary bi

How do you write a program to convert binary code to gray code using c?

Gray code, named after Frank Gray, a Bell Labs researcher who originally called it "reflected binary code", is used to help correct errors in digital communications. It was de

How do you write a c program to convert binary code to Gray code?

unsigned binary_to_gray (unsigned num) {   return num ^ (num >> 1);   }    unsigned gray_to_binary (unsigned num) {   /* note: assumes num is no more than 3
In Numbers

How do you convert a number into binary codes?

Convert decimal number to binary 1. Start by writing down the number as a start of a new column. 2. Fill out the rest of the column by dividing the number above itby 2. Write