Check the given number is power of 2 using BitWise
# if a number is power of 2 then n&(n-1) results 0
#lets see n=4
#in binary 4 is 100
#in binary 3 is 011
#so bitwise & = 000 =>0
def ispowerof2(n):
x=n
y=n&(n-1)
print(x and not(y))
n=int(input())
ispowerof2(n)
Comments
Post a Comment