Check if Number is Even or Odd Using Bits in DSA Typescript - Time & Space Complexity
We want to know how fast we can check if a number is even or odd using bits.
How does the time needed change when the number gets bigger?
Analyze the time complexity of the following code snippet.
function isEven(num: number): boolean {
return (num & 1) === 0;
}
const result = isEven(42);
console.log(result);
This code checks if a number is even by looking at its last bit.
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 regardless of input size.
Time Complexity: O(1)
This means the check takes the same tiny 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 always takes the same time.
Knowing this quick bit check shows you understand how computers work with numbers efficiently.
"What if we checked if a number is divisible by 4 using bits? 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 with 1
The expressionnum & 1isolates the least significant bit (rightmost bit) of the number.Step 2: Interpret the least significant bit
If the least significant bit is 1, the number is odd; if 0, it is even.Final Answer:
It checks if the least significant bit ofnumis 1, indicating odd number. -> Option BQuick Check:
num & 1= 1 means odd [OK]
- Thinking & 1 checks divisibility by 2
- Confusing bitwise AND with logical AND
- Assuming it checks if number is negative
n is even using bitwise operators?Solution
Step 1: Understand bitwise AND with 1
The expressionn & 1extracts the least significant bit ofn.Step 2: Check if least significant bit is zero
If(n & 1) === 0, the number is even; otherwise, it is odd.Final Answer:
if ((n & 1) === 0) { console.log('Even'); } else { console.log('Odd'); } -> Option AQuick Check:
Usen & 1 === 0for even check [OK]
- Using bitwise OR instead of AND
- Comparing with wrong values like 0 or 1 incorrectly
- Using XOR without understanding its effect
const numbers = [2, 3, 4, 5];
numbers.forEach(num => {
if (num & 1) {
console.log(num + ' is Odd');
} else {
console.log(num + ' is Even');
}
});Solution
Step 1: Evaluate bitwise AND for each number
For each number,num & 1checks the last bit: 2 & 1 = 0 (even), 3 & 1 = 1 (odd), 4 & 1 = 0 (even), 5 & 1 = 1 (odd).Step 2: Print corresponding output
Numbers withnum & 1 === 1print 'Odd', others print 'Even'.Final Answer:
2 is Even 3 is Odd 4 is Even 5 is Odd -> Option DQuick Check:
Bitwise check matches even/odd output [OK]
- Assuming all numbers are odd
- Confusing bitwise AND with logical AND
- Printing wrong labels for even/odd
x is odd using bits:
function isOdd(x: number): boolean {
return x & 1 === 1;
}Solution
Step 1: Check operator precedence in expression
Bitwise AND (&) has lower precedence than strict equality (===), sox & 1 === 1is evaluated asx & (1 === 1).Step 2: Understand the evaluation result
1 === 1is true, which converts to 1, so expression becomesx & 1. This returns a number, not a boolean, causing unexpected behavior.Step 3: Fix the expression with parentheses
Use parentheses:(x & 1) === 1to ensure correct evaluation order.Final Answer:
The bitwise AND operator has lower precedence than ===, causing wrong evaluation. -> Option AQuick Check:
Use parentheses: (x & 1) === 1 [OK]
- Ignoring operator precedence
- Using bitwise OR instead of AND
- Returning wrong data type
const nums = [10, 15, 22, 33, 40]; const evens = nums.filter( /* your code here */ ); console.log(evens);
Solution
Step 1: Understand filtering condition for even numbers
Even numbers have their least significant bit as 0, so(num & 1) === 0checks for even numbers.Step 2: Apply filter with correct condition
Usingnums.filter(num => (num & 1) === 0)returns only even numbers from the array.Step 3: Verify output
For[10, 15, 22, 33, 40], the filtered array is[10, 22, 40].Final Answer:
nums.filter(num => (num & 1) === 0) -> Option CQuick Check:
Filter even numbers with (num & 1) === 0 [OK]
- Using bitwise OR or XOR incorrectly
- Filtering for odd numbers instead of even
- Missing parentheses causing wrong evaluation
Ready to prove it?
Pass the test and this pattern moves unknown
