
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to check a number is power of two or not in Python
Suppose we have a number n. We have to check whether this is power of 2 or not.
So, if the input is like n = 2048, then the output will be True as 2048 is 2^11.
To solve this, we will follow these steps −
if n is same as 0, then
return False
return true when (n AND (n - 1)) is same as 0 otherwise false
Example
Let us see the following implementation to get better understanding
def solve(n): if n == 0: return False return (n & (n - 1)) == 0 n = 2048 print(solve(n))
Input
2048
Output
True
- Related Articles
- Program to check a number is ugly number or not in Python
- Check if a number is power of 8 or not in C++
- Python program to check if a number is Prime or not
- Python program to check a number n is weird or not
- Program to check whether given number is Narcissistic number or not in Python
- Program to check whether every rotation of a number is prime or not in Python
- Program to check two rectangular overlaps or not in Python
- Python program to check credit card number is valid or not
- Program to check a number is palindrome or not without help of a string in Python
- Program to check two parts of a string are palindrome or not in Python
- Program to check whether a number is Proth number or not in C++
- Swift Program to Check If a Number is Spy number or not
- Golang Program to check whether given positive number is power of 2 or not, without using any branching or loop
- Program to check a string is palindrome or not in Python
- C++ Program to Check Whether a Number is Prime or Not

Advertisements