0
0
DSA Pythonprogramming~5 mins

Check if Number is Even or Odd Using Bits in DSA Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A-1
B1
Cnum
D0
Which bit of a number determines if it is even or odd?
AMost significant bit
BSecond bit from right
CLeast significant bit
DMiddle bit
What is the output of 10 & 1?
A0
B10
C1
DNone
Which operator is used to check even or odd using bits?
ABitwise OR (|)
BBitwise AND (&)
CBitwise XOR (^)
DBitwise NOT (~)
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
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.