Discover a simple trick that turns a slow, boring task into a lightning-fast check!
Why Check if Number is Power of Two in DSA C++?
Imagine you have a list of numbers and you want to find out which ones are powers of two, like 1, 2, 4, 8, 16, and so on. Doing this by hand means dividing each number by 2 repeatedly and checking if you end up with 1. This can take a lot of time if the list is long.
Manually dividing numbers by 2 over and over is slow and easy to mess up. It's hard to keep track of all the divisions, especially for big numbers. This makes the process tiring and error-prone.
Using a simple trick with binary numbers, we can quickly check if a number is a power of two. This method uses a fast operation that computers do easily, making the check instant and reliable.
bool isPowerOfTwo(int number) {
if (number <= 0) return false;
while (number % 2 == 0) {
number /= 2;
}
return number == 1;
}bool isPowerOfTwo(int number) {
return number > 0 && (number & (number - 1)) == 0;
}This lets you instantly know if a number is a power of two, enabling faster decisions in programs that need this check often.
In computer graphics, textures often need to have sizes that are powers of two for better performance. Quickly checking this helps the program run smoothly.
Manual division to check powers of two is slow and error-prone.
Bitwise operations provide a fast and simple solution.
This check is useful in many computer applications like graphics and memory management.
Practice
Solution
Step 1: Understand binary representation of powers of two
A power of two in binary has exactly one '1' bit and the rest are '0's, e.g., 4 is 100.Step 2: Check other options
Odd numbers can have many bits set; divisibility by 3 is unrelated; all bits set to 1 means numbers like 7 (111), not powers of two.Final Answer:
It has exactly one bit set to 1 in its binary form. -> Option BQuick Check:
Power of two = single 1 bit [OK]
- Thinking powers of two are always odd
- Confusing all bits set to 1 with power of two
- Assuming divisibility by 3 means power of two
n is a power of two?Solution
Step 1: Understand bitwise check for power of two
The expression(n & (n - 1)) == 0is true only ifnhas exactly one bit set, andn > 0ensures positive numbers.Step 2: Analyze other options
(n | (n - 1)) == 0 uses OR which doesn't check power of two; (n & (n + 1)) == 0 usesn + 1which is incorrect; (n ^ (n - 1)) == 0 uses XOR which is unrelated.Final Answer:
(n & (n - 1)) == 0 && n > 0 -> Option CQuick Check:
Bitwise AND with n-1 zero means power of two [OK]
- Using OR or XOR instead of AND
- Not checking if n is positive
- Using n + 1 instead of n - 1
int n = 16;
if ((n & (n - 1)) == 0 && n > 0) {
std::cout << "Power of Two";
} else {
std::cout << "Not Power of Two";
}Solution
Step 1: Evaluate the expression for n = 16
16 in binary is 10000; 16 - 1 = 15 (01111). Bitwise AND: 10000 & 01111 = 0, condition true.Step 2: Check if n > 0
16 > 0 is true, so whole condition is true and "Power of Two" is printed.Final Answer:
Power of Two -> Option AQuick Check:
16 & 15 == 0 and 16 > 0 [OK]
- Miscomputing bitwise AND
- Ignoring the n > 0 check
- Confusing output strings
n is a power of two, but it has a bug. What is the bug?
bool isPowerOfTwo(int n) {
return (n & (n - 1)) == 0;
}Solution
Step 1: Analyze the expression without positivity check
The expression returns true for n=0 because 0 & -1 = 0, which is incorrect since 0 is not a power of two.Step 2: Identify missing condition
Addingn > 0fixes the bug by excluding zero and negative numbers.Final Answer:
It does not check if n is positive, so zero or negative numbers return true incorrectly. -> Option AQuick Check:
Check n > 0 to avoid zero/negative false positives [OK]
- Ignoring zero and negative inputs
- Using OR instead of AND
- Wrong return type assumptions
n. Which approach correctly achieves this in C++?Solution
Step 1: Understand the problem
We need the smallest power of two >= n, so we start from 1 and keep doubling (shifting left) until we reach or pass n.Step 2: Analyze options
Use a loop to shift 1 left until the value is >= n. uses a loop with left shift which correctly finds the smallest power of two >= n. Options B, C, D use bitwise operations that do not produce this result.Final Answer:
Use a loop to shift 1 left until the value is >= n. -> Option DQuick Check:
Shift 1 left until >= n to find smallest power of two [OK]
- Using bitwise AND/OR/XOR incorrectly for this problem
- Not handling cases where n is already a power of two
- Assuming bitwise ops alone give next power of two
Ready to prove it?
Pass the test and this pattern moves unknown
