Find first set bit | Practice | GeeksforGeeks

Given an integer an N. The task is to return the position of first set bit found from the right side in the binary representation of the number.
Note: If there is no set bit in the integer N, then return 0 from the function.  
Example 1:
Input: N = 18
Output: 2
Explanation: Binary representation of 
18 is 010010,the first set bit from the 
right side is at position 2.
Example 2:
Input: N = 12 
Output: 3 
Explanation: Binary representation 
of  12 is 1100, the first set bit 
from the right side is at position 3.
Your Task:
The task is to complete the function getFirstSetBit() that takes an integer n as a parameter and returns the position of first set bit.
Expected Time Complexity: O(log N).
Expected Auxiliary Space: O(1).
Constraints:
0 <= N <= 108
+1
proudtobe72719 hours ago
unsigned int getFirstSetBit(int n)
   {
       unsigned int c=0;
       while(n)
       {
           ++c;
           if(n%2==1)	break;
           n=n/2;
       }
       return c;
   }
0
kiran1931 day ago
unsigned int getFirstSetBit(int n)   {       for(int i=0;i<=log2(n);i++)           if((1<<i)&n)return i+1;       return 0;   }
0
kailashkolluru6 days ago
    unsigned int getFirstSetBit(int n)
    {
        // Your code here
        if(n%2 == 1) return 1;
        if(n ==0) return 0;
        int pos=0;
        while(n%2 == 0){
            n >>= 1;
            pos +=1;
        }
        pos +=1;
        return pos;
    }
0
rahuljamui70841 week ago
 unsigned int getFirstSetBit(int n){
        return log2(n&-n)+1;
    }
0
kaalthesuraj1 week ago
Easy 3 lines : 
    unsigned int getFirstSetBit(int n)
    {
        bitset<32> bits(n);
        for(int i=0;i<32;i++)   if(bits[i]==1) return i+1;
        return 0;
    }
0
amandeep278031 week ago
class Solution
{
    public:
    //Function to find position of first set bit in the given number.
    unsigned int getFirstSetBit(int n)
    {
        if(n==0)
            return 0;
        unsigned int setbit = 0;
        for(unsigned int i=0;i<32;i++)
        {
            unsigned int mask = 1<<i;
            if((mask&n)!=0)
            {
                setbit = i;
                break;
            }
        }
        return setbit+1;
    }
};
0
ss7181 week ago
class Solution
{
    //Function to find position of first set bit in the given number.
    public static int getFirstSetBit(int n){
            
        // Your code here
        if (n == 0) {
            return 0;
        }
        // n - 1 has the rightmost set bit reversed and the    //0's after it converted to 1.
        int p = n & (~(n - 1));
        int pow = (int)(Math.log(p)/ Math.log(2)) + 1;
        return pow;
            
    }
}
0
arobindosuklabaidya1 week ago
Simple C++ Solution
   unsigned int getFirstSetBit(int n)   {       int pos=0;       if(n==0){           return 0;       }       for(int i=0;i<32;i++){           if((n&(1<<i))!=0){               pos=i;               break;           }       }       return pos+1;   }
0
manmittiwade1241 week ago
class Solution{   public:   //Function to find position of first set bit in the given number.   unsigned int getFirstSetBit(int n)   {      return ffs(n);            }               //      //Your code here         };
0
pranavkumar123842 weeks ago
// java solution//User function Template for Java
class Solution{   //Function to find position of first set bit in the given number.   public static int getFirstSetBit(int n){                  // Your code here      if(n==0) return 0;      int pos=0;      while(n>0 &&(n&1)==0){          n=n>>1;          pos++;      }            return ++pos;      }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.