Complete the code to check if a number is even using bitwise AND.
def is_even(num): return (num & [1]) == 0 print(is_even(4)) # True print(is_even(7)) # False
The least significant bit (LSB) of an even number is 0. Using bitwise AND with 1 checks this bit.
Complete the code to check if a number is odd using bitwise AND.
def is_odd(num): return (num & [1]) == 1 print(is_odd(5)) # True print(is_odd(8)) # False
Odd numbers have the least significant bit set to 1. Bitwise AND with 1 checks this.
Fix the error in the code to correctly check if a number is even using bits.
def check_even(num): if (num & [1]) == 0: return True else: return False print(check_even(10)) # Should be True print(check_even(7)) # Should be False
For even numbers, the last bit is 0, so (num & 1) == 0 means even.
Fill both blanks to create a function that returns 'Even' or 'Odd' using bitwise operation.
def even_or_odd(num): if (num & [1]) == [2]: return 'Even' else: return 'Odd' print(even_or_odd(12)) # Even print(even_or_odd(15)) # Odd
Check last bit with 1; if result is 0, number is even; else odd.
Fill all three blanks to create a dictionary comprehension that labels numbers as 'Even' or 'Odd' using bits.
numbers = [1, 2, 3, 4, 5] result = {num: 'Even' if (num & [1]) == [2] else 'Odd' for [3] in numbers} print(result)
Use 1 as mask, compare to 0 for even, and iterate with variable 'num'.