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 find power of a number The task of finding the power of a number in Python involves calculating the result of raising a base number to an exponent. For example, if we have a base 2 and an exponent 3, the result is 2^3=8 .Using ** operatorThis is the simplest and most Pythonic way to calculate the power of a number. The **
2 min read
Python Program to Check if a Number is Odd or Even Even Numbers are exactly divisible by 2 and Odd Numbers are not exactly divisible by 2. We can use modulo operator (%) to check if the number is even or odd. For even numbers, the remainder when divided by 2 is 0, and for odd numbers, the remainder is 1.In this article, we will learn how to check if
2 min read
Python Program to Find Cube of a Number We are given a number and our task is to find the cube of this number in Python. The cube of a number is calculated by multiplying the number by itself twice. For example, if the input is 3, then the output will be 3 * 3 * 3 = 27. In this article, we will learn different ways to find the cube of a n
2 min read
Python program to check if number is palindrome (one-liner) In this article, we are given a number and we have to check whether the number is palindrome or not in one-liner code. The output will be True if it's a Palindrome number otherwise it would be False. Let's discuss how to find whether a number is palindrome or not in this article. Input1: test_number
3 min read
Python Program to Check If a Number is a Harshad Number Harshad Numbers can be divided by the sum of its digits. They are also called Niven Numbers. For instance, 18 is a Harshad Number as it can be divided by 9, the sum of its digits (8+1=9). In this article, we will discuss various approaches to determine whether the given number is a Harshad Number in
2 min read