Python Program to Check Number is a Power of Two
Last Updated :
12 Sep, 2024
we will discuss how to write a Python program to determine whether a given number is a power of two. A number is said to be a power of two if it can be expressed in the form of 2n, where n is a non-negative integer.
Examples:
Python
def is_power_of_two(n):
if n <= 0:
return False
return (n & (n - 1)) == 0
# Example Usage
number = 32
if is_power_of_two(number):
print(f"{number} is a power of two.")
else:
print(f"{number} is not a power of two.")
Output
32 is a power of two.
Check Power of Two using Iterative Division
One simple approach to check if a number is a power of two is by dividing the number repeatedly by 2 until it becomes 1. If, at any point, the number is not divisible by 2, it means the number is not a power of two.
Code Explanation:
- We start by checking if the number is less than or equal to 0. A power of two must be a positive number.
- Then, we continuously divide the number by 2 as long as it's divisible by 2.
- If we can reduce the number to 1, it means the number is a power of two; otherwise, it is not.
Python
def is_power_of_two(n):
if n <= 0:
return False
while n % 2 == 0:
n = n // 2
return n == 1
# Example Usage
number = int(input("Enter a number: "))
if is_power_of_two(number):
print(f"{number} is a power of two.")
else:
print(f"{number} is not a power of two.")
Output
Enter a number: 16
16 is a power of two.
Enter a number: 18
18 is not a power of two.
Find Whether a Number is a Power of Two using Bit Manipulation
A more efficient way to check if a number is a power of two is by leveraging the properties of binary representation. A power of two in binary has exactly one bit set to 1. For example:
- 20=12^0 = 120=1 (binary: 0001)
- 21=22^1 = 221=2 (binary: 0010)
- 22=42^2 = 422=4 (binary: 0100)
- 23=82^3 = 823=8 (binary: 1000)
If a number is a power of two, then n&(n−1)n \& (n - 1)n&(n−1) will be 0. This works because subtracting 1 from a number flips all the bits after the rightmost set bit (including the set bit itself), making the result 0 when ANDed with the original number.
Code Explanation:
- We first check if the number is greater than 0.
- Then, we apply the bitwise AND operation between the number and one less than the number. If the result is 0, the number is a power of two.
Python
def is_power_of_two(n):
return n > 0 and (n & (n - 1)) == 0
# Example Usage
number = int(input("Enter a number: "))
if is_power_of_two(number):
print(f"{number} is a power of two.")
else:
print(f"{number} is not a power of two.")
Output
Enter a number: 32
32 is a power of two.
Enter a number: 50
50 is not a power of two.
Check Power of Two using Math Library (Logarithmic Approach)
Another approach involves using logarithms to check if a number is a power of two. The logarithm base 2 of a power of two should return an integer value.
Code Explanation:
- We use
math.log2()
to compute the base-2 logarithm of the number. - If the result is an integer, it means the number is a power of two.
Python
import math
def is_power_of_two(n):
if n <= 0:
return False
return math.log2(n).is_integer()
# Example Usage
number = int(input("Enter a number: "))
if is_power_of_two(number):
print(f"{number} is a power of two.")
else:
print(f"{number} is not a power of two.")
Output
Enter a number: 64
64 is a power of two.
Enter a number: 70
70 is not a power of two.
Please refer complete article on Program to find whether a no is power of two for more details!
Similar Reads
Python Program to Check Armstrong Number Given a number x, determine whether the given number is Armstrong number or not. A positive integer of n digits is called an Armstrong number of order n (order is number of digits) if. abcd... = pow(a,n) + pow(b,n) + pow(c,n) + pow(d,n) + .... Example: Input : 153 Output : Yes 153 is an Armstrong nu
6 min read
Python Program for Check if count of divisors is even or odd Write a Python program for a given number ânâ, the task is to find its total number of divisors that are even or odd. Examples: Input : n = 10 Output: Even Input: n = 100Output: Odd Input: n = 125Output: Even Python Program for Check if count of divisors is even or odd using Naive Approach:A naive a
4 min read
Check for True or False in Python Python has built-in data types True and False. These boolean values are used to represent truth and false in logical operations, conditional statements, and expressions. In this article, we will see how we can check the value of an expression in Python.Common Ways to Check for True or FalsePython pr
2 min read
How to Check if a Given Number is Fibonacci number - Python Fibonacci numbers are part of a famous sequence where each number is the sum of the two preceding ones, i.e. F(n) = F(n-1) + F(n-2). The sequence starts as:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...Notice that every number is equal to the sum of its previous 2 numbers. In this article, we will learn how t
2 min read
Check if a given string is binary string or not - Python The task of checking whether a given string is a binary string in Python involves verifying that the string contains only the characters '0' and '1'. A binary string is one that is composed solely of these two digits and no other characters are allowed. For example, the string "101010" is a valid bi
3 min read