# Convert binary to gray code in Python

In this tutorial, we will learn to **convert binary to gray code in Python**. A binary number is a number that is expressed in the base-2 numeral system. Therefore, a binary number is formed using 0’s and 1’s only. So today, we will get to know the representation of binary and gray code numbers, conversion of a binary number to gray code, and a Python program to convert binary number to gray code.

## Representation of binary number and gray code

Any binary number is represented using 0 and 1 only i.e. every binary number is a sequence of 0’s and 1’s.

For example –> *Binary number – 101110*

Gray code belongs to the class minimum change codes. It is an encoding of numbers such that there is only one bit that differs in each successive number.

For example-

- The gray code of number 6 is 010
**1**and 7 is 010**0.** - The gray code of number 10 is 111
**1**and 11 is 111**0**.

In both examples, there is only one-bit change.

## Method to convert a binary number to gray code

So now we will learn to convert a binary number to its equivalent gray code number. Remember, the number of bits in binary and its equivalent gray code will be equal always. A gray code also contains only 0’s or 1’s. To convert binary number to gray code, we need to perform *exclusive-OR (XOR)* operation. So, let us see how to perform *XOR* operation and how to convert a binary number.

*Exclusive-OR (XOR)* operation-

For the conversion of a binary number to gray code, we will follow the rules given below-

- The
*most significant bit (MSB)*of gray code is the*MSB*of the given binary number. - To find the next bit, take the
*exclusive-OR (XOR)*of*MSB*and the next bit of binary number. - Similarly, to find the next bit, take the
*XOR*of the second and third bit from*MSB*of the binary number. - In this way, find the remaining bits of the equivalent gray code.

To understand this more clearly, let us find the equivalent gray code for binary number * 10110*.

- The
*MSB*of gray code is the*MSB*of the given binary number. - To find the remaining bits of gray code, we perform
*XOR*operation as follows-

Finally, we get the equivalent gray code i.e. ** 11101**.

## Python program to convert binary number to gray code

Now, we will see a Python program to convert binary number to gray code.

def convert_gray(binary): binary = int(binary, 2) binary ^= (binary >> 1) return bin(binary)[2:] binary_num = input("BINARY NUMBER : ") gray_code = convert_gray(binary_num) print("EQUIVALENT GRAY CODE IS : ",gray_code)

In the above program-

- Firstly, we take a binary number from the user.
- We call the function
*‘convert_gray’*and pass the binary number. - The function
*‘int()’*converts the binary number to a decimal integer number as follows-

Here, the string is the binary number and the base for a binary number is 2.**int(string, base) –>** - Then, we perform the
*XOR*operation of number with another number obtained by right shifting the number by 1 bit.

The symbol**binary ^= (binary >> 1) –>***‘^’*denotes*XOR*operation.*XOR*is performed between*‘binary’*and*‘binary >> 1’*where the symbol*‘>>’*denotes the right shift and 1 denotes the right shift by 1 bit. - At last, we convert the resultant decimal number to binary string i.e. gray code.
- Finally, the function returns the equivalent gray code of the given binary number.

#### Python program output

The above Python program displays the equivalent gray code for the given binary number. So, the output after sample execution is-

siddharth@siddharth-Lenovo-Y520-15IKBN:~/python$ python3 gray.py BINARY NUMBER : 1001 EQUIVALENT GRAY CODE IS : 1101 siddharth@siddharth-Lenovo-Y520-15IKBN:~/python$

So, the gray code for binary number 1001 is 1101. Therefore, using this program, we can find the gray code for any binary number.

Thank you for reading this tutorial and I hope it helps you.

Also read: How to convert binary to decimal in Python

Hi,

I am getting an error as below:

TypeError: int() can’t convert non-string with explicit base