Programming
Mathematics
Algorithms
Power of Two
Coding Tips

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.

Practice algorithms

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:

  • 20=12^0 = 1 (Binary: 1)
  • 21=22^1 = 2 (Binary: 10)
  • 22=42^2 = 4 (Binary: 100)
  • 23=82^3 = 8 (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:

  1. Start with the number n.
  2. If n is less than or equal to 0, return false.
  3. Divide n by 2 repeatedly.
  4. If n becomes 1, return true.
  5. If n cannot be divided evenly, return false.

Example:

Let's check if 16 is a power of two.

  1. 16 / 2 = 8
  2. 8 / 2 = 4
  3. 4 / 2 = 2
  4. 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 log2(n)log_2(n) 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.

  • log2(64)=6log_2(64) = 6, 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:

MethodDescriptionImplementation Insight
Iterative DivisionContinuously divide by 2 until 1 or non-divisibleSimple but may be slow for large numbers
Bitwise AND OperatorUse n & (n-1) which results in 0 if a power of twoEfficient, leveraging binary properties
LogarithmsCalculate if log2(n)log_2(n) is an integerUses 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.