Recall & Review
beginner Click to reveal answer
What does the expression
num & 1 check in a number?It checks the least significant bit of
num. If it is 1, the number is odd; if 0, the number is even.beginner Click to reveal answer
Why can we use bitwise AND with 1 to determine if a number is even or odd?
Because the least significant bit (rightmost bit) of an integer in binary is 1 if the number is odd and 0 if even.
beginner Click to reveal answer
Write a simple JavaScript function to check if a number is even using bitwise operators.
function isEven(num) { return (num & 1) === 0; }beginner Click to reveal answer
What is the output of
5 & 1 and what does it mean?5 & 1 equals 1, meaning 5 is odd.intermediate Click to reveal answer
Can this bitwise method be used for negative numbers to check even or odd? Why or why not?
Yes, because the least significant bit still indicates odd or even regardless of sign in two's complement representation.
What does the bitwise expression
num & 1 return for an even number?✗ Incorrect
For even numbers, the least significant bit is 0, so
num & 1 returns 0.Which bit of a binary number determines if it is even or odd?
✗ Incorrect
The least significant bit (rightmost bit) determines if a number is even (0) or odd (1).
What will
isEven(10) return if isEven uses return (num & 1) === 0;?✗ Incorrect
10 is even, so
10 & 1 is 0, making the function return true.What is the output of
7 & 1?✗ Incorrect
7 in binary ends with 1, so
7 & 1 returns 1 indicating odd.Is the bitwise method for checking even or odd faster than using modulus (%) operator?
✗ Incorrect
Bitwise operations are low-level and generally faster than modulus operations.
Explain how to check if a number is even or odd using bitwise operators.
Think about the rightmost bit in binary.
You got /4 concepts.
Write a JavaScript function using bitwise operators to return 'Even' or 'Odd' for a given number.
Use (num & 1) to decide.
You got /3 concepts.
