# Python Program to find whether a no is power of two

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

In this article, we will learn about the solution to the problem statement given below.

Problem statement − We are given a number, we need to check that the number is a power of two or not.

We can solve this using two approaches as discussed below.

## Approach 1: Taking the log of the given number on base 2 to get the power

### Example

Live Demo

# power of 2
def find(n):
if (n == 0):
return False
while (n != 1):
if (n % 2 != 0):
return False
n = n // 2
return True
# Driver code
if(find(98)):
print('Yes')
else:
print('No')

### Output

No

## Approach 2: Using the logical statements

### Example

Live Demo

# power of 2
def find(x):
# if x is 0 or not
return (x and (not(x & (x - 1))) )
# Driver code
if(find(98)):
print('Yes')
else:
print('No')

### Output

No

## Conclusion

In this article, we have learned how we can check that the given number is a power of two.