Unset he rightmost set bit using 2s complement Last Updated : 12 Jul, 2025 Comments Improve Suggest changes 2 Likes Like Report Given The task is to unset the rightmost set bit of N.Examples: Input: N = 5 Output: 4Explanation:101(5) -> 100(4)Input : N = 78Output : 76Explanation: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. 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. N = 1001110 (78)2's compliment = 0110010 N & (-N) = 0000010N - (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> OutputThe number after unsetting the rightmost set bit: 8Time Complexity: O(1)Auxiliary Space: O(1) Comment S sarthak_eddy Follow 2 Improve S sarthak_eddy Follow 2 Improve Article Tags : DSA Explore DSA FundamentalsLogic Building Problems 2 min read Analysis of Algorithms 1 min read Data StructuresArray Data Structure 3 min read String in Data Structure 2 min read Hashing in Data Structure 2 min read Linked List Data Structure 2 min read Stack Data Structure 2 min read Queue Data Structure 2 min read Tree Data Structure 2 min read Graph Data Structure 3 min read Trie Data Structure 15+ min read AlgorithmsSearching Algorithms 2 min read Sorting Algorithms 3 min read Introduction to Recursion 14 min read Greedy Algorithms 3 min read Graph Algorithms 3 min read Dynamic Programming or DP 3 min read Bitwise Algorithms 4 min read AdvancedSegment Tree 2 min read Binary Indexed Tree or Fenwick Tree 15 min read Square Root (Sqrt) Decomposition Algorithm 15+ min read Binary Lifting 15+ min read Geometry 2 min read Interview PreparationInterview Corner 3 min read GfG160 3 min read Practice ProblemGeeksforGeeks Practice - Leading Online Coding Platform 6 min read Problem of The Day - Develop the Habit of Coding 5 min read Like