Given an integer, write a function to determine if it is a power of two.

Example 1:


Input: 1
Output: true 
Explanation: 20 = 1


Example 2:


Input: 16
Output: true
Explanation: 24 = 16

Example 3:


Input: 218
Output: false

Difficulty: introductory

Starter code:

class Solution:
    def isPowerOfTwo(self, n: int) -> bool:
        

Solutions:
class Solution:
     def isPowerOfTwo(self, n):
         """
         :type n: int
         :rtype: bool
         """
         if n < 0:
             return False
         
         hasOne = False
         while n > 0:
             if n & 1:
                 if hasOne == True:
                     return False
                 else:
                     hasOne = True
                     
             n = n >> 1
         
         return hasOne

class Solution:
     def isPowerOfTwo(self, n):
         """
         :type n: int
         :rtype: bool
         """
         # so let's take a look at what it means to be a power of two
         # 2^2 = 4 = 0100   | Now note when we subtract one | 2^2 - 1 = 3 = 0011 
         # 2^3 = 8 = 1000   | Now note when we subtract one | 2^3 - 1 = 7 = 0111
         # Now note if we do n AND n-1, we should always get 0 | 1000 & 0111 = 0000 = 0
         return n>0 and not (n & n-1)
         # This holds true for all powers of 2. However note if we used 0 & anything else, it would show to be a power of 2.
         # To fix this we can add another clause v & (earlier clause) or we can simply check if value is greater than 0.
         
         


class Solution:
     def isPowerOfTwo(self, n):
         """
         :type n: int
         :rtype: bool
         """
         if n == 0:
             return False
         if n < 0:
             n **= -1
 
         pow_ = True
         while n and pow_:        
             if n == 1:
                 return True
             pow_ = not(n % 2)
             n //= 2
         return pow_
 
     


class Solution:
     def isPowerOfTwo(self, n):
         if n <= 0: return False
         if n == 1: return True
         while n > 1:
             if n & 1: return False
             n >>= 1
         else: return True


class Solution:
     def isPowerOfTwo(self, n):
         """
         :type n: int
         :rtype: bool
         """
         
         return True if n & (n-1) == 0 and n != 0 else False


class Solution:
     import math
     def isPowerOfTwo(self, n):
         """
         :type n: int
         :rtype: bool
         """
         if n<1:
             return False
         b = str(bin(n))
         b = b.replace('0b','')
         b = b.replace('0','')
         
         return len(b)==1

class Solution:
     def isPowerOfTwo(self, n):
         """
         :type n: int
         :rtype: bool
         """
 #         if n == 0:
 #             return False
 #         while n % 2 == 0:
 #             n = n // 2
             
 #         if n == 1:
 #             return True
 #         return False
 
         return n > 0 and 1073741824 % n == 0


class Solution:
     def isPowerOfTwo(self, n):
         """
         :type n: int
         :rtype: bool
         """
         
         return n > 0 and n & (n - 1) == 0

class Solution:
     def isPowerOfTwo(self, n):
         return n > 0 == 2**32 % n


class Solution:
     def isPowerOfTwo(self, n):
         """
         :type n: int
         :rtype: bool
         """
         return n > 0 and not (n & (n - 1))

class Solution:
     def isPowerOfTwo(self, n):
         """
         :type n: int
         :rtype: bool
         """
         return n != 0 and not n & (n - 1)

class Solution:
     def isPowerOfTwo(self, n):
         """
         :type n: int
         :rtype: bool
         """
         
         if n < 0:
             return False
         
         if bin(n).replace('0b','').count('1') == 1:
             return True
         return False

class Solution:
     def isPowerOfTwo(self, n):
         """
         :type n: int
         :rtype: bool
         """
         return n>0 and (not n&(n-1))