Given a number N and a bit number K, check if K^{th} index bit of N is set or not. A bit is called set if it is 1. Position of set bit '1' should be indexed starting with 0 from LSB side in binary representation of the number.
Note: Index is starting from 0.
Example 1:
Input: N = 4, K = 0
Output: No
Explanation: Binary representation of 4 is 100, 
in which 0^{th} index bit from LSB is not set. 
So, return false.
Example 2:
Input: N = 4, K = 2
Output: Yes
Explanation: Binary representation of 4 is 100, 
in which 2^{nd} index bit from LSB is set. 
So, return true.
Example 3:
Input: N = 500, K = 3
Output: No
Explanation: Binary representation of 500 is 
111110100, in which 3rd index bit from LSB is not set. 
So, return false.
Your task:
You don't have to read input or print anything. Your task is to complete the function checkKthbit that takes n and k as parameters and returns either true (if kth bit is set) or false(if kth bit is not set).
Expected Time Complexity: O(1).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{9}
0 ≤ K ≤ floor(log_{2}(N) + 1)

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3





class Solution:

    

    #Function to check if Kth bit is set or not.

    def checkKthBit(self, n,k):

        #Your code here

Solutions:
class Solution:

	def checkKthBit(self, n, k):
		return True if n >> k & 1 == 1 else False


class Solution:

	def checkKthBit(self, n, k):
		li = []
		while n:
			mod = n % 2
			n = n // 2
			li.append(mod)
		if k > len(li):
			return False
		if not li[k]:
			return False
		return True


class Solution:

	def checkKthBit(self, n, k):
		return n & 1 << k


class Solution:

	def checkKthBit(self, n, k):
		b = bin(n)[2:]
		b = b[::-1]
		t = list(b)
		if t[k] != '1':
			return False
		return True


class Solution:

	def checkKthBit(self, n, k):
		sh = 1 << k
		return sh & n != 0


class Solution:

	def checkKthBit(self, n, k):
		while k > 0:
			k -= 1
			n >>= 1
		if n & 1:
			return True
		return False


class Solution:

	def checkKthBit(self, n, k):
		if n == 0:
			return False
		t = bin(n)[2:]
		q = t[::-1]
		s = str(q)
		if s[k] == '1':
			return True
		return False


class Solution:

	def checkKthBit(self, n, k):
		binary_number = '{0:b}'.format(n)
		if binary_number[-k - 1] == '1':
			return 1
		else:
			return 0


class Solution:

	def checkKthBit(self, n, k):
		n = bin(n).replace('0b', '')
		if n[-k - 1] == '1':
			return True
		else:
			return False


class Solution:

	def checkKthBit(self, n, k):
		a = str(bin(n)[2:])
		a = a[::-1]
		n = len(a)
		if a[k] == '1':
			return True
		else:
			return False


class Solution:

	def checkKthBit(self, n, k):
		return (n >> k) % 2


class Solution:

	def checkKthBit(self, n, k):
		mask = 1 << k
		res = n & mask
		return res


class Solution(object):

	def checkKthBit(self, num, k):
		return num & 1 << k


class Solution:

	def checkKthBit(self, n, k):
		a = list(str(bin(n)))
		a.reverse()
		if a[k] == '0':
			return 0
		else:
			return 1


class Solution:

	def checkKthBit(self, n, k):
		res = n >> k
		b = bin(res)
		b = b[2:]
		b = int(b)
		r = b % 10
		return r


class Solution:

	def checkKthBit(self, n, k):
		if n & 1 << k:
			return 1
		else:
			return 0


class Solution:

	def checkKthBit(self, n, k):
		s = str(bin(n))
		t = s[2:]
		m = t[::-1]
		if m[k] == '1':
			return True
		else:
			return False


class Solution:

	def checkKthBit(self, n, k):
		mask = 1 << k
		return n & mask != 0


class Solution:

	def checkKthBit(self, n, k):
		b = bin(n)[2:]
		i = len(b) - 1
		count = 0
		while i >= 0:
			if count == k:
				if b[i] == '1':
					return True
				else:
					return False
			count += 1
			i -= 1
		if count < k:
			return -1


class Solution:

	def checkKthBit(self, n, k):
		binary_n = bin(n).replace('0b', '')
		bit = binary_n[-k - 1]
		if bit == '0':
			return False
		return True


class Solution:

	def checkKthBit(self, n, k):
		bit = n & 1 << k
		if bit:
			return True


class Solution:

	def checkKthBit(self, n, k):
		res = -1
		while k != -1:
			res = n % 2
			n //= 2
			k -= 1
		return res


class Solution:

	def checkKthBit(self, n, k):
		arrr = []

		def contobin(n):
			if n >= 1:
				contobin(n // 2)
				arrr.append(n % 2)
			return arrr
		contobin(n)
		if arrr[len(arrr) - k - 1] == 1:
			return True
		else:
			return False


class Solution:

	def checkKthBit(self, n, k):
		b = str(format(n, 'b'))
		b = b[::-1]
		if b[k] == '0':
			return False
		else:
			return True


class Solution:

	def checkKthBit(self, n, k):
		binn = bin(n)[2:][::-1]
		if binn[k] == '1':
			return True
		return False


class Solution:

	def checkKthBit(self, n, k):
		b = []
		while n:
			if n % 2 == 0:
				b.append('0')
			else:
				b.append('1')
			n = n // 2
		if b[k] == '1':
			return True
		else:
			return False


class Solution:

	def checkKthBit(self, n, k):
		string = ''
		while n > 0:
			r = n % 2
			string += str(r)
			n = n // 2
		if string[k] == '1':
			return True
		return False


class Solution:

	def checkKthBit(self, n, k):
		i = 0
		while n > 0:
			d = n % 2
			if k == i:
				if d == 1:
					return True
				else:
					return False
			i = i + 1
			n = n // 2
		return False


class Solution:

	def checkKthBit(self, n, k):
		ans = ''
		while n:
			ans += str(n % 2)
			n = n // 2
		if ans[k] == '1':
			return True
		else:
			return False


class Solution:

	def checkKthBit(self, n, k):
		mask = 1 << k
		ans = n & mask
		if ans != 0:
			return True
		return False


class Solution:

	def checkKthBit(self, n, k):
		for i in range(k):
			n = n // 2
		if n % 2 == 1:
			return True
		else:
			return False


class Solution:

	def checkKthBit(self, n, k):
		mask = 1 << k
		boolset = n & mask
		return boolset


class Solution:

	def checkKthBit(self, n, k):
		s = bin(n).replace('0b', ' ')
		s = str(s)
		s = s[::-1]
		if s[k] == '1':
			return True
		return False


class Solution:

	def checkKthBit(self, n, k):
		if n >> k & 1 != 0:
			return 'Yes'