Recall & Review
beginner
What does the expression
num & 1 check in a number?It checks the least significant bit of the number. If it is 1, the number is odd; if 0, the number is even.
Click to reveal answer
beginner
Why can we use bitwise AND with 1 to check if a number is even or odd?
Because the least significant bit (rightmost bit) of an odd number is always 1, and for even numbers, it is 0.
Click to reveal answer
beginner
What is the output of
5 & 1 and what does it mean?The output is 1, meaning 5 is odd.
Click to reveal answer
intermediate
How does checking even or odd using bits compare to using the modulus operator?
Using bits is faster because it directly checks the binary form without division, while modulus (%) involves division.
Click to reveal answer
beginner
Write a simple Python function to check if a number is even using bitwise operators.
def is_even(num):
return (num & 1) == 0
Click to reveal answer
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 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 is the output of
10 & 1?✗ Incorrect
10 in binary ends with 0, so
10 & 1 equals 0, meaning 10 is even.Which operator is used to check even or odd using bits?
✗ Incorrect
Bitwise AND (&) with 1 checks the least significant bit to determine even or odd.
Why might bitwise check for even/odd be preferred over modulus (%) in some cases?
✗ Incorrect
Bitwise operations are faster and simpler than division-based modulus operations.
Explain how to check if a number is even or odd using bitwise operators.
Think about the rightmost bit in the binary form.
You got /4 concepts.
Write a Python function that returns True if a number is odd using bitwise operations.
Use (num & 1) == 1 to check odd.
You got /3 concepts.