Turn off the rightmost set bit
The problem involves manipulating the bits of a given number to turn off the rightmost set bit (also known as the least significant set bit). This operation is often used in low-level programming and optimization tasks. In this article, we will explore the concept of turning off the rightmost set bit in a binary representation of a number using bitwise operations.

Problem Statement and Description
Given a positive integer, the goal is to unset or turn off the rightmost set bit (1) in its binary
representation. For example, if the binary representation of a number is 1010
, the rightmost set
bit is at the second position (from the right), and the task is to turn it off to get 1000
.
Example
Consider the following examples to understand the concept:
-
For the number
10
(binary1010
): The rightmost set bit is at the second position (from the right). After turning it off, the number becomes8
. -
For the number
15
(binary1111
): The rightmost set bit is at the first position (from the right). After turning it off, the number becomes14
. -
For the number
320
(binary101000000
): The rightmost set bit is at the sixth position (from the right). After turning it off, the number becomes256
.
Idea to Solve the Problem
The idea to solve the problem involves using bitwise operations, specifically
the bitwise AND operator (&
). By performing a bitwise AND operation between the given number
and the number obtained by subtracting one from it (num - 1
), we can effectively turn off the
rightmost set bit.
Standard Pseudocode
function changeActiveRightMost(num):
print "Number :", num
// Turn off rightmost set bit
result = num & (num - 1)
print "Result :", result
Algorithm Explanation
- Print the given number for reference.
- Perform the bitwise AND operation between the given number (
num
) and the result of subtracting one from it (num - 1
). This operation effectively turns off the rightmost set bit. - Print the resulting number after turning off the rightmost set bit.
Resultant Output Explanation
The provided Java code demonstrates the solution to the "Turn off the rightmost set bit" problem on various test cases:
-
For the number
10
(binary1010
): The rightmost set bit is at the second position (from the right). After turning it off using the bitwise AND operation, the number becomes8
. -
For the number
15
(binary1111
): The rightmost set bit is at the first position (from the right). After turning it off using the bitwise AND operation, the number becomes14
. -
For the number
320
(binary101000000
): The rightmost set bit is at the sixth position (from the right). After turning it off using the bitwise AND operation, the number becomes256
.
Code Solution
-
1) Turn off the rightmost set bit in java
2) Turn off the rightmost set bit in c++
3) Turn off the rightmost set bit in c
4) Turn off the rightmost set bit in c#
5) Turn off the rightmost set bit in vb.net
6) Turn off the rightmost set bit in php
7) Turn off the rightmost set bit in node js
8) Turn off the rightmost set bit in typescript
9) Turn off the rightmost set bit in ruby
10) Turn off the rightmost set bit in scala
11) Turn off the rightmost set bit in swift
12) Turn off the rightmost set bit in kotlin
13) Turn off the rightmost set bit in python
14) Turn off the rightmost set bit in golang
Time Complexity
The time complexity of the provided algorithm is O(1) because it involves a constant number of bitwise operations regardless of the value of the given number.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment