Check if Number is Even or Odd Using Bits in DSA Python - Time & Space Complexity
We want to know how fast we can tell if a number is even or odd using bits.
How does the time to check change as the number gets bigger?
Analyze the time complexity of the following code snippet.
def is_even(num: int) -> bool:
return (num & 1) == 0
number = 42
print(is_even(number)) # True if even, False if odd
This code uses a bitwise AND to check the last bit of the number to decide if it is even or odd.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single bitwise AND operation.
- How many times: Exactly once per function call.
The operation always takes the same time no matter how big the number is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time does not grow with input size; it stays constant.
Time Complexity: O(1)
This means the check takes the same small amount of time no matter how big the number is.
[X] Wrong: "Checking bits takes longer for bigger numbers because they have more bits."
[OK] Correct: The operation only looks at the last bit, so it always takes the same time regardless of number size.
Knowing this quick bit check shows you understand how computers work with numbers and can write very fast code.
"What if we checked multiple bits to find if a number is divisible by 4? How would the time complexity change?"
Practice
num & 1 check when determining if a number is even or odd?Solution
Step 1: Understand bitwise AND operation
The expressionnum & 1performs a bitwise AND betweennumand 1, which isolates the least significant bit (rightmost bit) ofnum.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 AQuick Check:
num & 1 = 1 means odd [OK]
- Thinking & 1 shifts bits instead of checking last bit
- Confusing bitwise AND with addition
- Assuming & 1 checks if number > 1
n is even using bitwise operators?Solution
Step 1: Analyze each bitwise operation
if n & 1 == 0: print('Even') usesn & 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 CQuick Check:
n & 1 == 0 means even [OK]
- Using bitwise OR or XOR instead of AND
- Comparing with wrong values like 1 instead of 0
- Misplacing parentheses causing syntax errors
def check_even_odd(num):
if num & 1:
return 'Odd'
else:
return 'Even'
print(check_even_odd(10))
print(check_even_odd(7))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 BQuick Check:
10 even, 7 odd [OK]
- Confusing return values for even and odd
- Misreading bitwise AND result
- Assuming & 1 returns boolean instead of int
def is_even(num):
if num & 1 = 0:
return True
else:
return False
print(is_even(4))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 AQuick Check:
Use '==' for comparison, not '=' [OK]
- Using '=' instead of '==' in conditions
- Confusing bitwise AND with OR
- Ignoring syntax errors causing runtime failure
Solution
Step 1: Understand filtering condition for odd numbers
Odd numbers have least significant bit 1, sox & 1 == 1filters 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 DQuick Check:
Filter odds with x & 1 == 1 [OK]
- Using 'x & 1 == 0' which filters even numbers
- Using bitwise OR or XOR incorrectly
- Forgetting to compare with 1 explicitly
Ready to prove it?
Pass the test and this pattern moves unknown
