Related Articles

Related Articles

Set the Left most unset bit
  • Difficulty Level : Medium
  • Last Updated : 18 Apr, 2018

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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 
?>

chevron_right



Output:

14

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :