Set the Left most unset bit Last Updated : 28 May, 2022 Comments Improve Suggest changes Like Article Like Report 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 bit Input : 15 Output : 15 15 = 1 1 1 1 // 15 binary 15 = 1 1 1 1 // because all bits are set Approach:- 1. Return the number if all bits are set. 2. Traverse all the bit to get the last unset bit. 3. Take OR with the original number and the unset bit. Below is the implementation of the approach. C++ // C++ program to set the leftmost unset bit #include <iostream> using namespace std; // set left most unset bit int setleftmostunsetbit(int n) { // if number contain all // 1 then return n if ((n & (n + 1)) == 0) return n; // Find position of leftmost unset bit. int pos = 0; for (int temp=n, count=0; temp>0; temp>>=1, count++) // if temp L.S.B is zero // then unset bit pos is // change if ((temp & 1) == 0) pos = count; // return OR of number and // unset bit pos return (n | (1 << (pos))); } // Driver Function int main() { int n = 10; cout << setleftmostunsetbit(n); return 0; } Java // Java program to set // the leftmost unset bit import java.io.*; class GFG { // set left most unset bit static int setleftmostunsetbit(int n) { // if number contain all // 1 then return n if ((n & (n + 1)) == 0) return n; // Find position of leftmost unset bit. int pos = 0; for (int temp = n, count = 0; temp > 0; temp >>= 1, count++) // if temp L.S.B is zero // then unset bit pos is // change if ((temp & 1) == 0) pos = count; // return OR of number and // unset bit pos return (n | (1 << (pos))); } // Driver Function public static void main (String[] args) { int n = 10; System.out.println(setleftmostunsetbit(n)); } } // This code is contributed by Ansu Kumari Python3 # Python program to set the leftmost unset bit # Set left most unset bit def setleftmostunsetbit(n): # if number contain all # 1 then return n if not (n & (n + 1)): return n # Find position of leftmost unset bit pos, temp, count = 0, n, 0 while temp: # if temp L.S.B is zero # then unset bit pos is # change if not (temp & 1): pos = count count += 1; temp>>=1 # return OR of number and # unset bit pos return (n | (1 << (pos))) # Driver Function n = 10 print(setleftmostunsetbit(n)) # This code is contributed by Ansu Kumari C# // C# program to set // the leftmost unset bit using System; class GFG { // set left most unset bit static int setleftmostunsetbit(int n) { // if number contain all // 1 then return n if ((n & (n + 1)) == 0) return n; // Find position of leftmost unset bit. int pos = 0; for (int temp = n, count = 0; temp > 0; temp >>= 1, count++) // if temp L.S.B is zero // then unset bit pos is // change if ((temp & 1) == 0) pos = count; // return OR of number and // unset bit pos return (n | (1 << (pos))); } // Driver Function public static void Main () { int n = 10; Console.WriteLine(setleftmostunsetbit(n)); } } // This code is contributed by vt_m PHP <?php // php program to set the // leftmost unset bit // set left most unset bit function setleftmostunsetbit($n) { // if number contain all // 1 then return n if (($n & ($n + 1)) == 0) return $n; // Find position of leftmost // unset bit. $pos = 0; for ($temp = $n, $count = 0; $temp > 0; $temp >>= 1, $count++) // if temp L.S.B is zero // then unset bit pos is // change if (($temp & 1) == 0) $pos = $count; // return OR of number // and unset bit pos return ($n | (1 << ($pos))); } // Driver code $n = 10; echo setleftmostunsetbit($n); //This code is contributed by mits ?> JavaScript <script> // Javascript program to set the // leftmost unset bit // Set left most unset bit function setleftmostunsetbit(n) { // If number contain all // 1 then return n if ((n & (n + 1)) == 0) return n; // Find position of leftmost unset bit. let pos = 0; for(let temp = n, count = 0; temp > 0; temp >>= 1, count++) // If temp L.S.B is zero // then unset bit pos is // change if ((temp & 1) == 0) pos = count; // Return OR of number and // unset bit pos return (n | (1 << (pos))); } // Driver Code let n = 10; document.write(setleftmostunsetbit(n)); // This code is contributed by Manoj. </script> Output: 14 Time Complexity: O(log2n) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Unset the last m bits D devanshuagarwal Follow Improve Article Tags : Misc Bit Magic DSA Practice Tags : Bit MagicMisc Similar Reads 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 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 Unset the last m bits Given a non-negative number n. The problem is to unset the last m bits in the binary representation of n.Constraint: 1 <= m <= num, where num is the number of bits in the binary representation of n. Examples: Input : n = 10, m = 2 Output : 8 (10)10 = (1010)2 (8)10 = (1000)2 The last two bits i 6 min read 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 Get the position of rightmost unset bit Given a non-negative number n. Find the position of rightmost unset bit in the binary representation of n, considering the last bit at position 1, 2nd last bit at position 2 and so on. If no 0's are there in the binary representation of n. then print "-1".Examples: Input : n = 9 Output : 2 (9)10 = ( 5 min read Like