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
0
uranmol03Premium1 hour ago
//C++ code
        int count = 1;
        if(n == 0){ return 0;}
        if(n % 2 != 0) { return 1;}
        for(int i = n; i > 1; i= i/2){
        	if((i & 1) == 1){ break;}
        	count++;
        }
        return count;
 
0
subhaduleygba6 days ago
//c++ code 
      unsigned int c=1;     while(n>0){         if(n&1){             return c;         }         c++;         n=n>>1;     }    return 0;   } 
0
atulharsh274Premium1 week ago
int count = 0;       if(n==0) return count;        while( (n&1) != 1){                n>>=1;                count++;        }        return count+1;
 
0
aditya sharan1 week ago
unsigned int getFirstSetBit(int n)
   {
       if(n==0)return 0;
       int rsbm= n &-n;
       return log2(rsbm)+1;
   }
+1
himanshukug19cs2 weeks ago
java solution
 
  if(n==0)           return 0;       int i=1;       while(n>0&&(n&1)==0){           n=n>>1;           i++;       }                   return i;
+1
shikhardubey94577568752 weeks ago
Simple  Using simple Maths .....
 unsigned int getFirstSetBit(int n)   {       int count = 0 ;      if ( n ==0 )      {          return 0 ;      }      else if ( n % 2 != 0 )      {          return 1 ;      }      else {          while ( n % 2==0)          {              n = n / 2 ;              count ++ ;          }          return count +1 ;      }   }
0
mankesh0162 weeks ago
return  log2 ( n & -n  ) + 1;
0
asifsaba5812 weeks ago
class Solution
{
    public:
    //Function to find position of first set bit in the given number.
    unsigned int getFirstSetBit(int n)
    {
        
       
        
        int count=0;
        
        while(n>0){      //1- 18>0  // 2- 9>0
           
           if((n&1)==1){ //010010 & 000001 = 000000 false //001001&000001=000001(true)
               
             count++; //1+1=2;
          
               break; //stop.
          
           }
           
           else{        
               
               count++;   //  count=1
          
              n=n>>1;    //  001001 = 9
           }
       }
        
       return count;
        
    }
};
0
harshilgupta00993 weeks ago
unsigned int getFirstSetBit(int n)
    {
        // Your code here
        if(n==0)
            return 0;
            
        int count=0;
        while(n>0){
            if(n&1==1)
                return count+1;
            count++;
            n/=2;
        }
        return 0;
    }
0
sdmrf3 weeks ago
Python Soln:
    def getFirstSetBit(self,n):
        return bin(n)[2:][::-1].find("1")+1
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.