Check if Number is Power of Two in DSA C# - Time & Space Complexity
We want to know how fast we can check if a number is a power of two.
How does the time needed change as the number gets bigger?
Analyze the time complexity of the following code snippet.
public bool IsPowerOfTwo(int n) {
if (n <= 0) return false;
return (n & (n - 1)) == 0;
}
This code checks if a number is a power of two using a bitwise trick.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single bitwise AND operation and subtraction.
- How many times: Exactly once, no loops or recursion.
Since the code does only one check, the time does not grow with input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The number of operations stays the same no matter how big the number is.
Time Complexity: O(1)
This means the check takes the same amount of time regardless of the number size.
[X] Wrong: "Checking if a number is a power of two requires looping or dividing repeatedly."
[OK] Correct: This bitwise method does the check in one step without loops, making it very fast.
Knowing this quick check shows you understand efficient bitwise operations, a useful skill in many coding problems.
"What if we used a loop to divide the number by two repeatedly until it is 1? How would the time complexity change?"
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 for correctness
Odd numbers and divisibility by 3 do not define powers of two. Having 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 DQuick Check:
Power of two = one '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
For a power of two,n & (n - 1)clears the only set bit, resulting in 0. Also,nmust be positive.Step 2: Analyze other expressions
Other options use incorrect bitwise operations that do not correctly identify powers of two.Final Answer:
return (n & (n - 1)) == 0 && n > 0; -> Option CQuick Check:
Bitwise AND with n-1 equals 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; bool result = (n & (n - 1)) == 0 && n > 0; Console.WriteLine(result);
Solution
Step 1: Calculate bitwise AND of n and n-1
For n=16 (binary 10000), n-1=15 (binary 01111). Bitwise AND is 0.Step 2: Check if result is zero and n is positive
Since (n & (n - 1)) == 0 and n > 0, the expression is true.Final Answer:
True -> Option AQuick Check:
16 is power of two, so output is True [OK]
- Miscomputing bitwise AND
- Ignoring n > 0 condition
- Confusing True and False outputs
n is a power of two. What is the error?bool IsPowerOfTwo(int n) {
return (n & (n - 1)) == 0;
}Solution
Step 1: Analyze the bitwise condition
The expression (n & (n - 1)) == 0 is true for powers of two but also for zero.Step 2: Identify missing condition
Zero and negative numbers are not powers of two, so the function must check n > 0.Final Answer:
It does not check if n is positive, so zero or negative numbers return true incorrectly. -> Option BQuick Check:
Check n > 0 to avoid false positives [OK]
- Omitting n > 0 check
- Using wrong bitwise operator
- Returning wrong data type
n. Which approach correctly achieves this in C#?Solution
Step 1: Understand the problem requirement
We need the smallest power of two >= n, so start from 1 and double until reaching or exceeding n.Step 2: Analyze other options
Bitwise operations like AND, OR, XOR with n-1 do not produce the smallest power of two >= n.Final Answer:
Use a loop doubling a variable starting at 1 until it is >= n. -> Option AQuick Check:
Doubling loop finds smallest power of two >= n [OK]
- Using bitwise AND/OR/XOR incorrectly for this task
- Not handling n less than 1
- Assuming bitwise ops give next power of two
Ready to prove it?
Pass the test and this pattern moves unknown
