Bitwise AND of Numbers Range
Given two integers left and right that represent an inclusive range [left, right], return the bitwise AND of every integer in that range.
Open official problem prompt ↗Compute the AND of a potentially enormous run of consecutive integers without touching each one.
Think of two odometer readings. Whatever leading digits are identical stay fixed; every digit that changed at least once between the two readings must have rolled through all its values, so it cannot be pinned to a single value — for AND, those unstable positions collapse to 0.
- Input
- left = 5, right = 7
- Output
- 4
- Why
- 5 & 6 & 7 = 101 & 110 & 111 = 100 = 4
0 <= left <= right <= 2^31 - 1