Discover how a tiny bit can save you tons of time checking numbers!
Why Check if Number is Even or Odd Using Bits in DSA Javascript?
Imagine you have a long list of numbers and you want to quickly find out which ones are even and which are odd by checking each number one by one using division and remainder operations.
Using division and remainder for every number can be slow and tiring, especially if the list is very long. It also uses more computing power than needed, making your program less efficient.
Using bits to check if a number is even or odd is like looking at the last light switch in a row to see if it's on or off. This method is super fast and uses very little power because it only checks one tiny part of the number.
function isEven(number) {
return number % 2 === 0;
}function isEven(number) {
return (number & 1) === 0;
}This lets you quickly and efficiently separate even and odd numbers, making your programs faster and more responsive.
When sorting large data sets or filtering numbers in games and apps, checking even or odd quickly helps improve performance and user experience.
Manual division is slow for large data.
Bitwise check is fast and efficient.
Useful for performance-critical tasks.
Practice
Solution
Step 1: Understand bitwise AND with 1
The least significant bit (rightmost bit) of a number determines if it is even or odd. ANDing with 1 extracts this bit.Step 2: Check the result of number & 1
If the result is 0, the number is even; if 1, it is odd.Final Answer:
AND with 1 (number & 1) -> Option DQuick Check:
Least significant bit check = AND with 1 [OK]
- Using OR instead of AND
- Using XOR which flips bits
- Using left shift which multiplies number
num is odd using bitwise operators?Solution
Step 1: Understand the bitwise AND check
Using (num & 1) extracts the last bit. If it equals 1, the number is odd.Step 2: Match expression to odd check
Expression (num & 1) === 1 returns true only for odd numbers.Final Answer:
(num & 1) === 1 -> Option CQuick Check:
Odd check = (num & 1) === 1 [OK]
- Checking for 0 instead of 1 for odd
- Using OR or XOR instead of AND
- Confusing equality check with assignment
function isEven(num) {
return (num & 1) === 0 ? 'Even' : 'Odd';
}
console.log(isEven(10));
console.log(isEven(7));Solution
Step 1: Evaluate isEven(10)
10 in binary ends with 0, so (10 & 1) === 0 is true, returns 'Even'.Step 2: Evaluate isEven(7)
7 in binary ends with 1, so (7 & 1) === 0 is false, returns 'Odd'.Final Answer:
"Even" followed by "Odd" -> Option AQuick Check:
10 is even, 7 is odd [OK]
- Mixing up output order
- Assuming bitwise AND returns boolean directly
- Confusing bitwise AND with logical AND
function checkOdd(num) {
if (num & 1 === 1) {
return 'Odd';
} else {
return 'Even';
}
}Solution
Step 1: Analyze operator precedence
Bitwise AND (&) has lower precedence than ===, so expression evaluates as num & (1 === 1).Step 2: Fix expression with parentheses
Correct expression is (num & 1) === 1 to check last bit properly.Final Answer:
Missing parentheses around (num & 1) -> Option AQuick Check:
Use parentheses to ensure correct bitwise check [OK]
- Ignoring operator precedence
- Using bitwise OR instead of AND
- Assuming bitwise AND returns boolean
const numbers = [3, 4, 7, 8, 10]; const evens = numbers.filter(num => ???); console.log(evens);
Solution
Step 1: Understand filtering even numbers
Even numbers have last bit 0, so (num & 1) === 0 is true for evens.Step 2: Apply filter condition
Using numbers.filter(num => (num & 1) === 0) keeps only even numbers.Final Answer:
(num & 1) === 0 -> Option BQuick Check:
Filter evens with (num & 1) === 0 [OK]
- Using odd check instead of even
- Using OR or XOR instead of AND
- Missing parentheses causing wrong precedence
Ready to prove it?
Pass the test and this pattern moves unknown
