Challenge - 5 Problems
Bitwise Parity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this code snippet?
This JavaScript code uses bitwise AND to check if a number is even or odd. What will it print?
DSA Javascript
const num = 7; if ((num & 1) === 0) { console.log('Even'); } else { console.log('Odd'); }
Attempts:
2 left
💡 Hint
The bitwise AND with 1 checks the least significant bit of the number.
✗ Incorrect
The number 7 in binary ends with 1, so (7 & 1) equals 1, which is not zero, so it prints 'Odd'.
❓ Predict Output
intermediateWhat does this function return for input 10?
This function uses bitwise operations to check if a number is even. What is the return value when input is 10?
DSA Javascript
function isEven(n) {
return (n & 1) === 0;
}
console.log(isEven(10));Attempts:
2 left
💡 Hint
Even numbers have their least significant bit as 0.
✗ Incorrect
10 in binary ends with 0, so (10 & 1) is 0, making the function return true.
🔧 Debug
advancedWhat error does this code produce?
This code attempts to check if a number is odd using bitwise AND. What error or output does it produce?
DSA Javascript
const num = 5; if ((num & 1) === 1) { console.log('Odd'); } else { console.log('Even'); }
Attempts:
2 left
💡 Hint
Check the operator used inside the if condition.
✗ Incorrect
The code uses assignment '=' instead of comparison '===' inside the if condition, causing a SyntaxError.
❓ Predict Output
advancedWhat is the output of this code with negative number?
This code checks if a negative number is even or odd using bitwise AND. What will it print?
DSA Javascript
const num = -4; if ((num & 1) === 0) { console.log('Even'); } else { console.log('Odd'); }
Attempts:
2 left
💡 Hint
Bitwise operations work on two's complement binary representation.
✗ Incorrect
Negative even numbers have least significant bit 0 in two's complement, so it prints 'Even'.
🧠 Conceptual
expertWhy does using bitwise AND with 1 check if a number is even or odd?
Choose the best explanation for why the expression (number & 1) === 0 determines if a number is even.
Attempts:
2 left
💡 Hint
Think about binary representation of even and odd numbers.
✗ Incorrect
In binary, even numbers end with 0, so AND with 1 yields 0; odd numbers end with 1, so AND with 1 yields 1.
