# Python Program to Convert Binary to Gray Code

Although binary numbers are the most common way to store numbers, they can be challenging to use in many situations, necessitating the usage of a binary number variant. This is where Gray codes come in handy.

Gray code has the property that two consecutive numbers differ in just one bit. Because of this quality, grey code cycles through multiple states with low effort and is used in K-maps, error correction, communication, and so on.

We will learn how to convert binary to grey code in Python in this tutorial. A binary number is a number written in the base-2 numeral system. As a result, a binary number is made up of only 0s and 1s. So, today, we’ll learn how to represent binary and grey code numbers, how to convert a binary number to grey code, and how to use a Python programÂ to convert a binary number to grey code.

Examples:

Example1:

Input:

binary String = "1010110101"

Output:

The Gray Code of the binary string= 1010110101 = 1111101111

Example2:

Input:

binary string = "110001101010"

Output:

The Gray Code of the binary string= 110001101010 = 101001011111

## Program to Convert Binary to Gray Code in Python

Below are the ways to convert the given binary number to gray code in python:

### Method #1:Using Right shift operator and Xor (Static Input)

Approach:

• Give the binary number as static.
• The function binToGray is created.
• As an argument, it accepts a binary number as a string.
• It returns the string corresponding to its Gray codeword.
• If b(i) is the ith bit in the binary number and g(i) is the ith bit in the associated Gray codeword, where the 0th bit is the MSB, g(0) = b(0) and g(i) = b(i) XOR b(i â€“ 1) for I > 0.
• Thus, b XOR (b >> 1) can be used to convert a binary number b to its corresponding Gray codeword.
• Exit of Program.

Below is the implementation:

# function which accepts the binary string and returns the gray code of the binary string
def binToString(binStr):
# Converting the given binary string to integer
binStr = int(binStr, 2)
binStr ^= (binStr >> 1)

# Returning the binary converted string using bin() function
return bin(binStr)[2:]

# given binary number as static
binString = "1010110101"
# passing this binString to binToString function
resultGray = binToString(binString)
print('The Gray Code of the binary string=', binString, '=', resultGray)


Output:

The Gray Code of the binary string= 1010110101 = 1111101111

### Method #2:Using Right shift operator and Xor (User Input)

Approach:

• Scan the binary string using input() function.
• The function binToGray is created.
• As an argument, it accepts a binary number as a string.
• It returns the string corresponding to its Gray codeword.
• If b(i) is the ith bit in the binary number and g(i) is the ith bit in the associated Gray codeword, where the 0th bit is the MSB, g(0) = b(0) and g(i) = b(i) XOR b(i â€“ 1) for I > 0.
• Thus, b XOR (b >> 1) can be used to convert a binary number b to its corresponding Gray codeword.
• Exit of Program.

Below is the implementation:

# function which accepts the binary string and returns the gray code of the binary string
def binToString(binStr):
# Converting the given binary string to integer
binStr = int(binStr, 2)
binStr ^= (binStr >> 1)

# Returning the binary converted string using bin() function
return bin(binStr)[2:]

# given binary number as static
binString = input("Enter some random binary string = ")
# passing this binString to binToString function
resultGray = binToString(binString)
print('The Gray Code of the binary string=', binString, '=', resultGray)


Output:

Enter some random binary string = 11100010100101
The Gray Code of the binary string= 11100010100101 = 10010011110111

Related Programs: