Challenge - 5 Problems
Bitwise Even-Odd Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember, bitwise AND with 1 checks the last bit of the number.
✗ Incorrect
The last bit of an even number is 0, so num & 1 == 0 means even. For odd numbers, last bit is 1.
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Bitwise AND works the same for negative numbers in two's complement.
✗ Incorrect
Negative numbers in two's complement keep the last bit for odd/even check. -4 ends with 0 bit, -3 ends with 1 bit.
🧠 Conceptual
advanced2: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.
Attempts:
2 left
💡 Hint
Think about binary representation of even and odd numbers.
✗ Incorrect
Even numbers end with 0 in binary, so AND with 1 (which is 0001) results in 0. Odd numbers end with 1, so AND with 1 results in 1.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check the operator used in the if condition.
✗ Incorrect
The code uses '=' instead of '==' in the if condition, which causes a SyntaxError.
🚀 Application
expert2: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)
Attempts:
2 left
💡 Hint
Count how many numbers have last bit 1 between 1 and 10.
✗ Incorrect
Odd numbers between 1 and 10 are 1,3,5,7,9, total 5 numbers.