How to Remove the Rightmost Set Bit in a Number in Python


In this example we will show the methods to clear the rightmost set bit in a given number with Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

n = int(input("Enter a number: n = "))
crsb = n & (n - 1)
print("After clearing the rightmost set bit: ", crsb)

Output:

Enter a number: n = 8
After clearing the rightmost set bit: 0

Enter a number: n = 7
After clearing the rightmost set bit: 6
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments