Skip to content

Commit

Permalink
Create CheckPowerOf2.py
Browse files Browse the repository at this point in the history
Given an integer, check if it is a power of 2 or not
  • Loading branch information
vardaan-raj committed Oct 1, 2022
1 parent 93b3613 commit baddc37
Showing 1 changed file with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#Question : 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 == 2x.

def isPowerOfTwo(self, n: int) -> bool:
if n<0:
return False
if str(bin(n)).count('1')==1:
return True
return False

0 comments on commit baddc37

Please sign in to comment.