Discover a tiny trick that saves you from slow, boring number checks!
Why Check if Number is Power of Two in DSA Rust?
Imagine you have a big list of numbers and you want to find which ones are powers of two, like 1, 2, 4, 8, 16, and so on. Doing this by hand means checking each number by dividing it repeatedly or comparing it to a list of powers. This takes a lot of time and can easily cause mistakes.
Manually dividing numbers or using loops to check if a number is a power of two is slow and error-prone. It wastes time and can cause bugs if you forget edge cases like zero or negative numbers. It's like counting each grain of rice one by one instead of using a quick trick.
Using a simple bit trick, we can quickly check if a number is a power of two by looking at its binary form. If a number is a power of two, it has exactly one '1' bit in its binary representation. This method is fast, clean, and avoids mistakes.
fn is_power_of_two(mut n: u32) -> bool {
if n == 0 {
return false;
}
while n % 2 == 0 {
n /= 2;
}
n == 1
}fn is_power_of_two(n: u32) -> bool {
n != 0 && (n & (n - 1)) == 0
}This lets you instantly check if a number is a power of two, enabling fast decisions in programs like memory allocation, graphics, and algorithms.
When your computer allocates memory, it often uses sizes that are powers of two for speed and efficiency. Quickly checking if a size is a power of two helps the system run smoothly.
Manual checks are slow and error-prone.
Bitwise trick makes checking fast and simple.
Useful in many computer systems and algorithms.
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: Analyze the options
It has exactly one bit set to 1 in its binary form. matches this property. Options B, C, and D are incorrect because powers of two can be even, are not necessarily divisible by 3, and do not have all bits set to 1.Final Answer:
It has exactly one bit set to 1 in its binary form. -> Option CQuick 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
Solution
Step 1: Recall the bitwise trick for power of two
The expression `n & (n - 1) == 0` is true only if `n` is a power of two (and n > 0).Step 2: Check each option
n & (n - 1) == 0 matches the known correct bitwise check. Options A, C, and D use incorrect bitwise operators or operands.Final Answer:
n & (n - 1) == 0 -> Option AQuick Check:
Bitwise AND with n-1 zero means power of two [OK]
- Using OR or XOR instead of AND
- Using n + 1 instead of n - 1
- Not considering n must be positive
fn main() {
let n = 16;
let result = n > 0 && (n & (n - 1)) == 0;
println!("{}", result);
}Solution
Step 1: Evaluate the condition for n = 16
16 in binary is 10000. 16 - 1 = 15 (binary 01111). Bitwise AND: 10000 & 01111 = 0.Step 2: Check the full expression
Since n > 0 is true and bitwise AND is 0, the expression evaluates to true.Final Answer:
true -> Option AQuick Check:
16 is power of two, so output is true [OK]
- Ignoring the n > 0 check
- Miscomputing bitwise AND
- Confusing true/false output
fn is_power_of_two(n: u32) -> bool {
n & (n - 1) == 0
}Solution
Step 1: Analyze the function logic
The expression `n & (n - 1) == 0` returns true for zero, which is not a power of two.Step 2: Identify missing condition
We must add `n > 0` to exclude zero. Options B, C, and D are incorrect because the operator and syntax are correct, and return type bool is appropriate.Final Answer:
Missing check for n > 0 to avoid zero being true -> Option DQuick Check:
Check n > 0 to exclude zero [OK]
- Not excluding zero as power of two
- Changing correct bitwise operator
- Wrong return type assumption
let numbers = vec![3, 4, 6, 8, 16, 18];
let powers: Vec<u32> = numbers.iter()
.filter(|&&x| /* condition */)
.cloned()
.collect();
What should replace /* condition */?Solution
Step 1: Understand filtering condition for power of two
The condition must check that x is positive and that x & (x - 1) == 0 to confirm power of two.Step 2: Evaluate each option
x > 0 && (x & (x - 1)) == 0 correctly combines both checks with AND. x > 0 || (x & (x - 1)) == 0 uses OR which is incorrect. Options A and D use wrong conditions allowing zero or incorrect bitwise operations.Final Answer:
x > 0 && (x & (x - 1)) == 0 -> Option BQuick Check:
Filter positive numbers with bitwise AND zero test [OK]
- Using OR instead of AND in condition
- Allowing zero as power of two
- Using incorrect bitwise operations
Ready to prove it?
Pass the test and this pattern moves unknown
