Open In App

Python Program to Check Number is a Power of Two

Last Updated : 12 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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!
 


Next Article
Article Tags :

Similar Reads