Difficulty: Medium
Correct Answer: Use a bitwise AND operation with 1; if number & 1 equals 0 the number is even, otherwise it is odd
Explanation:
Introduction / Context:
Interviewers sometimes ask for simple algorithms with constraints, such as detecting whether a number is even or odd without using the modulus or division operators. This tests understanding of how numbers are represented in binary and the use of bitwise operations. Even and odd properties are directly reflected in the least significant bit of the binary representation. This question focuses on a clean, efficient technique that avoids division and modulus entirely.
Given Data / Assumptions:
Concept / Approach:
In binary, an even number always has its least significant bit equal to 0, because it is divisible by 2 without remainder. An odd number has least significant bit equal to 1. A bitwise AND with 1 isolates this least significant bit. If number & 1 evaluates to 0, the least significant bit is 0 and the number is even. If number & 1 evaluates to 1, the least significant bit is 1 and the number is odd. This approach avoids division and modulus while providing a constant time operation. The correct option must describe this bitwise AND technique clearly.
Step-by-Step Solution:
Step 1: Express the number in binary, conceptually, even if not actually converting it in code.
Step 2: Observe that the least significant bit determines whether the number is even or odd.
Step 3: Use the bitwise AND operator with 1 to mask out all other bits and keep only the least significant bit.
Step 4: If the result of number & 1 is 0, then the least significant bit is 0 and the number is even.
Step 5: If the result is 1, then the least significant bit is 1 and the number is odd.
Verification / Alternative check:
Try a few examples. For number equal to 6, the binary representation is 110. Applying number & 1 gives 110 & 001 which equals 000, so 6 is even. For number equal to 7, binary 111, number & 1 equals 001, so 7 is odd. This pattern holds for all integers because the least significant bit always reflects whether the number includes a factor of 2.
Why Other Options Are Wrong:
Option b simply adds 10 and checks if the result is greater than 10, which does not correlate with even or odd status and will always be true for positive numbers. Option c suggests assuming all positive numbers are odd, which is obviously incorrect. Option d converts to floating point and checks for a decimal part, but this does not avoid division or modulus and is an unreliable way to test parity, especially considering floating point representation issues.
Common Pitfalls:
Some candidates attempt repeated subtraction by 2 or loops that count up to the number, which are less efficient than bitwise operations. Another pitfall is misusing bitwise OR instead of AND, which does not isolate the least significant bit correctly. In languages where booleans and integers interact, ensure you compare number & 1 explicitly with 0 or 1 for clarity. The bitwise AND method is both efficient and idiomatic in low level programming contexts.
Final Answer:
You can determine parity by using a bitwise AND operation with 1; if number & 1 equals 0 the number is even, otherwise it is odd.
Discussion & Comments