For a positive integer n, which operations represent multiplying by 2 and dividing by 2...
Accenture technical mcq question, verified with a worked answer. Free to practise - no sign-up.
For a positive integer n, which operations represent multiplying by 2 and dividing by 2 (integer division), respectively?
Show answer & explanation
Left shift by 1 bit (n << 1) multiplies by 2 because it moves all bits one position left, effectively doubling the value. Right shift by 1 bit (n >> 1) divides by 2 (integer division) because it moves all bits one position right, effectively halving the value. The other options perform bitwise AND, OR, XOR, and NOT operations, which do not consistently achieve multiplication or division.
Step-by-step Derivation:
Bitwise shift operations work on the binary representation of numbers.
For multiplication by 2:
- Example: n = 5 (binary: 101)
- n << 1 shifts left: 1010 (binary) = 10 (decimal) = 5 × 2 ✓
For division by 2 (integer division):
- Example: n = 10 (binary: 1010)
- n >> 1 shifts right: 101 (binary) = 5 (decimal) = 10 ÷ 2 ✓
Verifying wrong options:
- Option B reverses the operators (wrong effect)
- Option C: n & 1 extracts the least significant bit (0 or 1), not multiplication
- Option D: XOR and bitwise NOT do not scale values arithmetically