How to check if a number is a power of 2
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Understanding whether a number is a power of two is an important aspect in fields such as computer science, mathematics, and electrical engineering. This concept is particularly relevant when working with memory allocation, data structures, and algorithms. Powers of two are numbers that can be expressed as 2^n for some non-negative integer n.
In this article, we will explore methods to determine if a given number is a power of two, provide technical explanations with examples, and summarize key points in a table.
Binary Representation of Powers of Two
A binary system simplifies the concept of powers of two. In binary notation, a power of two is represented as a single '1' followed by zero or more '0's.
For example:
- (Binary:
1) - (Binary:
10) - (Binary:
100) - (Binary:
1000)
The pattern shows that only one bit is set in a binary representation if a number is a power of two.
Methods to Check if a Number is a Power of Two
Method 1: Iterative Division
One of the simplest methods involves continuously dividing the number by 2.
- If the number reduces to 1, then it is a power of two.
- If it cannot be divided evenly by 2, it is not a power of two.
Algorithm:
- Start with the number
n. - If
nis less than or equal to 0, return false. - Divide
nby 2 repeatedly. - If
nbecomes 1, return true. - If
ncannot be divided evenly, return false.
Example:
Let's check if 16 is a power of two.
- 16 / 2 = 8
- 8 / 2 = 4
- 4 / 2 = 2
- 2 / 2 = 1 (Reached 1)
So, 16 is a power of two.
Method 2: Bitwise AND Operator
A more efficient way to check involves using the bitwise AND operator. The expression n & (n-1) evaluates to 0 if n is a power of two.
Explanation:
If n is a power of two:
- In binary, it has only one set bit.
- Subtracting 1 from it flips all the bits to the right of the set bit, including the set bit.
Thus, n & (n-1) will be 0.
Example:
Check if 32 is a power of two.
- Binary of 32:
100000 - Binary of 31:
011111 - 32 & 31 =
100000&011111=000000
So, 32 is a power of two.
Method 3: Using Logarithms
Another approach is utilizing logarithms.
- Compute the logarithm base 2 of the number.
- If the result is an integer, then the number is a power of two.
Explanation:
If is an integer, then n can be represented as ``$2^k$for some integerk`.
Example:
Calculate if 64 is a power of two using logarithms.
- , which is an integer.
Therefore, 64 is a power of two.
Table of Methods
Below is a summary of methods to check if a number is a power of two:
| Method | Description | Implementation Insight |
| Iterative Division | Continuously divide by 2 until 1 or non-divisible | Simple but may be slow for large numbers |
| Bitwise AND Operator | Use n & (n-1) which results in 0 if a power of two | Efficient, leveraging binary properties |
| Logarithms | Calculate if is an integer | Uses mathematical properties, requires logarithmic computation |
Conclusion
Each method offers unique advantages depending on the use case requirements such as efficiency, simplicity, or computational resources. Understanding these methods enhances problem-solving skills in array manipulations, optimizations, and memory management tasks in programming. Happily exploring numbers as powers of two can lead to more robust and optimized code!
By understanding and employing these solutions, you'll be thoroughly prepared to tackle programming challenges and optimization tasks that involve powers of two.
Related reading
- How to check if a number is a power of 2
- How to check if an integer is a power of 3?
- How to check if two permutations are symmetric?
- How to check if two words are anagrams
- How to check if a point is inside an ellipsoid?
- How to check if a point lies on a line between 2 other points
- How to check/find if an item is in a DEQUE
- How to checkout old git commit including all submodules recursively?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.