Loading problem...
You are given two non-negative integers, left and right, which define an inclusive range [left, right]. Your task is to compute the result of applying the bitwise AND operation to every integer within this range, starting from left and going up to and including right.
The bitwise AND operation compares corresponding bits of two numbers and returns a new number where each bit is set to 1 only if both corresponding bits in the operands are 1; otherwise, that bit is set to 0.
When applied across a contiguous sequence of integers, patterns emerge in the binary representations. The key insight is to identify which bits remain consistently set to 1 across all numbers in the range. Bits that flip at any point within the range will contribute a 0 to the final result.
Understanding the Problem:
left == right), the result is simply that number itself.left and right, the more likely that all bits will eventually flip, potentially resulting in 0.Return the single integer that represents the cumulative bitwise AND of all integers from left to right, inclusive.
left = 5, right = 74The binary representations are: 5 = 101, 6 = 110, 7 = 111. Performing bitwise AND: 101 & 110 & 111 = 100 (which is 4 in decimal). The only bit that remains 1 across all three numbers is the leftmost bit (bit position 2).
left = 0, right = 00The range contains only 0. The bitwise AND of just 0 with itself is 0.
left = 1, right = 21474836470This is a very large range spanning from 1 to the maximum 32-bit signed integer. Within this range, every single bit position will have both 0s and 1s at some point, so the cumulative AND of all numbers produces 0.
Constraints