Open In App

Unset he rightmost set bit using 2s complement

Last Updated : 13 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given 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)


We have discussed Different Approaches Unset the LSB. In this post, we are going to discuss a new approach using two’s complement.

  1. Obtain 2’s complement of the number (simply -n gives 2’s complement)  
  2. Perform bitwise AND operation between number and it’s 2’s complement 
  3. Subtract the above result from the given number.  
  4. The final result has LSB flipped. 
     
             N = 1001110 (78)
2's compliment = 0110010
N & (-N) = 0000010
N - (N & (-N)) = 1001100 (76)
C++
// C++ program to unset the rightmost
// set bit
#include <bits/stdc++.h> 
using namespace std; 
  
// Unsets the rightmost set bit  
// of n and returns the result  
int FlipBits(unsigned int n)  
{  
    return n -= (n & (-n)); 
}  
  
// Driver Code  
int main()  
{  
    int N = 12;  
    cout<<"The number after unsetting the";  
    cout<<" rightmost set bit: "<<FlipBits(N);  
    return 0;  
}  
Java
// Java program to unset the rightmost set bit
import java.util.*;
class GFG{ 
    
// Unsets the rightmost set bit 
// of n and returns the result 
static int FlipBits(int n) 
{ 
    return n -= (n & (-n)); 
} 
    
// Driver Code 
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)); 
} 
} 

// This code is contributed by 29AjayKumar
Python
# Python3 program to unset the rightmost set bit

# Unsets the rightmost set bit
# of n and returns the result
def FlipBits(n):
    
    n -= (n & (-n));
    return n;
    
# Driver Code
if __name__ == '__main__':
    
    N = 12;

    print("The number after unsetting the", end = "");
    print(" rightmost set bit: ", FlipBits(N));

# This code is contributed by Rohit_ranjan
C#
// C# program to unset the rightmost set bit
using System;

class GFG{ 
    
// Unsets the rightmost set bit 
// of n and returns the result 
static int FlipBits(int n) 
{ 
    return n -= (n & (-n)); 
} 
    
// Driver Code 
public static void Main(String[] args) 
{ 
    int N = 12; 
    
    Console.Write("The number after" + 
                  "unsetting the "); 
    Console.Write("rightmost set bit: " + 
                  FlipBits(N)); 
} 
} 

// This code is contributed by 29AjayKumar
JavaScript
<script>

    // Javascript program to unset the rightmost set bit
    
    // Unsets the rightmost set bit  
    // of n and returns the result  
    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)



Next Article
Practice Tags :

Similar Reads