- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Clear the Rightmost Set Bit of a Number
When it is required to clear the rightmost bit of a number which was previously set, the ‘&’ operator can be used.
Below is the demonstration of the same −
Example
def clear_right_bit(my_val): return my_val & (my_val-1) n_val = 6 print("The vlaue of n is :") print(n_val) print("The number after unsetting the rightmost set bit is ") print(clear_right_bit(n_val))
Output
The vlaue of n is : 6 The number after unsetting the rightmost set bit is 4
Explanation
A method is defined that takes an integer as a parameter.
It computes the ‘&’ operation between the number and the number decremented by 1.
Outside the method, an integer is defined, and the method is called by passing the parameter.
The output is displayed on the console.
- Related Articles
- Golang Program to find the position of the rightmost set bit
- Position of rightmost set bit in C++
- Clear/Set a specific bit of a number in Arduino
- Position of rightmost different bit in C++
- Program to find number of bit 1 in the given number in Python
- How do you set, clear, and toggle a bit in C/C++?
- 8085 program to find the set bit of accumulator
- Position of rightmost common bit in two numbers in C++
- set clear() in python
- 8085 Program to Divide a 16-bit number by an 8-bit number
- 8086 program to divide a 16 bit number by an 8 bit number
- Golang program to check if k’th bit is set for a given number or not.
- 8085 program to find square of a 8 bit number
- Find most significant set bit of a number in C++
- Clear a bit in a BigInteger in Java

Advertisements