Given an integer 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 <= 10^{8}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3



class Solution:

    

    #Function to find position of first set bit in the given number.

    def getFirstSetBit(self,n):

        #Your code here

Solutions:
class Solution:

	def getFirstSetBit(self, n):
		ans = 0
		while n:
			mod = n % 2
			ans += 1
			if mod == 1:
				return ans
			n = n // 2
		return 0


class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		b = bin(n)[2:]
		b = b[::-1]
		k = list(b)
		p = k.index('1')
		return p + 1


class Solution:

	def getFirstSetBit(self, n):
		pos = 1
		while n != 0:
			if n & 1 == 1:
				return pos
			pos += 1
			n >>= 1
		return 0


class Solution:

	def getFirstSetBit(self, n):
		bi = bin(n).replace('0b', '')
		c = 0
		for i in range(len(bi) - 1, -1, -1):
			if bi[i] == '1':
				return c + 1
			c += 1
		return 0


class Solution:

	def getFirstSetBit(self, n):
		x = 1
		pos = 1
		if n == 0:
			return 0
		while x <= n:
			if n == 0:
				return False
			if n & x != 0:
				return pos
			else:
				pos = pos + 1
			x = x << 1


class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		t = bin(n)[2:]
		q = t[::-1]
		p = q.index('1')
		return p + 1


class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		b = str(bin(n).replace('0b', ''))
		b = b[::-1]
		for i in range(len(b)):
			if b[i] == '1':
				return i + 1


class Solution:

	def convert(self, n):
		return bin(n).replace('0b', '')

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		x = self.convert(n)[::-1]
		for i in range(len(x)):
			if x[i] == '1':
				return i + 1


class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return '0'
		a = bin(n)[2:][::-1]
		return a.index('1') + 1


class Solution:

	def getFirstSetBit(self, n):
		m = bin(n)
		s = str(m)
		j = 1
		for i in range(len(s) - 1, 0, -1):
			if s[i] == '1':
				return j
				break
			j += 1
		return 0


class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		return str(math.log2(n & -n) + 1)[:-2]


class Solution:

	def getFirstSetBit(self, n):
		c = 0
		if n == 0:
			return 0
		else:
			while n > 0:
				c += 1
				if n & 1 == 1:
					return c
				n = n >> 1


class Solution:

	def getFirstSetBit(self, n):
		count = 0
		if n == 0:
			return count
		while n != 0:
			count += 1
			if n & 1 == 1:
				return count
			n >>= 1


class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		i = 1
		while n > 0:
			remain = n % 2
			if remain == 1:
				return i
			i += 1
			n = n / 2


class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		s = bin(n)
		num = s[2:]
		num = num[::-1]
		c = 1
		for x in num:
			if x == '1':
				return c
			c += 1


class Solution:

	def getFirstSetBit(self, n):
		m = list(bin(n))
		m.remove('b')
		str1 = ''.join(m)
		if '1' not in str1:
			return 0
		return len(str1) - str1.rindex('1')


class Solution:

	def getFirstSetBit(self, n):
		binn = bin(n)[2:][::-1]
		for i in range(len(binn)):
			if binn[i] == '1':
				return i + 1
		return 0


class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		res = 0
		while n != 1:
			if n % 2 != 0:
				return res + 1
			res += 1
			n = n // 2
		return res + 1


class Solution:

	def getFirstSetBit(self, n):
		x = list('{0:b}'.format(n))
		c = 0
		for i in x[::-1]:
			c += 1
			if int(i) == 1:
				return c
		return 0


class Solution:

	def getFirstSetBit(self, n):
		for i in range(32):
			if n & 1 << i != 0:
				return i + 1
		return 0


class Solution:

	def getFirstSetBit(self, n):
		count = 0
		pos = 1
		if n == 0:
			return 0
		while n > 0:
			if n % 2 == 1:
				count += 1
				break
			pos = pos + 1
			n = n // 2
		return pos


class Solution:

	def getFirstSetBit(self, n):
		i = n
		d = []
		flag = 0
		while i != 0:
			d.append(i % 2)
			i = int(i / 2)
		l = len(d)
		for i in range(len(d)):
			if d[i] == 1:
				return i + 1
		if flag == 0:
			return 0


class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		t = bin(n)
		b = t.rfind('1')
		return len(t) - b


class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		x = bin(n)[2:][::-1]
		y = []
		for i in range(len(x)):
			y.append(x[i])
			if '1' in y and '0' in y:
				return y.index('1') + 1
		return 1


class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		return math.ceil(math.log2(n & -n) + 1)


class Solution:

	def getFirstSetBit(self, n):
		if n <= 0:
			return 0
		string = ''
		while n > 0:
			k = n % 2
			n = n // 2
			string += str(k)
			if k == 1:
				return len(string)
		return 0


class Solution:

	def getFirstSetBit(self, n):
		m = n
		k = ''
		k1 = []
		while m > 0:
			d = m % 2
			k = k + str(d)
			k1.append(d)
			m = m // 2
		if 1 in k1:
			return k1.index(1) + 1
		else:
			return 0


class Solution:

	def getFirstSetBit(self, n):
		k = bin(n)
		k = k[2:]
		n = len(k)
		z = 0
		for i in k[::-1]:
			z += 1
			if i == '1':
				return z
		return 0


import math

class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		else:
			return int(math.log(n & -n, 2) + 1)


class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		k = bin(n)
		s = k[2:]
		ans = s.rfind('1')
		return len(s) - ans


class Solution:

	def getFirstSetBit(self, n):
		p = 1
		while n:
			if n & 1:
				return p
			p = p + 1
			n = n >> 1
		return 0


class Solution:

	def getFirstSetBit(self, n):
		s = str(bin(n))
		s = s[::-1]
		for i in range(n):
			if s[i] == '1':
				return i + 1
		else:
			return 0


class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		i = 0
		while True:
			if n & 1 << i != 0:
				return i + 1
			i += 1


class Solution:

	def getFirstSetBit(self, n):
		nums = bin(n)
		nums = list(nums.replace('b', '0')[2:])
		nums.reverse()
		for i in range(len(nums)):
			if nums[i] == '1':
				return i + 1
		return 0


class Solution:

	def getFirstSetBit(self, n):
		b = 0
		cont = 0
		res = n
		while b != 1 and res != 0:
			b = res % 2
			res -= res // 2
			cont += 1
		return cont


class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		t = bin(n)
		p = t[2:]
		b = t.rindex('1')
		return len(t) - b


import math

class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		without_first = n & n - 1
		only_first = n ^ without_first
		return math.floor(math.log(only_first, 2)) + 1


class Solution:

	def getFirstSetBit(self, n):
		b = bin(n)
		s = str(b)
		s = s[-1::-1]
		if '1' in s:
			x = s.index('1')
			return x + 1
		else:
			return 0


class Solution:

	def getFirstSetBit(self, n):
		i = -1
		b = n.bit_length()
		for i in range(b):
			if n & 1 << i != 0:
				break
		return i + 1


class Solution:

	def getFirstSetBit(self, n):
		x = str(bin(n))
		j = 1
		for i in range(len(x) - 1, -1, -1):
			if x[i] == '1':
				return j
			j += 1
		return 0


class Solution:

	def getFirstSetBit(self, n):
		(temp, count) = (bin(n)[2:][::-1], 0)
		for i in temp:
			if i == '1':
				return count + 1
			else:
				count += 1
		return 0


class Solution:

	def getFirstSetBit(self, n):
		a = list(bin(n)[::-1])
		if '1' in a:
			a = a.index('1')
			return a + 1
		return 0


class Solution:

	def getFirstSetBit(self, n):
		a = []
		while n > 0:
			a.append(n % 2)
			n = n // 2
		for i in range(0, len(a)):
			if a[i] == 1:
				return i + 1
		return 0


import math

class Solution:

	def getFirstSetBit(self, n):
		if n <= 1:
			return n
		k = n & ~n + 1
		return int(math.log2(k)) + 1


class Solution:

	def getFirstSetBit(self, n):
		incr = 0
		while n >= 1 << incr:
			if n & 1 << incr:
				break
			incr += 1
		else:
			return 0
		return incr + 1


import math

class Solution:

	def getFirstSetBit(self, n):
		ans = 0
		while n:
			if n & 1:
				return ans + 1
			else:
				n = n >> 1
				ans += 1
		return ans


class Solution:

	def getFirstSetBit(self, n):
		one = 1
		for i in range(1, 17):
			if n & one:
				return i
			one <<= 1
		return 0


class Solution:

	def getFirstSetBit(self, n):
		b = bin(n)[2:][::-1]
		for i in range(n):
			if b[i] == '1':
				return i + 1
		return 0


import math

class Solution:

	def getFirstSetBit(self, n):
		temp = n ^ n & n - 1
		if temp == 0:
			return 0
		else:
			return int(math.log2(temp) + 1)


class Solution:

	def getFirstSetBit(self, n):
		if n == 0:
			return 0
		else:
			return str(bin(n)[2:][::-1]).index('1') + 1