Check multiple of 4 using bitwise operator
Filed under Synopsys
Free read 3 of 3Confirm if negative numbers are allowed and what constitutes a multiple of 4.
The question
Given an integer n, determine if it is a multiple of 4 using bitwise operations only.
Constraints
- n is a 32-bit signed integer
- Must use bitwise operators
- O(1) time and space
Examples
- Input
- n = 8
- Output
- true
- Why
- 8 in binary is 1000. Its last two bits are 00, indicating it is divisible by 4.
- Input
- n = 6
- Output
- false
- Why
- 6 in binary is 0110. The last two bits are not 00.
Approach
Confirm if negative numbers are allowed and what constitutes a multiple of 4.
Check `n % 4 == 0`, which relies on division operators.
Since 4 is $2^2$, a number is a multiple of 4 if its last two binary bits are zero. This can be checked with `(n & 3) == 0`.
Handle zero and negative numbers carefully.
Solution
O(1) time, O(1) space
def is_multiple_of_4(n: int) -> bool:
return (n & 3) == 0
Tips
- What they are testing: Test powers of 2 (0, 4, 8, 16) and non-multiples like 2, 6, and negative numbers.
- Common mistake: Using `n & 3 == 0` incorrectly with negative numbers due to sign-extension or assuming division.
Related Synopsys questions
Collected from public candidate posts and coding forums. Updated Nov 2026.