Check if Number is Even or Odd Using Bits in DSA C++ - Time & Space Complexity
We want to understand how fast we can check if a number is even or odd using bits.
How does the time to check change as the number gets bigger?
Analyze the time complexity of the following code snippet.
bool isEven(int num) {
return (num & 1) == 0;
}
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 amount of 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 even or odd takes longer for bigger numbers because the number has more digits."
[OK] Correct: The bitwise operation looks only at the last bit, so it does not depend on the size of the number.
Knowing this quick bit trick shows you understand how computers work with numbers at a low level, which is a useful skill in many coding problems.
"What if we checked if a number is divisible by 4 using bits? How would the time complexity change?"
Practice
Solution
Step 1: Understand the least significant bit (LSB)
The LSB of a binary number determines if it is even or odd. If LSB is 0, number is even; if 1, number is odd.Step 2: Use AND operation with 1 to check LSB
Performing bitwise AND with 1 isolates the LSB. Result 0 means even, 1 means odd.Final Answer:
Check the least significant bit using AND with 1 -> Option AQuick Check:
LSB & 1 = 0 (even) or 1 (odd) [OK]
- Using XOR instead of AND to check LSB
- Checking the most significant bit instead
- Using bit shift without checking bits
num is odd using bits?Solution
Step 1: Understand bitwise AND with 1
Using (num & 1) extracts the least significant bit. If it equals 1, number is odd.Step 2: Check condition for odd number
The condition (num & 1) == 1 correctly identifies odd numbers.Final Answer:
if ((num & 1) == 1) { /* odd */ } -> Option DQuick Check:
(num & 1) == 1 means odd [OK]
- Using == 0 to check odd (actually checks even)
- Using bit shift without comparison
- Using XOR incorrectly for odd check
int num = 14;
if (num & 1) {
std::cout << "Odd";
} else {
std::cout << "Even";
}Solution
Step 1: Evaluate bitwise AND of num and 1
14 in binary is 1110. The least significant bit is 0. So, 14 & 1 = 0.Step 2: Check the if condition
Since (num & 1) is 0, the else block executes, printing "Even".Final Answer:
Even -> Option BQuick Check:
14 & 1 = 0 means Even [OK]
- Assuming 14 is odd because it ends with 4
- Confusing bitwise AND with arithmetic AND
- Printing num instead of "Even" or "Odd"
int num = 7;
if (num & 1 == 1) {
std::cout << "Odd";
} else {
std::cout << "Even";
}Solution
Step 1: Analyze operator precedence in condition
In C++, == has higher precedence than &. So,num & 1 == 1is parsed asnum & (1 == 1), which isnum & 1. This is not the intended comparison.Step 2: Identify the error
Without parentheses, the expression does not explicitly compare (num & 1) to 1, which can cause confusion or unintended behavior. Parentheses are needed:(num & 1) == 1.Final Answer:
Missing parentheses around (num & 1) -> Option CQuick Check:
Operator precedence requires parentheses for clarity [OK]
- Misinterpreting operator precedence (== > &)
- Thinking parentheses are not needed
- Believing the code works correctly without parentheses
true if a number is even and false if odd, using bitwise operations. Which of the following implementations is correct?Solution
Step 1: Understand even number bit pattern
Even numbers have least significant bit 0. So (n & 1) == 0 means even.Step 2: Check each function implementation
bool isEven(int n) { return (n & 1) == 1; } returns true for odd numbers, so incorrect. bool isEven(int n) { return (n & 1) == 0; } correctly returns true for even numbers. Options C and D do not correctly check evenness.Final Answer:
bool isEven(int n) { return (n & 1) == 0; } -> Option AQuick Check:
Even if LSB is 0 [OK]
- Returning true for odd numbers by mistake
- Using bit shift incorrectly for even check
- Using XOR without understanding its effect
Ready to prove it?
Pass the test and this pattern moves unknown
