Unset he rightmost set bit using 2s complement Last Updated : 13 Sep, 2024 Summarize Comments Improve Suggest changes Share Like Article 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 More infoAdvertise with us Next Article Turn off the rightmost set bit S sarthak_eddy Follow Improve Article Tags : DSA Similar Reads Turn off the rightmost set bit Given an integer n, turn remove turn off the rightmost set bit in it. Input: 12Output: 8Explanation : Binary representation of 12 is 00...01100. If we turn of the rightmost set bit, we get 00...01000 which is binary representation of 8Input: 7 Output: 6 Explanation : Binary representation for 7 is 0 7 min read Set the rightmost unset bit Given a non-negative number n. The problem is to set the rightmost unset bit in the binary representation of n. If there are no unset bits, then just leave the number as it is. Examples: Input : 21 Output : 23 (21)10 = (10101)2 Rightmost unset bit is at position 2(from right) as highlighted in the b 10 min read Set the rightmost unset bit Given a non-negative number n. The problem is to set the rightmost unset bit in the binary representation of nExamples: Input : 21Output : 23(21)10 = (10101)2Rightmost unset bit is at position 2(from right) as highlighted in the binary representation of 21.(23)10 = (10111)2The bit at position 2 has 5 min read 2's complement for a given string using XOR Given a binary string, task is to convert this string in to two's complement with the help of XOR operator. Examples: Input : 00000101Output :11111011 Input : 10010Output : 01110 We have discussed an approach in previous post to find 2's complement For 2âs complement, we first find oneâs complement. 6 min read Set the Left most unset bit Given an integer, set the leftmost unset bit. Leftmost unset bit is the first unset bit after most significant set bit. If all bits (after most significant set bit) are set, then return the number. Examples: Input : 10 Output : 14 10 = 1 0 1 0 // 10 binary 14 = 1 1 1 0 // after set left most unset b 5 min read Efficient method for 2's complement of a binary string Given a Binary Number as string, print its 2's complements.2âs complement of a binary number is 1 added to the 1âs complement of the binary number. Note that 1's complement is simply flip of given binary number. Examples: 2's complement of "0111" is "1001" 2's complement of "1100" is "0100" We stron 6 min read Like