{"id":9049,"date":"2021-06-17T18:44:02","date_gmt":"2021-06-17T13:14:02","guid":{"rendered":"https:\/\/python-programs.com\/?p=9049"},"modified":"2021-11-22T18:38:35","modified_gmt":"2021-11-22T13:08:35","slug":"python-program-to-convert-binary-to-gray-code","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-convert-binary-to-gray-code\/","title":{"rendered":"Python Program to Convert Binary to Gray Code"},"content":{"rendered":"
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.<\/p>\n
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.<\/p>\n
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\u00a0to convert a binary number to grey code.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to convert the given binary number to gray code in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\nbinary String = \"1010110101\"<\/pre>\n
The Gray Code of the binary string= 1010110101 = 1111101111<\/pre>\n
binary string = \"110001101010\"<\/pre>\n
The Gray Code of the binary string= 110001101010 = 101001011111<\/pre>\n
Program to Convert Binary to Gray Code in Python<\/h2>\n
\n
Method #1:Using Right shift operator and Xor (Static Input)<\/h3>\n
\n
# function which accepts the binary string and returns the gray code of the binary string\r\ndef binToString(binStr):\r\n # Converting the given binary string to integer\r\n binStr = int(binStr, 2)\r\n binStr ^= (binStr >> 1)\r\n\r\n # Returning the binary converted string using bin() function\r\n return bin(binStr)[2:]\r\n\r\n\r\n# given binary number as static\r\nbinString = \"1010110101\"\r\n# passing this binString to binToString function\r\nresultGray = binToString(binString)\r\nprint('The Gray Code of the binary string=', binString, '=', resultGray)\r\n<\/pre>\n
The Gray Code of the binary string= 1010110101 = 1111101111<\/pre>\n
Method #2:Using Right shift operator and Xor (User Input)<\/h3>\n
\n
# function which accepts the binary string and returns the gray code of the binary string\r\ndef binToString(binStr):\r\n # Converting the given binary string to integer\r\n binStr = int(binStr, 2)\r\n binStr ^= (binStr >> 1)\r\n\r\n # Returning the binary converted string using bin() function\r\n return bin(binStr)[2:]\r\n\r\n\r\n# given binary number as static\r\nbinString = input(\"Enter some random binary string = \")\r\n# passing this binString to binToString function\r\nresultGray = binToString(binString)\r\nprint('The Gray Code of the binary string=', binString, '=', resultGray)\r\n<\/pre>\n
Enter some random binary string = 11100010100101\r\nThe Gray Code of the binary string= 11100010100101 = 10010011110111<\/pre>\n
\n