aboutsummaryrefslogtreecommitdiff
path: root/power_of_two.py
blob: 80dd7d02cf103fbe1b79bb1b941975e3331e25f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 231. Power of Two

"""
given an integer 'n', return true if it is a power of two. otherwise, return
false. an integer 'n' is a power of two if there exists an integer 'x' such
that 'n == 2^x'
"""


class Solution(object):
    def isPowerOfTwo(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n <= 0:
            return False
        return not (n & (n - 1))
       
if __name__ == "__main__":
    obj = Solution()
    print(obj.isPowerOfTwo(1))
    print(obj.isPowerOfTwo(16))
    print(obj.isPowerOfTwo(3))