Python Program to Clear nth Bit of a Number

Given a number, bit position and the task is to get the number after clearing the bit at the given bit position for a given number.

In simple words, it converts all 1’s to 0

let number=7  (111)

Bit position that you want to clear =1

It converts the 1 at index 1 to 0

Number after clearing = 5 (101)

Bitwise & Operator:

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

Complement operator(~):

The ones’ complement operator flips the bits of an integer, transforming one into zero and zero into one.

Examples:

Example1:

Input:

Given Number = 60 
Bit position(in range 0-31)= 3

Output:

The Number after clearing the bit at the given position{ 3 } for a given number{ 60 } = 52

Example2:

Input:

Given Number = 7 
Bit position(in range 0-31)= 1

Output:

The Number after clearing the bit at the given position{ 1 } for a given number{ 7 } = 5

Program to Clear nth Bit of a Number in Python

Below are the ways to get the number after clearing the bit at the given bit position for a given number:

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

Approach:

  • Give the number as static input and store it in a variable.
  • Give the bit position as static input and store it in another variable.
  • Apply the left shift operator to 1 and the above-given bit position and store it in another variable.
  • Apply the complement operator (which converts 0 to 1 and vice-versa) to the above result and store it in another variable.
  • Apply bitwise & operation for the given number and the above result and store it in another variable say rslt_numb.
  • Print the number after clearing the bit at the given position for a given number.
  • The Exit of the Program.

Note: ‘0’ indexing .Give the bit position in range(0-31).

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 60
# Give the bit position as static input and store it in another variable.
bitpositin = 3
# Apply the left shift operator to 1 and the above-given bit position and
# store it in another variable.
numbr_bit = (1 << bitpositin)
# Apply the complement operator (which converts 0 to 1 and vice-versa) to the above result
# and store it in another variable.
complemt = (~numbr_bit)
# Apply bitwise & operation for the given number and the above result and store it in
# another variable say rslt_numb.
rslt_numb = gvn_numb & complemt
# Print the number after clearing the bit at the given position for a given number.
print("The Number after clearing the bit at the given position{",
      bitpositin, "} for a given number{", gvn_numb, "} =", rslt_numb)

Output:

The Number after clearing the bit at the given position{ 3 } for a given number{ 60 } = 52

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.
  • Give the bit position as user input using the int(input()) function and store it in another variable.
  • Apply the left shift operator to 1 and the above-given bit position and store it in another variable.
  • Apply the complement operator (which converts 0 to 1 and vice-versa) to the above result and store it in another variable.
  • Apply bitwise & operation for the given number and the above result and store it in another variable say rslt_numb.
  • Print the number after clearing the bit at the given position for a given 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 = "))
# Give the bit position as user input using the int(input()) function 
# and store it in another variable.
bitpositin = int(input("Enter some random number = "))
# Apply the left shift operator to 1 and the above-given bit position and
# store it in another variable.
numbr_bit = (1 << bitpositin)
# Apply the complement operator (which converts 0 to 1 and vice-versa) to the above result
# and store it in another variable.
complemt = (~numbr_bit)
# Apply bitwise & operation for the given number and the above result and store it in
# another variable say rslt_numb.
rslt_numb = gvn_numb & complemt
# Print the number after clearing the bit at the given position for a given number.
print("The Number after clearing the bit at the given position{",
      bitpositin, "} for a given number{", gvn_numb, "} =", rslt_numb)

Output:

Enter some random number = 7
Enter some random number = 1
The Number after clearing the bit at the given position{ 1 } for a given number{ 7 } = 5

Leave a Comment