

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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
<p>When it is required to clear the rightmost bit of a number which was previously set, the ‘&’ operator can be used.</p><p>Below is the demonstration of the same −</p><h2>Example</h2><p><a class="demo" href="http://tpcg.io/ZgqzPGwA" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate">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))</pre><h2>Output</h2><pre class="result notranslate">The vlaue of n is : 6 The number after unsetting the rightmost set bit is 4</pre><h2>Explanation</h2><ul class="list"><li><p>A method is defined that takes an integer as a parameter.</p></li><li><p>It computes the ‘&’ operation between the number and the number decremented by 1.</p></li><li><p>Outside the method, an integer is defined, and the method is called by passing the parameter.</p></li><li><p>The output is displayed on the console.</p></li></ul>
- Related Questions & Answers
- 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
- set clear() in python
- 8085 program to find the set bit of accumulator
- Position of rightmost common bit in two numbers in C++
- How do you set, clear, and toggle a bit in C/C++?
- 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
- Position of the K-th set bit in a number in C++
- Clear a bit in a BigInteger in Java
- Find most significant set bit of a number in C++
- 8085 program to find square of a 8 bit number
Advertisements