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.

0

Find first set bit :- 

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. LPU GeeksCodes Geeks


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

JAVA
//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){
int pos=1;

if(n==0)
return 0;
while(n>0){

if((n&1)==1){
return pos;
}
pos++;
n=n>>1;
}

return pos;
}
}

C++
class Solution
{
public:
//Function to find position of first set bit in the given number.
unsigned int getFirstSetBit(int n)
{
// Your code here
if(n&1 == 1)
return 1;
unsigned int count = 1;
while(n>0){
n >>= 1;
count++;
if(n&1 == 1){
return count;
}
}

}
};

Python
class Solution:

#Function to find position of first set bit in the given number.
def getFirstSetBit(self,n):
#Your code here
for i in range(n):
if((1<<i)&n):
return i+1
return 0

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !