Check if Number is Even or Odd Using Bits in DSA Rust - Time & Space Complexity
We want to understand how fast the bit check for even or odd numbers runs.
How does the time taken change as the number size grows?
Analyze the time complexity of the following code snippet.
fn is_even(num: u32) -> bool {
(num & 1) == 0
}
fn is_odd(num: u32) -> bool {
(num & 1) == 1
}
This code uses a bitwise AND to check the last bit of the number to decide if it is even or odd.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Single bitwise AND operation.
- How many times: Exactly once per function call.
The operation always takes the same time no matter how big the number is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays constant regardless of input size.
Time Complexity: O(1)
This means the check takes the same small amount of time no matter how large the number is.
[X] Wrong: "Checking bits takes longer for bigger numbers because they have more bits."
[OK] Correct: The code only looks at the last bit once, so the size of the number does not affect the time.
Knowing this quick bit check shows you understand how computers use bits efficiently, a skill that helps in many coding problems.
"What if we checked multiple bits or all bits to find something else? How would the time complexity change?"
Practice
num & 1 check when determining if a number is even or odd?Solution
Step 1: Understand bitwise AND operation
The expressionnum & 1performs a bitwise AND between the number and 1, which isolates the least significant bit (rightmost bit).Step 2: Interpret the least significant bit
If the least significant bit is 1, the number is odd; if it is 0, the number is even.Final Answer:
It checks the least significant bit of the number -> Option AQuick Check:
Least significant bit = odd/even check [OK]
- Thinking & 1 divides the number
- Confusing bitwise AND with addition
- Assuming it checks if number > 1
n is odd using bitwise operations?Solution
Step 1: Identify correct bitwise operator for odd check
The bitwise AND operator&with 1 isolates the least significant bit, which indicates oddness.Step 2: Verify condition correctness
Checking ifn & 1 == 1means the number is odd. Other operators like OR|or XOR^do not correctly check oddness.Final Answer:
if n & 1 == 1 { println!("Odd"); } -> Option DQuick Check:
Use & 1 == 1 to check odd [OK]
- Using OR or XOR instead of AND
- Comparing with 0 instead of 1
- Using bitwise operators incorrectly
let num = 14;
if num & 1 == 0 {
println!("Even");
} else {
println!("Odd");
}Solution
Step 1: Evaluate bitwise AND of 14 and 1
Binary of 14 is 1110, and 1 is 0001. 1110 & 0001 = 0.Step 2: Check condition and print output
Sincenum & 1 == 0is true, the code prints "Even".Final Answer:
Even -> Option AQuick Check:
14 & 1 = 0 means Even [OK]
- Assuming 14 is odd
- Confusing bitwise AND with arithmetic AND
- Expecting output to be the number itself
let n = 7;
if n & 1 = 1 {
println!("Odd");
} else {
println!("Even");
}Solution
Step 1: Check the if condition syntax
The condition uses single equals=which is assignment, not comparison.Step 2: Correct the comparison operator
It should use double equals==to comparen & 1with 1.Final Answer:
Using single = instead of == in if condition -> Option CQuick Check:
Use == for comparison, not = [OK]
- Confusing assignment (=) with equality (==)
- Thinking & is logical AND instead of bitwise
- Ignoring compiler error messages
nums = [2, 3, 4, 5, 6], which Rust code correctly uses bitwise operations to filter only odd numbers and print them?Solution
Step 1: Understand iteration and bitwise odd check
Iterate over references to numbers and usenum & 1 == 1to check odd numbers.Step 2: Verify correct syntax and logic
for &num in &nums { if num & 1 == 1 { println!("{}", num); } } correctly iterates withfor &num in &numsand checks oddness with bitwise AND. Other options misuse operators or iteration.Final Answer:
for &num in &nums { if num & 1 == 1 { println!("{}", num); } } -> Option BQuick Check:
Use & 1 == 1 inside loop over &nums [OK]
- Using wrong bitwise operators like | or ^
- Iterating without references causing ownership errors
- Checking with & 0 instead of & 1
Ready to prove it?
Pass the test and this pattern moves unknown
