0
0
DSA Pythonprogramming~20 mins

Check if Number is Even or Odd Using Bits in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bitwise Even-Odd Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code using bitwise AND to check even or odd?
Look at the code below. It uses bitwise AND to check if a number is even or odd. What will it print?
DSA Python
def check_even_odd(num):
    if num & 1 == 0:
        print(f"{num} is Even")
    else:
        print(f"{num} is Odd")

check_even_odd(10)
check_even_odd(7)
A10 is Even\n7 is Odd
B10 is Odd\n7 is Even
C10 is Odd\n7 is Odd
D10 is Even\n7 is Even
Attempts:
2 left
💡 Hint
Remember, bitwise AND with 1 checks the last bit of the number.
Predict Output
intermediate
2:00remaining
What is the output of this function for negative numbers?
This function uses bitwise AND to check even or odd. What will it print for -4 and -3?
DSA Python
def check_even_odd(num):
    if num & 1 == 0:
        print(f"{num} is Even")
    else:
        print(f"{num} is Odd")

check_even_odd(-4)
check_even_odd(-3)
A-4 is Odd\n-3 is Odd
B-4 is Even\n-3 is Odd
C-4 is Odd\n-3 is Even
D-4 is Even\n-3 is Even
Attempts:
2 left
💡 Hint
Bitwise AND works the same for negative numbers in two's complement.
🧠 Conceptual
advanced
2:00remaining
Why does bitwise AND with 1 check if a number is even or odd?
Choose the best explanation why num & 1 == 0 means the number is even.
ABecause bitwise AND with 1 converts the number to its negative form.
BBecause bitwise AND with 1 counts the total number of bits in the number.
CBecause bitwise AND with 1 shifts the number to the right by one bit.
DBecause the last bit of an even number is always 0, so AND with 1 results in 0.
Attempts:
2 left
💡 Hint
Think about binary representation of even and odd numbers.
🔧 Debug
advanced
2:00remaining
What error does this code raise?
Look at this code that tries to check even or odd using bits. What error will it raise?
DSA Python
def check_even_odd(num):
    if num & 1 = 0:
        print(f"{num} is Even")
    else:
        print(f"{num} is Odd")

check_even_odd(5)
ANameError
BTypeError
CSyntaxError
DNo error, prints '5 is Odd'
Attempts:
2 left
💡 Hint
Check the operator used in the if condition.
🚀 Application
expert
2:00remaining
How many numbers between 1 and 10 (inclusive) are identified as odd by this bitwise check?
Using the bitwise check num & 1 == 1 to find odd numbers, how many numbers between 1 and 10 (including both) will be identified as odd?
DSA Python
count = 0
for num in range(1, 11):
    if num & 1 == 1:
        count += 1
print(count)
A5
B6
C4
D10
Attempts:
2 left
💡 Hint
Count how many numbers have last bit 1 between 1 and 10.