DIY Projects
Math and Arithmetic
Electronics Engineering

# How do you convert binary to gray code?

151617 ###### 2014-05-19 21:44:32

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

[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)

Step 2) Add from left to right bit by bit of your binary number:

(1+1) 001010 [1+1 = 0]

Step 3) Continue:

1(1+0)01010

-------------------------

11(0+0)1010

------------------------

110(0+1)010

-------------------------

until you get to the end

110010(1+0)

🎃
0
🤨
0
😮
0
😂
0

## Related Questions  To convert a gray code to binary1.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 bit.... and if the grey bit is 1 the 2nd binary bit is the complement of the first binary bit3.step 2 is repeated for every bit....EXAMPLE:the binary of the grey code 1110011 is 1011101  Its often done in hardware with a lookup ROM, or in software with a lookup table. The Gray Code is a type of binary code developed by a programmer named Frank Gray. Gray code is a binary numeral system that differ than normal binary code, and is used widely to detect errors in software. Converting Gray Code to Binary1). Write down the number in gray code.2). The most significant bit of the binary number is the most significant bitof the gray code.3). Add (using modulo 2) the next significant bit of the binary number to thenext significant bit of the gray coded number to obtain the next binary bit.4). Repeat step 3 till all bits of the gray coded number have been added inmodulo 2. The resultant number is the binary equivalent of the gray number.Converting Binary to Gray Code1). Write down the number in binary code.2). The most significant bit of the gray number is the most significant bitof the binary code.3). Add (using modulo 2) the next significant bit of the binary number to thenext significant bit of the binary number to obtain the next gray coded bit.4). Repeat step 3 till all bits of the binary coded number have been added inmodulo 2. The resultant number is the gray coded equivalent of the binarynumber.  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 binary and the right one is gray code.binary gray000 000001 001010 011011 010100 110101 111110 101111 100000 000 Gray Code is Reflective Binary code. One of the main disadvantages of Gray code is that it is very difficult to come up with an arithmetic logic unit to support Gray code.   The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only one bit (binary digit). The reflected binary code was originally designed to prevent spurious output from electro mechanical switches. Today, Gray codes are widely used to facilitate error correction in digital communications such as digital terrestrial television and some cable TV systems. I do not believe that is a valid binary number. All binary numbers must be divisible by 8 Gray code is where only one bit changes at a time. Binary code is where one or more bits change at a time. An example, for three bits, with binary on the left and gray on the right, with the changing bits underlined...000 000001 001010 011011 010100 110101 111110 101111 100000 000 Repeating...The advantage of binary code is that it is easy to convert to a numeric value. The advantage of gray code is that it makes very stable position digitizers, because only one bit changes at a time, resulting in uncertainty of only one bit. You can easily convert decimal to binary in the scientific calculator - for example, the scientific calculator found in Windows. In this case, type the number in decimal, then click on "binary" to convert to binary.   It can be implemented very easily .... Suppose the Binary word is X7X6X5.... X0 then the corresponding Gray code is G7G6G5....G0 where G7=X7 G6=X7 XOR X6 G5=X6 XOR X5 ..... G0=X1 XOR X0 Now implement the above algorithm When going from one number to the next in Gray Code 1 bit only ever changes, whereas with binary more than 1 bit could change. The reflected binary code, also known as Gray codeafter Frank Gray, is a binary numeral system where two successive values differ in only one bit.Here is an example of a 4-bit Gray code:0000000100110010011001110101010011001101111111101010101110011000 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 next bit in the Gray. Fix the result as the next binary bit.Step 3. Repeat step 2 until all digits are fixed.Examples (bold indicates a fixed binary bit):Gray: 0111 = 0 XOR 1 = 1 XOR 1 = 0 XOR 1 = 1Binary: 0101 (5 decimal)Gray: 1011 = 1 XOR 0 = 1 XOR 1 = 0 XOR 1 = 1Binary: 1101 (13 decimal)Gray: 1101 = 1 XOR 1 = 0 XOR 0 = 0 XOR 1 = 1Binary: 1001 (9 decimal)To convert from binary code to Gray code, proceed as follows:Step 1: Fix the most-significant bit (always the same for both):Step 2: XOR the first two binary bits. Fix the result as the next Gray bit.Step 3. Discard the first binary bit. If there are two or more binary bits remaining, goto step 2.Examples (bold indicates a fixed Gray bit):Binary: 0101 = 0, (0 XOR 1) = 1, (1 XOR 0) = 1, (0 XOR 1) = 1Gray: 0111 (decimal 5)Binary: 1101 = 1, (1 XOR 1) = 0, (1 XOR 0) = 1, (0 XOR 1) = 1Gray: 1011 (decimal 13)Binary: 1001 = 1, (1 XOR 0) = 1, (0 XOR 0) = 0, (0 XOR 1) = 1Gray: 1101 (decimal 9)[EDIT: The previous answer was wrong as it incorrectly assumed converting both ways was achieved with the same algorithm. It is not. Moreover, the algorithm that was used (Binary to Gray) used an arithmetic ADD instead of a logical XOR. Although the end result is essentially the same, ADDing two set bits forces a redundant carry bit, whereas XOR does not. Thus XOR is more efficient.] Well, with a BCD to gray code converter, of course.However as gray code deliberately has no coding glitches and BCD has multiple coding glitches, designing such a converter without introducing coding glitches in the gray code is more difficult than might be imagined. The 6 missing binary codes in BCD are also a significant issue in trying to convert to gray code, as no missed values are permitted.A coding glitch can occur at any time more than one bit of the code changes to go to the next (or previous) sequential value. You read binary code by translating it to ASCII text. Usually, computers convert binary encoded information to ASCII, Decimal code and other human-friendly forms before outputting it. Multiplexer used to direct one input to many out put. But decoder is used to convert one code form to another code (e.g. binary to gray) depends on design. Gray code is a 'reflected code', why is it named so will be illustrated soon. The advantage of Gray code over binary code is that only one bit in the code group changes when going from one number to the next. By, Ashish Kumar (Roh, Nawada, Bihar)

###### Computer ProgrammingMath and ArithmeticElectronics EngineeringIntel 8086 and 8088TechnologyDIY ProjectsComputersGlobal Positioning Systems (GPS) Copyright © 2020 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.