Program to Check Even or Odd Using Bitwise Operator

Python Program to Check Even or Odd Using Bitwise Operator

In the previous article, we have discussed Python Program to Set nth Bit of a Number

Given a Number and the task is to check if the given number is even or odd using the bitwise operator.

Bitwise & Operator:

If both bits are 1, sets each bit to 1.

Examples:

Example1:

Input:

Given Number = 251

Output:

The Number given is an Odd Number

Example2:

Input:

Given Number = 54

Output:

The Number given is an Even Number

Program to Check Even or Odd Using Bitwise Operator in Python:

Below are the ways to check if the given number is even or odd using the bitwise operator in python:

Method #1: Using Bitwise &(and) Operator (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Apply bitwise & operation for the given number and 1 and store it in another variable say evn_or_od.
  • Pass the above result to the if conditional statement.
  • If the statement is true, then print “The Number given is an Odd Number”.
  • Else print “The Number given is an Even Number”.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 251
# Apply bitwise & operation for the given number and 1 and store it in another variable
# say evn_or_od.
evn_or_od = gvn_numb & 1
# Pass the above result to the if conditional statement.
if (evn_or_od):
    # If the statement is true, then print "The Number given is an Odd Number".
    print("The Number given is an Odd Number")
else:
    # Else print "The Number given is an Even Number".
    print("The Number given is an Even Number")

Output:

The Number given is an Odd Number

Method #2: Using Bitwise &(and) Operator (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Apply bitwise & operation for the given number and 1 and store it in another variable say evn_or_od.
  • Pass the above result to the if conditional statement.
  • If the statement is true, then print “The Number given is an Odd Number”.
  • Else print “The Number given is an Even Number”.
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and 
# store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Apply bitwise & operation for the given number and 1 and store it in another variable
# say evn_or_od.
evn_or_od = gvn_numb & 1
# Pass the above result to the if conditional statement.
if (evn_or_od):
    # If the statement is true, then print "The Number given is an Odd Number".
    print("The Number given is an Odd Number")
else:
    # Else print "The Number given is an Even Number".
    print("The Number given is an Even Number")

Output:

Enter some random number = 32
The Number given is an Even Number

Grab the opportunity and utilize the Python Program Code Examples over here to prepare basic and advanced topics too with ease and clear all your doubts.