Challenge - 5 Problems
Bitwise Even-Odd Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this TypeScript code using bitwise AND to check even or odd?
Consider the following TypeScript code that checks if a number is even or odd using bitwise operations. What will it print?
DSA Typescript
function checkEvenOdd(num: number): string {
return (num & 1) === 0 ? "Even" : "Odd";
}
console.log(checkEvenOdd(10));
console.log(checkEvenOdd(7));Attempts:
2 left
💡 Hint
The bitwise AND with 1 checks the least significant bit of the number.
✗ Incorrect
The expression (num & 1) checks if the last bit is 1 (odd) or 0 (even). 10 in binary ends with 0, so it's even; 7 ends with 1, so it's odd.
❓ Predict Output
intermediateWhat is the output of this code that uses bitwise XOR to toggle even/odd check?
This TypeScript function uses bitwise XOR to check if a number is even or odd. What will it print?
DSA Typescript
function isOdd(num: number): boolean {
return (num ^ 1) === num - 1;
}
console.log(isOdd(4));
console.log(isOdd(5));Attempts:
2 left
💡 Hint
XOR with 1 flips the last bit, so for even numbers it adds 1, for odd numbers it subtracts 1.
✗ Incorrect
For 4 (even), 4 ^ 1 = 5 !== 4 - 1 = 3, so returns false. For 5 (odd), 5 ^ 1 = 4 === 5 - 1 = 4, so returns true.
🔧 Debug
advancedWhat error does this TypeScript code produce when checking even or odd using bits?
This code attempts to check if a number is even or odd using bitwise AND but has a mistake. What error will it raise?
DSA Typescript
function check(num: number): string {
if (num & 1 === 0) {
return "Even";
} else {
return "Odd";
}
}
console.log(check(3));Attempts:
2 left
💡 Hint
Check the use of '=' inside the if condition.
✗ Incorrect
The code uses '=' (assignment) instead of '==' or '===' (comparison) inside the if condition, causing a syntax error.
🧠 Conceptual
advancedWhich statement about using bitwise AND to check even or odd is correct?
Select the correct statement about using the expression (num & 1) to check if a number is even or odd.
Attempts:
2 left
💡 Hint
Think about the least significant bit in binary for even and odd numbers.
✗ Incorrect
The least significant bit of even numbers is 0, so (num & 1) is 0 for even numbers and 1 for odd numbers.
🚀 Application
expertWhat is the output of this TypeScript code that filters even numbers using bitwise AND?
Given the following code that filters even numbers from an array using bitwise AND, what is the printed output?
DSA Typescript
const numbers = [1, 2, 3, 4, 5, 6]; const evens = numbers.filter(n => (n & 1) === 0); console.log(evens);
Attempts:
2 left
💡 Hint
Filter keeps numbers where the last bit is 0.
✗ Incorrect
Numbers 2, 4, and 6 have last bit 0, so they are even and included in the filtered array.
