Check if Number is Even or Odd Using Bits in DSA C - Time & Space Complexity
We want to understand how fast the bit check method works to find if a number is even or odd.
How does the time needed change as the number gets bigger?
Analyze the time complexity of the following code snippet.
#include <stdio.h>
int isEven(int num) {
return (num & 1) == 0;
}
int main() {
int number = 10;
if (isEven(number)) {
printf("Even\n");
} else {
printf("Odd\n");
}
return 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: A single bitwise AND operation.
- How many times: Exactly once per function call.
The operation does not depend on the size of the input number; it always checks just one bit.
| 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 small amount of time no matter how large the number is.
[X] Wrong: "Checking if a number is even using bits takes longer for bigger numbers because the number has more bits."
[OK] Correct: The operation only looks at the last bit, so it does not matter how many bits the number has.
Knowing this quick bit check shows you understand how computers use bits, which is a handy skill for many coding problems.
"What if we checked the second last bit instead of the last bit? 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: 0 means even, 1 means odd.Step 2: Use AND operation with 1 to isolate LSB
Performing bitwise AND with 1 keeps only the LSB. If result is 0, number is even; if 1, odd.Final Answer:
Check the least significant bit using AND with 1 -> Option BQuick 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
Solution
Step 1: Understand bitwise AND with 1
Using n & 1 isolates the least significant bit. If it equals 1, the number is odd.Step 2: Check condition for odd number
The correct condition is (n & 1) == 1 to print "Odd".Final Answer:
if ((n & 1) == 1) printf("Odd"); -> Option AQuick Check:
n & 1 == 1 means odd [OK]
- Checking for == 0 to print odd (actually even)
- Using right shift instead of AND
- Using XOR incorrectly for odd check
int n = 14;
if (n & 1)
printf("Odd");
else
printf("Even");Solution
Step 1: Evaluate n & 1 for n = 14
Binary of 14 is 1110. The least significant bit is 0. So, 14 & 1 = 0.Step 2: Check the if condition
Since (n & 1) is 0 (false), the else block executes, printing "Even".Final Answer:
Even -> Option CQuick Check:
14 is even, so output is Even [OK]
- Assuming 14 is odd because it ends with 4
- Confusing bitwise AND with logical AND
- Expecting output "14" instead of even/odd
int n = 7;
if (n & 1 == 1)
printf("Odd");
else
printf("Even");Solution
Step 1: Analyze operator precedence in condition
In C, == has higher precedence than &. So expression is evaluated as n & (1 == 1).Step 2: Evaluate the expression and fix
Since (1 == 1) is true (1), expression becomes n & 1, which is correct but confusing. To avoid this, parentheses should be used: (n & 1) == 1.Final Answer:
Missing parentheses around (n & 1) -> Option AQuick Check:
Use (n & 1) == 1 to ensure correct evaluation [OK]
- Ignoring operator precedence causing wrong evaluation
- Using assignment = instead of comparison ==
- Confusing bitwise AND with logical AND
int isOdd(int n) {
// Choose one
}Solution
Step 1: Understand the goal of the function
The function should return 1 if n is odd, 0 if even, using bitwise operations.Step 2: Evaluate each option
return (n & 1) == 1 ? 1 : 0; correctly returns 1 if LSB is 1 (odd), else 0. return (n >> 1) & 1; returns the second least significant bit, incorrect. return n ^ 1; returns n XOR 1, which changes the number, not a boolean. return !(n & 1); returns 1 if even, opposite of requirement.Final Answer:
return (n & 1) == 1 ? 1 : 0; -> Option DQuick Check:
Use (n & 1) == 1 to detect odd and return 1 [OK]
- Using bit shift instead of checking LSB
- Returning modified number instead of boolean
- Negating the condition incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
