Given a non-negative integer N. The task is to check if N is a power of 2. More formally, check if N can be expressed as 2^{x} for some x.
Example 1:
Input: N = 1
Output: YES
Explanation:1 is equal to 2 
raised to 0 (2^{0} = 1).
Example 2:
Input: N = 98
Output: NO
Explanation: 98 cannot be obtained
by any power of 2.
Your Task:Your task is to complete the function isPowerofTwo() which takes n as a parameter and returns true or false by checking if the given number can be represented as a power of two or not.
Expected Time Complexity:O(log N).
Expected Auxiliary Space:O(1).
Constraints:
0 ≤N ≤10^{18}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3



class Solution:

    ##Complete this function

    # Function to check if given number n is a power of two.

    def isPowerofTwo(self,n):

        ##Your code here

Solutions:
class Solution:

	def isPowerofTwo(self, n):
		if n <= 0:
			return False
		return n & n - 1 == 0


class Solution:

	def isPowerofTwo(self, n):
		if n == 1:
			return 'YES'
		b = bin(n)[2:]
		t = list(b)
		k = t.count('1')
		if k == 1:
			return True
		return False


class Solution:

	def isPowerofTwo(self, n):
		s = bin(n).replace('0b', '')
		return s.count('1') == 1


class Solution:

	def isPowerofTwo(self, n):
		c = 0
		if n == 0:
			return False
		elif n & n - 1 == 0:
			return True
		else:
			return False


class Solution:

	def isPowerofTwo(self, N):
		if N == 0:
			return False
		return N & N - 1 == 0


import math

class Solution:

	def isPowerofTwo(self, n):
		self.n = n
		b = 2
		if n == 0:
			return False
		r = math.log10(n) / math.log10(b)
		r = int(r)
		if math.pow(b, r) == n:
			return True
		else:
			return False


class Solution:

	def isPowerofTwo(self, n):
		j = 1
		while j <= n:
			if j == n:
				return True
			else:
				pass
			j = j << 1


class Solution:

	def isPowerofTwo(self, n):
		if n == 0:
			return False
		while n != 1:
			if n % 2 != 0:
				return False
			n = n // 2
		return True


class Solution:

	def isPowerofTwo(self, n):
		return n > 0 and n & n - 1 == 0


class Solution:

	def isPowerofTwo(self, n):
		k = bin(n)[2:]
		count = 0
		for i in range(len(k)):
			if k[i] == '1':
				count = count + 1
		if count == 1:
			return True
		else:
			return False


import math

class Solution:

	def isPowerofTwo(self, n):
		if n == 0:
			return False
		if n & n - 1 == 0:
			return True
		return False


class Solution:

	def isPowerofTwo(self, n):
		if bin(n).count('1') == 1:
			return True
		else:
			return False


class Solution:

	def isPowerofTwo(self, n):
		if n == 0:
			return 0
		while n > 1:
			x = n % 2
			if x == 0:
				n = n // 2
			else:
				return 0
		return 1


class Solution:

	def isPowerofTwo(self, n):
		res = bin(n).replace('0b', '')
		count_ = res.count('1')
		if count_ == 1 and res[0] == '1':
			return True
		else:
			return False


class Solution:

	def isPowerofTwo(self, n):
		if n == 1:
			return True
		else:
			a = bin(n)[2:]
			ans = 0
			for i in range(len(a)):
				if i == 0:
					if a[i] == '1':
						ans += 1
				elif a[i] == '0':
					ans += 1
			if ans == len(a):
				return True
			else:
				return False


import math

class Solution:

	def isPowerofTwo(self, n: int) -> bool:
		if n <= 0:
			return False
		return math.log2(n).is_integer()


import math

def log2(x):
	return math.log10(x) / math.log10(2)

class Solution:

	def isPowerofTwo(self, n):
		if n == 0:
			return 0
		return math.floor(log2(n)) == math.ceil(log2(n))


def ispower(n):
	if n % 2 == 1:
		return 1
	elif n == 2.0:
		return 0
	else:
		ispower(n / 2)

class Solution:

	def isPowerofTwo(self, n):
		if n == 0:
			return False
		elif n == 1:
			return True
		while n > 0:
			if n % 2 == 1:
				return False
			elif n == 2.0 or n == 1024.0 or n == 524288.0:
				return True
			else:
				n = n / 2


class Solution:

	def isPowerofTwo(self, n):
		cnt = 0
		str1 = bin(n)[2:]
		for i in str1:
			if i == '1':
				cnt += 1
		if cnt == 1:
			return 1
		else:
			return 0


class Solution:

	def isPowerofTwo(self, n):
		k = 1
		while k < n:
			k = k * 2
		if k > n:
			return False
		else:
			return True


class Solution:

	def isPowerofTwo(self, n):
		a = 1
		for i in range(0, 70):
			if a == n:
				return True
			a *= 2
		return False


class Solution:

	def isPowerofTwo(self, n):
		if n == 0:
			return 0
		return n & -n == n


class Solution:

	def isPowerofTwo(self, n):
		if n > 0 and n & n - 1 == 0:
			return True
		else:
			return False


class Solution:

	def isPowerofTwo(self, n):
		while n != 0:
			if n & 1:
				break
			n = n >> 1
		return n == 1


class Solution:

	def isPowerofTwo(self, n):
		ans = 0
		while n != 0:
			ans += n & 1
			n = n >> 1
		return ans == 1


class Solution:

	def isPowerofTwo(self, n):
		k = False
		if n == 1:
			return True
		else:
			for i in range(n):
				c = pow(2, i)
				if c == n:
					k = True
					break
				if c > n:
					break
			return k


class Solution:

	def isPowerofTwo(self, n):
		if n == 0:
			return 0
		else:
			return n & n - 1 == 0


class Solution:

	def isPowerofTwo(self, n):
		if n <= 0:
			return False
		return n & n - 1 == 0
import math

def main():
	T = int(input())
	while T > 0:
		n = int(input())
		ob = Solution()
		if ob.isPowerofTwo(n):
			print('YES')
		else:
			print('NO')


class Solution:

	def isPowerofTwo(self, n):
		if n % 2 != 0 and n > 1:
			return False
		for i in range(n):
			if 2 ** i == n:
				return True
			if 2 ** i > n:
				break
		return False


class Solution:

	def isPowerofTwo(self, n):
		return bin(n).count('1') == 1


class Solution:

	def isPowerofTwo(self, n):
		i = 0
		res = 0
		while res < n:
			res = 1 << i
			if res == n:
				return True
			i = i + 1
		else:
			return False


class Solution:

	def isPowerofTwo(self, n):
		if n == 1:
			return True
		elif n % 2 != 0 or n <= 0:
			return False
		else:
			return self.isPowerofTwo(n // 2)


class Solution:

	def isPowerofTwo(self, n):
		count = 0
		while n:
			count += n & 1
			n = n >> 1
		if count == 1:
			return True
		else:
			return False


class Solution:

	def isPowerofTwo(self, n):
		x = 1
		while x <= n:
			if x == n:
				return True
			x = x << 1
		return False