AlgoLounge

Bitwise AND of Numbers Range

medium

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

Examples

Example 1

Input:  left = 5, right = 7
Output: 4

5 (101) AND 6 (110) AND 7 (111) = 4 (100)

Example 2

Input:  left = 0, right = 0
Output: 0

Example 3

Input:  left = 1, right = 2147483647
Output: 0

Constraints

  • 0 <= left <= right <= 2^31 - 1
TypeScript
Loading editor…
List of test cases (12)
Case 1
left = 5
right = 7
Case 2
left = 0
right = 0
Case 3
left = 1
right = 2147483647
Case 4
left = 1
right = 5
Case 5
left = 26
right = 30
Case 6
left = 10
right = 12
Case 7
left = 15
right = 15
Case 8
left = 100
right = 110
Case 9
left = 1000
right = 1023
Case 10
left = 2
right = 3
Case 11
left = 8
right = 15
Case 12
left = 2147483646
right = 2147483647