Given a positive integer n. The problem is to check whether only the first and last bits are set in the binary representation of n. The answer is 1 if the first and last bit is set else 0.
Example 1:
Input: N = 9
Output: 1
Explanation: (9)_{10} = (1001)_{2}, only 
the first and last bits are set.
â€‹Example 2:
Input: N = 15
Output: 0
Explanation: (15)_{10} = (1111)_{2}, except 
first and last there are other bits also
which are set.
Your Task:  
Your task is to complete the function onlyFirstAndLastAreSet() which takes the N as inputs and returns the answer.
Expected Time Complexity: O(log N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{9}

Difficulty: EASY
Tags: Bit manipulation

Data structures
Skill types: Bit manipulation

Data structures
Expected Time Complexity: O(log N)
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3

class Solution:
	def onlyFirstAndLastAreSet(self, n):
		# code here

Solutions:
class Solution:

	def onlyFirstAndLastAreSet(self, n):
		b = str(bin(n).replace('0b', ''))
		if b[0] == '1' and b[-1] == '1' and (b.count('1') == 2):
			return 1
		return 0


class Solution:

	def onlyFirstAndLastAreSet(self, n):
		if n > 2 and n - 1 & n - 2 == 0:
			return 1
		return 0


class Solution:

	def onlyFirstAndLastAreSet(self, n):
		if not n & 1:
			return 0
		return 1 if not n - 1 & n - 2 else 0


class Solution:

	def onlyFirstAndLastAreSet(self, n):
		a = n - 1
		if a > 1 and a & a - 1 == 0:
			return 1
		else:
			return 0


class Solution:

	def onlyFirstAndLastAreSet(self, N):
		if N - 1 & N - 2 == 0 and N > 2:
			return 1
		return 0


class Solution:

	def onlyFirstAndLastAreSet(self, n):
		num = [*bin(n)[2:]]
		if num.pop() == '0':
			return 0
		while len(num) > 1:
			if num.pop() == '1':
				return 0
		return 1


class Solution:

	def onlyFirstAndLastAreSet(self, n):
		pw = 1
		cnt = 0
		while pw <= n:
			pw *= 2
			cnt += 1
		if n == 2 ** (cnt - 1) + 1:
			return 1
		else:
			return 0


class Solution:

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


class Solution:

	def onlyFirstAndLastAreSet(self, n):
		n = n ^ 1
		x = n & n - 1
		if x == 0:
			return 1
		return 0


class Solution:

	def onlyFirstAndLastAreSet(self, n):
		s = str(bin(n))[2:]
		if s[0] == '1' and s[-1] == '1' and (s.count('1') == 2):
			return 1
		else:
			return 0


class Solution:

	def onlyFirstAndLastAreSet(self, n):
		l = bin(n)[2:]
		if l[0] == l[-1] == '1' and l.count('1') == 2:
			return 1
		return 0


class Solution:

	def onlyFirstAndLastAreSet(self, n):
		x = bin(n)[2:]
		if x[0] != '1' or x[-1] != '1':
			return 0
		count = 0
		for i in x:
			if i == '1':
				count += 1
			if count > 2:
				return 0
		return 1


class Solution:

	def onlyFirstAndLastAreSet(self, n):
		binary = bin(n).replace('0b', '')
		if binary.count('1') == 2:
			return 1
		else:
			return 0


class Solution:

	def onlyFirstAndLastAreSet(self, n):
		S = bin(n)[2:]
		if S[0] == '0' or S[-1] == '0':
			return 0
		for i in range(1, len(S) - 1):
			if S[i] == '1':
				return 0
		return 1


class Solution:

	def onlyFirstAndLastAreSet(self, n):
		b = bin(n)[2:]
		t = b[0]
		m = b[-1]
		r = b[1:-1]
		if t == '1' and m == '1' and (set(r) == {'0'}):
			return 1
		elif t == '1' and m == '1' and (len(r) == 0):
			return 1
		else:
			return 0


class Solution:

	def onlyFirstAndLastAreSet(self, n):
		p = bin(n).replace('0b', '')
		l = list(p)
		d = len(l)
		if l[0] == '1' and l[d - 1] == '1' and ('1' not in l[1:d - 2]):
			return 1
		else:
			return 0


class Solution:

	def onlyFirstAndLastAreSet(self, n):
		l = []
		while n:
			l.append(n & 1)
			n >>= 1
		if len(l) == 0:
			return 0
		if l[0] != 1 or l[-1] != 1:
			return 0
		flag = True
		for i in range(1, len(l) - 1):
			if l[i] == 1:
				return 0
		return 1