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?
A-1
B1
Cnum
D0
✗ 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?
AMost significant bit
BSecond bit from right
CLeast significant bit
DMiddle bit
✗ Incorrect
The least significant bit (rightmost bit) determines if a number is even (0) or odd (1).
What is the output of 10 & 1?
A0
B10
C1
DNone
✗ 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?
ABitwise OR (|)
BBitwise AND (&)
CBitwise XOR (^)
DBitwise NOT (~)
✗ 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?
AIt is faster and uses simpler operations
BIt is easier to read
CIt uses less memory
DIt works only for positive numbers
✗ 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.
Practice
(1/5)
1. What does the expression num & 1 check when determining if a number is even or odd?
easy
A. It checks if the least significant bit of num is 1, indicating odd number.
B. It checks if the number is greater than 1.
C. It shifts the bits of num to the right by 1.
D. It adds 1 to the number.
Solution
Step 1: Understand bitwise AND operation
The expression num & 1 performs a bitwise AND between num and 1, which isolates the least significant bit (rightmost bit) of num.
Step 2: Interpret the least significant bit
If the least significant bit is 1, the number is odd; if it is 0, the number is even.
Final Answer:
It checks if the least significant bit of num is 1, indicating odd number. -> Option A
Quick Check:
num & 1 = 1 means odd [OK]
Hint: Check last bit with & 1; 1 means odd, 0 means even [OK]
Common Mistakes:
Thinking & 1 shifts bits instead of checking last bit
Confusing bitwise AND with addition
Assuming & 1 checks if number > 1
2. Which of the following Python code snippets correctly checks if a number n is even using bitwise operators?
easy
A. if n | 1 == 0:
print('Even')
B. if n & 0 == 1:
print('Even')
C. if n & 1 == 0:
print('Even')
D. if n ^ 1 == 0:
print('Even')
Solution
Step 1: Analyze each bitwise operation
if n & 1 == 0:
print('Even') uses n & 1 == 0, which checks if the least significant bit is 0, meaning even number. The other options use incorrect bitwise operations or comparisons.
Step 2: Confirm correct syntax and logic
if n & 1 == 0:
print('Even') correctly uses bitwise AND and compares to zero to detect even numbers.
Final Answer:
if n & 1 == 0:
print('Even') -> Option C
Quick Check:
n & 1 == 0 means even [OK]
Hint: Use 'if n & 1 == 0' to check even numbers [OK]
Common Mistakes:
Using bitwise OR or XOR instead of AND
Comparing with wrong values like 1 instead of 0
Misplacing parentheses causing syntax errors
3. What will be the output of the following code?
def check_even_odd(num):
if num & 1:
return 'Odd'
else:
return 'Even'
print(check_even_odd(10))
print(check_even_odd(7))
medium
A. Odd
Odd
B. Even
Odd
C. Odd
Even
D. Even
Even
Solution
Step 1: Evaluate check_even_odd(10)
10 in binary ends with 0, so 10 & 1 = 0, condition is false, returns 'Even'.
Step 2: Evaluate check_even_odd(7)
7 in binary ends with 1, so 7 & 1 = 1, condition is true, returns 'Odd'.
Final Answer:
Even
Odd -> Option B
Quick Check:
10 even, 7 odd [OK]
Hint: num & 1 true means odd, false means even [OK]
Common Mistakes:
Confusing return values for even and odd
Misreading bitwise AND result
Assuming & 1 returns boolean instead of int
4. Find the error in the following code that tries to print whether a number is even or odd using bits:
def is_even(num):
if num & 1 = 0:
return True
else:
return False
print(is_even(4))
medium
A. Using single '=' instead of '==' in the if condition
B. Using bitwise AND instead of bitwise OR
C. Missing colon after function definition
D. Incorrect indentation of return statements
Solution
Step 1: Identify syntax error in if condition
The condition uses single '=' which is assignment, not comparison. It should be '==' to compare values.
Step 2: Confirm correct syntax for comparison
Changing '=' to '==' fixes the syntax error and allows the function to work as intended.
Final Answer:
Using single '=' instead of '==' in the if condition -> Option A
Quick Check:
Use '==' for comparison, not '=' [OK]
Hint: Use '==' for comparison in if statements [OK]
Common Mistakes:
Using '=' instead of '==' in conditions
Confusing bitwise AND with OR
Ignoring syntax errors causing runtime failure
5. You want to filter a list of integers and keep only the odd numbers using bitwise operations. Which of the following code snippets correctly achieves this?
hard
A. odds = [x for x in numbers if x ^ 1 == 1]
B. odds = [x for x in numbers if x & 1 == 0]
C. odds = [x for x in numbers if x | 1 == 1]
D. odds = [x for x in numbers if x & 1 == 1]
Solution
Step 1: Understand filtering condition for odd numbers
Odd numbers have least significant bit 1, so x & 1 == 1 filters odd numbers.
Step 2: Check each option's logic
odds = [x for x in numbers if x & 1 == 1] correctly filters odd numbers. odds = [x for x in numbers if x & 1 == 0] filters even numbers. Options C and D use incorrect bitwise operations for this purpose.
Final Answer:
odds = [x for x in numbers if x & 1 == 1] -> Option D
Quick Check:
Filter odds with x & 1 == 1 [OK]
Hint: Use list comprehension with 'x & 1 == 1' to get odd numbers [OK]
Common Mistakes:
Using 'x & 1 == 0' which filters even numbers
Using bitwise OR or XOR incorrectly
Forgetting to compare with 1 explicitly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.