Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to find whether a no is power of two
In this article, we will learn how to check whether a given number is a power of two. A number is a power of two if it can be expressed as 2^n where n is a non-negative integer (e.g., 1, 2, 4, 8, 16, 32...).
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 three approaches as discussed below.
Using Division Method
This approach repeatedly divides the number by 2 until we reach 1 or find that the number is not divisible by 2 ?
# Check if number is power of 2 using division
def is_power_of_two_division(n):
if n <= 0:
return False
while n != 1:
if n % 2 != 0:
return False
n = n // 2
return True
# Test with different numbers
test_numbers = [1, 2, 4, 8, 16, 98, 64, 100]
for num in test_numbers:
if is_power_of_two_division(num):
print(f"{num} is a power of 2")
else:
print(f"{num} is not a power of 2")
1 is a power of 2 2 is a power of 2 4 is a power of 2 8 is a power of 2 16 is a power of 2 98 is not a power of 2 64 is a power of 2 100 is not a power of 2
Using Bitwise AND Operation
This approach uses the property that a power of 2 has only one bit set in its binary representation. For any power of 2, n & (n-1) equals 0 ?
# Check if number is power of 2 using bitwise operation
def is_power_of_two_bitwise(n):
# n must be positive and n & (n-1) should be 0
return n > 0 and (n & (n - 1)) == 0
# Test with different numbers
test_numbers = [1, 2, 4, 8, 16, 98, 64, 100]
for num in test_numbers:
if is_power_of_two_bitwise(num):
print(f"{num} is a power of 2")
else:
print(f"{num} is not a power of 2")
1 is a power of 2 2 is a power of 2 4 is a power of 2 8 is a power of 2 16 is a power of 2 98 is not a power of 2 64 is a power of 2 100 is not a power of 2
Using Logarithm Method
This approach calculates log?(n) and checks if the result is an integer ?
import math
# Check if number is power of 2 using logarithm
def is_power_of_two_log(n):
if n <= 0:
return False
# Calculate log base 2
log_result = math.log2(n)
# Check if log result is an integer
return log_result == int(log_result)
# Test with different numbers
test_numbers = [1, 2, 4, 8, 16, 98, 64, 100]
for num in test_numbers:
if is_power_of_two_log(num):
print(f"{num} is a power of 2")
else:
print(f"{num} is not a power of 2")
1 is a power of 2 2 is a power of 2 4 is a power of 2 8 is a power of 2 16 is a power of 2 98 is not a power of 2 64 is a power of 2 100 is not a power of 2
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Division | O(log n) | O(1) | Easy to understand |
| Bitwise AND | O(1) | O(1) | Most efficient |
| Logarithm | O(1) | O(1) | Mathematical approach |
Conclusion
The bitwise AND method is the most efficient approach with O(1) time complexity. Use the division method for better readability, and the logarithm method when working with mathematical computations.
