Turnoff the rightmost set bit using 2s complement
Given a Number N. The task is to unset the rightmost set bit of N.
Examples:
Input: N = 5
Output: 4
Explanation:
101(5) -> 100(4)
Input : N = 78
Output : 76
Explanation:
1001110(78) -> 1001100(76)
Naive Approach:
- A simple approach to flip the rightmost set bit is to find the position of the rightmost set bit in the number by a bitwise right shift operation to check for the first occurrence of 1.
- Then, flip the bit in this position. Flipping can be done by applying XOR of the given number and the number with the bit in this position set.
Important property to flip bits:
0 ^ 1 = 1
1 ^ 1 = 0
Xor with 1 flips the bit.
-
- Setting only a given bit can be done by taking 2 to the power of position to set a particular bit.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void FlipBits( int n)
{
for ( int bit = 0; bit < 32; bit++)
{
if ((n >> bit) & 1)
{
n = n ^ (1ll << bit);
break ;
}
}
cout<< "The number after unsetting the" ;
cout<< " rightmost set bit " << n;
}
int main()
{
int N = 12;
FlipBits(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void FlipBits( int n)
{
for ( int bit = 0 ; bit < 32 ; bit++)
{
if ((n >> bit) % 2 > 0 )
{
n = n ^ ( 1 << bit);
break ;
}
}
System.out.print( "The number after unsetting the" );
System.out.print( " rightmost set bit " + n);
}
public static void main(String[] args)
{
int N = 12 ;
FlipBits(N);
}
}
|
Python3
def FlipBits(n):
for bit in range ( 32 ):
if ((n >> bit) & 1 ):
n = n ^ ( 1 << bit)
break
print ( "The number after unsetting the" , end = " " )
print ( "rightmost set bit" , n)
if __name__ = = '__main__' :
N = 12 ;
FlipBits(N)
|
C#
using System;
class GFG{
static void FlipBits( int n)
{
for ( int bit = 0; bit < 32; bit++)
{
if ((n >> bit) % 2 > 0)
{
n = n ^ (1 << bit);
break ;
}
}
Console.Write( "The number after unsetting the " );
Console.Write( "rightmost set bit " + n);
}
static void Main()
{
int N = 12;
FlipBits(N);
}
}
|
Javascript
<script>
function FlipBits(n)
{
for (let bit = 0; bit < 32; bit++)
{
if (((n >> bit) & 1) > 0)
{
n = n ^ (1 << bit);
break ;
}
}
document.write( "The number after unsetting the" );
document.write( " rightmost set bit " + n);
}
let N = 12;
FlipBits(N);
</script>
|
Output:
The number after unsetting the rightmost set bit 8
Time Complexity: O(32)
Auxiliary Space: O(1)
Efficient Approach:
We can flip the LSB in O(1). Though the naive approach also works fine, there also exists a one-liner solution to the problem. We can do the following:
- Obtain 2’s complement of the number (simply -n gives 2’s complement)
- Perform bitwise AND operation between number and it’s 2’s complement.
- Subtract the above result from the given number.
- The final result has LSB flipped.
Explanation
N = 1001110 (78)
2's compliment = 0110010
N & (-N) = 0000010
N - (N & (-N)) = 1001100 (76)
C++
#include <bits/stdc++.h>
using namespace std;
int FlipBits(unsigned int n)
{
return n -= (n & (-n));
}
int main()
{
int N = 12;
cout<< "The number after unsetting the" ;
cout<< " rightmost set bit: " <<FlipBits(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int FlipBits( int n)
{
return n -= (n & (-n));
}
public static void main(String[] args)
{
int N = 12 ;
System.out.print( "The number after unsetting the " );
System.out.print( "rightmost set bit: " + FlipBits(N));
}
}
|
Python3
def FlipBits(n):
n - = (n & ( - n));
return n;
if __name__ = = '__main__' :
N = 12 ;
print ( "The number after unsetting the" , end = "");
print ( " rightmost set bit: " , FlipBits(N));
|
C#
using System;
class GFG{
static int FlipBits( int n)
{
return n -= (n & (-n));
}
public static void Main(String[] args)
{
int N = 12;
Console.Write( "The number after" +
"unsetting the " );
Console.Write( "rightmost set bit: " +
FlipBits(N));
}
}
|
Javascript
<script>
function FlipBits(n)
{
return n -= (n & (-n));
}
let N = 12;
document.write( "The number after unsetting the" );
document.write( " rightmost set bit: " + FlipBits(N));
</script>
|
Output:
The number after unsetting the rightmost set bit: 8
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
28 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...