Bitwise AND of Numbers Range
Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.

30:00

Bitwise AND of Numbers Range
medium
Topics
Companies

Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.

Example 1:
Input: {"left":5,"right":7}
Output: 4
Constraints:
  • 0leftright23110 \leq \text{left} \leq \text{right} \leq 2^{31} - 1

Input
arr ={"left":5,"right":7}

Find common prefix of 5 and 7 in binary

Range: [5, 7] | Shifts: 0

left (5):
0
0
0
0
0
1
0
1
right (7):
0
0
0
0
0
1
1
1
Result:
0
0
0
0
0
0
0
0

= 0

Find common prefix by right-shifting both numbers until equal, then left-shift back
Variables
No variables to display
DepthFunction Call
Stack empty
0/3