Saturday, 14 December 2024

Day 36 : Python Program to Convert Binary to Gray Code


def binary_to_gray(binary_str):

    """

    Function to convert a binary number to its corresponding Gray Code.

    :param binary_str: The input binary string.

    :return: The converted Gray Code string.

    """

    gray_code = binary_str[0]  

    for i in range(1, len(binary_str)):

        gray_bit = str(int(binary_str[i - 1]) ^ int(binary_str[i]))

        gray_code += gray_bit  

    return gray_code

binary_input = input("Enter a binary number to convert into Gray Code: ")

if all(bit in '01' for bit in binary_input):  

     result = binary_to_gray(binary_input)

    print(f"Binary number: {binary_input}")

    print(f"Corresponding Gray Code: {result}")

else:

    print("Invalid input. Please enter a valid binary number consisting only of 0's and 1's.")

#source code --> clcoding.com 

Code Explanation:

1. What is Gray Code?

Gray Code is a binary numeral system where two successive values differ by only one bit. It is often used in digital logic to minimize errors when transitioning between states.

2. Function: binary_to_gray
This function takes a binary string as input and converts it into Gray Code using the following steps:

Parameters:

binary_str: The input binary number as a string.

Process:
The first bit of the Gray Code is always the same as the first bit of the binary number:
gray_code = binary_str[0]
For each subsequent bit, compute the XOR of the previous bit (binary_str[i-1]) and the current bit 

(binary_str[i]):
gray_bit = str(int(binary_str[i - 1]) ^ int(binary_str[i]))
int(binary_str[i - 1]) and int(binary_str[i]): Convert the binary characters to integers for the XOR operation.

^: The XOR operator in Python.
str(): Converts the resulting integer back to a string for concatenation.

Append this new bit (gray_bit) to the Gray Code:
gray_code += gray_bit
Continue until all bits are processed.

Return:
The function returns the Gray Code string.

3. Input Handling
The program asks the user to enter a binary number:
binary_input = input("Enter a binary number to convert into Gray Code: ")

Validation:
It ensures the input consists only of 0 and 1:
if all(bit in '01' for bit in binary_input):
all(bit in '01' for bit in binary_input): Checks if every character in binary_input is either '0' or '1'.

If valid:
Calls the binary_to_gray function to compute the Gray Code.
Prints both the original binary number and the computed Gray Code.

If invalid:
Prints an error message.

4. Output
When the input is valid:
Displays the binary number and its Gray Code:
Binary number: <binary_input>
Corresponding Gray Code: <result>

If the input is invalid:
Outputs:
Invalid input. Please enter a valid binary number consisting only of 0's and 1's.

 

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (41) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (225) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (925) Python Coding Challenge (343) Python Quiz (12) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses