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 bit operations.
The question is: how does the time to check change as the number gets bigger?
Analyze the time complexity of the following code snippet.
bool IsEven(int number)
{
return (number & 1) == 0;
}
This code checks if the last bit of the number is 0, which means the number is even.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single bitwise AND operation.
- How many times: Exactly once per function call, no loops or recursion.
Checking the last bit 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 operation count stays the same even if the number grows larger.
Time Complexity: O(1)
This means the check takes the same amount of time no matter how big the number is.
[X] Wrong: "Checking bits takes longer for bigger numbers because they have more bits."
[OK] Correct: The operation only looks at the last bit, so it does not depend on the number size.
Knowing this quick bit check shows you understand how computers work with numbers efficiently, 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 bitwise AND with 1
Bitwise AND with 1 checks the least significant bit of the number.Step 2: Check least significant bit for even or odd
If the least significant bit is 0, the number is even; if 1, it is odd.Final Answer:
Using bitwise AND with 1 (number & 1) -> Option CQuick Check:
Least significant bit check = number & 1 [OK]
- Using bitwise OR instead of AND
- Using XOR which toggles bits
- Using NOT which flips all bits
num is odd using bits?Solution
Step 1: Understand bitwise AND check for odd
Odd numbers have least significant bit 1, so (num & 1) == 1 means odd.Step 2: Verify code logic
if ((num & 1) == 1) { Console.WriteLine("Odd"); } prints "Odd" when (num & 1) equals 1, correctly identifying odd numbers.Final Answer:
if ((num & 1) == 1) { Console.WriteLine("Odd"); } -> Option DQuick Check:
Odd check = (num & 1) == 1 [OK]
- Checking (num & 1) == 0 for odd (actually even)
- Using OR or XOR instead of AND
- Confusing equality check with assignment
int num = 14;
if ((num & 1) == 0)
Console.WriteLine("Even");
else
Console.WriteLine("Odd");Solution
Step 1: Calculate bitwise AND of 14 and 1
14 in binary is 1110; 1110 & 0001 = 0, so (num & 1) == 0 is true.Step 2: Determine output based on condition
Since condition is true, it prints "Even".Final Answer:
Even -> Option BQuick Check:
14 & 1 = 0 means Even [OK]
- Assuming 14 is odd
- Confusing bitwise AND with OR
- Expecting numeric output instead of string
int num = 7;
if (num & 1 == 1)
Console.WriteLine("Odd");
else
Console.WriteLine("Even");Solution
Step 1: Analyze operator precedence
In C#, equality (==) has higher precedence than bitwise AND (&), so 'num & 1 == 1' parses as 'num & (1 == 1)', which is 'num & 1'. This does not compare (num & 1) to 1 as intended.Step 2: Identify the error
Missing parentheses around (num & 1) causes incorrect evaluation. Correct code should be 'if ((num & 1) == 1)'.Final Answer:
Missing parentheses around (num & 1) -> Option AQuick Check:
Use parentheses to ensure correct precedence [OK]
- Misjudging precedence (thinking & > ==)
- Using bitwise OR instead of AND
- Assuming parentheses are not required
public string CheckEvenOdd(int num) {
// Options:
// A) return (num & 1) == 0 ? "Even" : "Odd";
// B) return (num | 1) == 0 ? "Even" : "Odd";
// C) return (num ^ 1) == 0 ? "Even" : "Odd";
// D) return (~num & 1) == 0 ? "Even" : "Odd";
}Solution
Step 1: Evaluate each option's logic
Option A correctly checks if least significant bit is 0 for even; others use incorrect bitwise logic.Step 2: Confirm correctness and efficiency
Option A uses simple bitwise AND and ternary operator, efficient and clear.Final Answer:
Option A -> Option AQuick Check:
Use (num & 1) == 0 for even check [OK]
- Using OR or XOR which give wrong results
- Using NOT which flips bits incorrectly
- Ignoring ternary operator for return
Ready to prove it?
Pass the test and this pattern moves unknown
