Python Basic
Python Interview Programs
- Python Interview Programs
- Python Abundant Number
- Python Amicable Number
- Python Armstrong Number
- Python Average of N Numbers
- Python Automorphic Number
- Python Biggest of three numbers
- Python Binary to Decimal
- Python Common Divisors
- Python Composite Number
- Python Condense a Number
- Python Cube Number
- Python Decimal to Binary
- Python Decimal to Octal
- Python Disarium Number
- Python Even Number
- Python Evil Number
- Python Factorial of a Number
- Python Fibonacci Series
- Python GCD
- Python Happy Number
- Python Harshad Number
- Python LCM
- Python Leap Year
- Python Magic Number
- Python Matrix Addition
- Python Matrix Division
- Python Matrix Multiplication
- Python Matrix Subtraction
- Python Matrix Transpose
- Python Maximum Value of an Array
- Python Minimum Value of an Array
- Python Multiplication Table
- Python Natural Number
- Python Number Combination
- Python Odd Number
- Python Palindrome Number
- Python Pascalβs Triangle
- Python Perfect Number
- Python Perfect Square
- Python Power of 2
- Python Power of 3
- Python Pronic Number
- Python Prime Factor
- Python Prime Number
- Python Smith Number
- Python Strong Number
- Python Sum of Array
- Python Sum of Digits
- Python Swap Two Numbers
- Python Triangular Number
Python Program to Check Power of 2
Photo Credit to CodeToFun
π Introduction
In the realm of programming, efficiency and optimization are key considerations. One common optimization involves determining whether a given number is a power of 2.
Checking if a number is a power of 2 is a fundamental operation with applications in various domains.
In this tutorial, we'll explore a Python program that efficiently checks whether a given number is a power of 2.
π Example
Let's delve into the Python code that accomplishes this functionality.
# 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 a single set bit.
# Using bitwise AND operation to check this condition.
return num > 0 and (num & (num - 1)) == 0
# Driver program
if __name__ == "__main__":
# Replace this value with your desired number
number = 16
# Check if the number is a power of 2
if is_power_of_two(number):
print(f"{number} is a power of 2.")
else:
print(f"{number} is not a power of 2.")
π» Testing the Program
To test the program with different numbers, simply replace the value of number in the if __name__ == "__main__": block.
16 is a power of 2.
Run the script to see whether the given number is a power of 2.
π§ How the Program Works
- The program defines a function is_power_of_two that takes an integer as input and returns True if the number is a power of 2 and False otherwise.
- Inside the function, it uses a bitwise AND operation to check if the number has a single set bit, which is a characteristic of power-of-2 numbers.
- The if __name__ == "__main__": block demonstrates the usage of the is_power_of_two function by checking if a given number is a power of 2.
π Between the Given Range
Let's dive into the python code that performs the check for powers of 2 in the range 1 to 20.
# Function to check if a number is a power of 2
def is_power_of_two(num):
return (num & (num - 1)) == 0
# Display powers of 2 in the range from 1 to 20
def display_powers_of_two():
print("Power of 2 in the range 1 to 20:")
for i in range(1, 21):
if is_power_of_two(i):
print(i, end=" ")
# Call the function to display powers of 2
display_powers_of_two()
π» Testing the Program
Power of 2 in the range 1 to 20: 1 2 4 8 16
Run the script to see the powers of 2 in the specified range.
π§ How the Program Works
- The program defines a function is_power_of_two that checks whether a given number is a power of 2 using bitwise operations.
- The display_powers_of_two function iterates through numbers from 1 to 20 and prints those that are powers of 2.
π§ Understanding the Concept of Power of 2
A number is considered a power of 2 if and only if it has a single set bit.
For example, 2, 4, 8, and 16 are powers of 2 because their binary representations have only one bit set.
Understanding this concept is crucial for efficient programming and algorithm design.
π’ Optimizing the Program
The provided program is a straightforward implementation. However, there are more advanced techniques for checking if a number is a power of 2, such as using logarithmic operations or counting the number of set bits. Depending on your specific use case, you might explore these optimizations.
Feel free to incorporate and modify this code as needed for your specific requirements. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Python Program to Check Power of 2), please comment here. I will help you immediately.