Bird
Raised Fist0

Given the function below, what is the output of rangeBitwiseAnd(10, 15)?

easy🧾 Trace Q3 of Q15
Bit Manipulation - Bitwise AND of Numbers Range
Given the function below, what is the output of rangeBitwiseAnd(10, 15)?
def rangeBitwiseAnd(left: int, right: int) -> int:
    shift = 0
    while left != right:
        left >>= 1
        right >>= 1
        shift += 1
    return left << shift
A10
B8
C0
D15
Step-by-Step Solution
Solution:
  1. Step 1: Right shift until equal

    Shift left=10 (1010) and right=15 (1111) right until equal:
    10(1010) -> 5(0101), 15(1111) -> 7(0111), shift=1
    5 != 7, shift again:
    5(0101) -> 2(0010), 7(0111) -> 3(0011), shift=2
    2 != 3, shift again:
    2(0010) -> 1(0001), 3(0011) -> 1(0001), shift=3
  2. Step 2: Left shift back

    Now left=1, shift=3, so result = 1 << 3 = 8
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Check binary prefixes match after shifts [OK]
Quick Trick: Shift until equal, then shift back [OK]
Common Mistakes:
MISTAKES
  • Returning left or right without shifting back
  • Stopping shifts too early
  • Confusing bitwise AND with bitwise OR
Trap Explanation:
PITFALL
  • Forgetting to shift back leads to incorrect smaller values.
Interviewer Note:
CONTEXT
  • Tests ability to trace bitwise AND range algorithm step-by-step.
Master "Bitwise AND of Numbers Range" in Bit Manipulation

3 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bit Manipulation Quizzes