

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Convert Binary to Gray Code
When it is required to convert binary code to gray code, a method is defined that performs the ‘xor’ operation.
Below is the demonstration of the same −
Example
def binary_to_gray_op(n): n = int(n, 2) n ^= (n >> 1) return bin(n)[2:] gray_val = input('Enter the binary number: ') binary_val = binary_to_gray_op(gray_val) print('Gray codeword is :', binary_val)
Output
Enter the binary number: 101100110 Gray codeword is : 111010101
Explanation
A method named ‘binary_to_gray_op’ is defined, that takes the binary number as its parameter.
It performs the ‘xor’ operation.
It returns the converted output.
The input of binary number is taken from the user.
The function is called, and this value is passed as a parameter to it.
The converted output is displayed on the console.
- Related Questions & Answers
- Python Program to Convert Gray Code to Binary
- 8085 program to convert gray to binary
- Conversion of Binary to Gray Code
- Conversion of Gray Code to Binary
- 8085 program to convert binary numbers to gray
- Binary to Gray code using recursion in C program
- Program to convert gray code for a given number in python
- 8086 program to convert binary to Grey code
- 8085 code to convert binary number to ASCII code
- What is Gray code?
- Gray Code in C++
- Python program to convert floating to binary
- Python program to convert decimal to binary number
- Convert decimal to binary number in Python program
- Python Program to Generate Gray Codes using Recursion
Advertisements