Bird
Raised Fist0

Consider the following code snippet implementing the optimal approach for bitwise AND of numbers range. What is the output when calling rangeBitwiseAnd(5, 7)?

easy🧾 Code Trace Q12 of Q15
Bit Manipulation - Bitwise AND of Numbers Range
Consider the following code snippet implementing the optimal approach for bitwise AND of numbers range. What is the output when calling rangeBitwiseAnd(5, 7)?
A4
B5
C0
D7
Step-by-Step Solution
Solution:
  1. Step 1: Trace initial values

    left = 5 (binary 0101), right = 7 (binary 0111)
  2. Step 2: Execute loop iterations

    First iteration: right = 7 & 6 = 6 (0110) Second iteration: right = 6 & 5 = 4 (0100) Now right (4) <= left (5) is true, loop ends.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Final returned value is 4 after clearing differing bits [OK]
Quick Trick: Bit-clearing reduces right until common prefix found [OK]
Common Mistakes:
MISTAKES
  • Confusing loop condition
  • Off-by-one in bit clearing
  • Returning right too early
Trap Explanation:
PITFALL
  • Candidates often mistake the loop condition or the effect of bit clearing, leading to wrong final value.
Interviewer Note:
CONTEXT
  • Tests ability to mentally execute bitwise operations and loop behavior.
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