Program to Check whether a given number is Power of 2

Introduction to Check Power of 2
In this article, we will discuss how to write a Python program to check whether a given number is power of 2 or not. Checking if a number is a power of 2 is a common mathematical operation, and Python makes it easy to implement.
The concept is straightforward: a number is considered a power of 2 if it can be expressed as 2^x, where x is a non-negative integer. Recognizing powers of 2 is not only a mathematical curiosity but also a practical necessity in many programming scenarios.
Understanding the Logic
A number is considered a power of 2 if it can be expressed as 2^x, where x is a non-negative integer. To determine if a number is a power of 2, we can use a simple bitwise operation. Any power of 2 in binary form has only one bit set to 1.
- For example, 2^3 is 8, which is 0b1000 in binary, with only one bit set to 1.
Examples
Input : n = 64
Output : Yes
Explanation: 26 = 4
Input : n = 32
Output : Yes
Explanation: 25 = 32
Program to Check whether a given number is Power of 2
Here’s a code snippet :
# Function to check if a number is a power of 2 def is_power_of_two(num): # A number is a power of 2 if and only if it has only one bit set in its binary representation. # Using bitwise operations, we can check if the number has only one '1' bit. return num > 0 and (num & (num - 1)) == 0 # Get the input number from the user try: number = int(input("Enter a number: ")) if is_power_of_two(number): print(str(number) + " is a power of 2.") else: print(str(number) + " is not a power of 2.") except ValueError: print("Invalid input. Please enter a valid integer.")
Output :
Enter a number: 8 8 is a power of 2.
Explanation :
The Python program checks if a given number is a power of 2. It uses bitwise operations to determine if the number has only one ‘1’ bit in its binary representation, which is a characteristic of powers of 2. The program accepts user input, validates it, and then prints whether the number is a power of 2 or not, converting numbers to strings for output without using ‘f’ notation for string formatting.
Time & Space Complexity :
Operation | Time Complexity | Space Complexity |
---|---|---|
Check if number is power of 2 | O(1) | O(1) |
Input reading | O(1) | O(1) |
Overall Program | O(1) | O(1) |
How it Works
The core of the logic remains the same as the previous version. The “is_power_of_two” function checks if the number is greater than 0 and whether it has only one ‘1’ bit in its binary representation. We display the results by converting the numbers to strings for concatenation.
- Understanding Powers of 2 in Binary
- A power of 2 always has a single 1 in its binary form.
- 1 → 0001
- 2 → 0010
- 4 → 0100
- 8 → 1000
- Any number that is a power of 2 will satisfy the condition:
num & (num – 1) == 0
because subtracting 1 flips all bits after the first 1 and turns that first 1 into 0, resulting in no overlapping 1 bits.
- A power of 2 always has a single 1 in its binary form.
- Bitwise Operation Logic
- Example:
- num = 8 → 1000 (binary)
- num – 1 = 7 → 0111 (binary)
- 1000 & 0111 = 0000 → equals 0, so it’s a power of 2.
- This operation runs in O(1) time.
- Example:
- Checking Positive Values
The condition num > 0 ensures negative numbers and zero are excluded, because they are not powers of 2.
- User Input & Error Handling
The try/except block catches invalid inputs (e.g., strings or special characters) and shows a clear error message without crashing.
- Displaying Results
If the condition is true, it prints that the number is a power of 2; otherwise, it states it is not.
To wrap it up:
In this tutorial, we’ve explored a Python program to check whether a given number is a power of 2. This program serves as a valuable tool in a programmer’s arsenal, offering an elegant solution to a common mathematical problem. It can be applied in a wide range of scenarios, from optimizing algorithms to enhancing the efficiency of data structures.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
FAQs
You can use the condition num > 0 and (num & (num – 1)) == 0 which checks if only one bit is set in the binary representation of the number.
For powers of 2, the binary representation has only one bit set, so subtracting 1 flips all bits after it, making the bitwise AND zero.
The time complexity is O(1) because bitwise operations take constant time regardless of the number’s size.
Yes, by repeatedly dividing the number by 2 and checking if it becomes 1, though this takes O(log n) time.
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment