Bitwise AND of Numbers Range
Bitwise AND of Numbers Range is LeetCode 201 (Medium). Given two integers left and right, return the bitwise AND of every integer from left to right, both ends included.
Both ends lie between 0 and 2³¹ − 1, so one range can hold over two billion numbers. Visiting them one by one is far too slow; the answer has to come from the bits of the two ends.
- 0 <= left <= right <= 2³¹ - 1
Intuition
Find the highest bit where left and right differ. left has a 0 there and right has a 1, so while counting up from left the range passes a number with a 1 in that place and only 0s below it. That one number clears every lower bit of the AND, and left itself clears the differing bit.
The bits above that place are equal in left and right, and every number between them carries the same bits, so they all survive. The answer is the common binary prefix of the two ends, followed by zeros.
An AND (or OR) over a huge run of consecutive integers cannot be computed one number at a time. Ask which bits are allowed to change inside the run: a bit that changes even once is 0 in an AND, so only the bits shared by both ends can remain.
Approach
Before reading on, write 9, 10, 11 and 12 in binary and AND them by hand. Which bits do 9 and 12 share at the front, and what does the result look like after those bits? Then find a way to get that answer from 9 and 12 alone, in at most 31 steps.
Two ways to solve it
Drop the lowest bit of both numbers until they are equal, then shift the result back.
- Steps: one per bit below the prefix, 31 at most.
- Proof: follows straight from the common-prefix argument.
The usual interview answer.
Repeat right &= right - 1 until right is no longer above left.
- Steps: one per 1-bit removed, which can be fewer than the shifts.
- Code: a two-line loop.
The common follow-up, built on Brian Kernighan's algorithm for clearing 1-bits.
Both keep only the common prefix and both take at most 31 steps. Shifting both ends is the easier one to prove, so the steps, code and live demo below use it; the bit-clearing loop comes after the demo.
Drop the lowest bit while the ends differ
While left != right, shift both one place to the right and add 1 to shift. While the two numbers still differ, the bit being dropped sits at or below their highest differing bit, so it is 0 in the answer.
The equal value is the shared prefix
When left == right, what remains is exactly the run of high bits both ends had in common. Every number in the range has these same high bits, so each of them is 1 in the AND wherever it is 1 here.
Shift the prefix back into place
Return left << shift. This moves the prefix back to its original bit positions and fills the shift lowest places with 0s, one for each bit that some number in the range cleared.
Code in Python, C++ and Java
Clear the lowest 1 of right
right & (right - 1) removes the lowest 1-bit of right. Repeat it while right is still above left. Each removed bit lies below the common prefix, because the prefix with zeros after it is never above left, so the loop stops on exactly that value.
Common pitfalls
Looping over every number in the range
ans = left
for x in range(left + 1, right + 1):
ans &= xwhile left != right:
left >>= 1
right >>= 1
shift += 1On left = 1, right = 2147483647 the loop runs over two billion times and times out. In C++ or Java, for (int i = left; i <= right; i++) is worse: when right is the largest int, i wraps around to a negative number and the loop never ends.
Returning left & right
return left & right
return left << shift
It checks only the two ends, but the numbers between them can clear more bits. On left = 5, right = 7, 5 & 7 is 5 (binary 101), yet 6 (binary 110) is in the range and clears the last bit, so the answer is 4.
Returning the prefix without shifting it back
return left
return left << shift
After the loop the prefix still sits at the bottom of the number. On left = 5, right = 7 the loop stops at 1 after two shifts, so the code returns 1 instead of 4.
Edge cases
The range holds one number, the loop runs zero times, and left << 0 returns it unchanged: left = right = 6 gives 6.
0 is in the range and has no 1-bits, so the answer is 0. The loop shifts until right also reaches 0, then returns 0 << shift, which is 0.
Complexity
Bitwise AND of Numbers Range FAQ
Why does a range that crosses a power of two always give 0?
If left is below 2^k and right is at least 2^k, then 2^k − 1 (all 1s below bit k) and 2^k (a single 1 at bit k) are both in the range. They share no 1-bits, so their AND is 0 already. For example, left = 3, right = 4 gives 0.
Does the same prefix trick work for OR or XOR over a range?
No. A bit that changes inside the range is 0 in an AND but 1 in an OR, so the OR of a range is the common prefix followed by all 1s. XOR needs a different idea entirely, based on the pattern of 0 ^ 1 ^ … ^ n repeating every four numbers.