Bitwise AND of Numbers Range
Source
- leetcode: Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise
AND of all numbers in this range, inclusive.
For example, given the range [5, 7], you should return 4.
Java
public class Solution {
public int rangeBitwiseAnd(int m, int n) {
int result=1;
while(m!=n){
m>>=1;
n>>=1;
result <<=1;
}
return m*result;
}
}
/*
The idea is very simple:
last bit of (odd number & even number) is 0.
when m != n, There is at least an odd number and an even number, so the last bit position result is 0.
Move m and n rigth a position.
Keep doing step 1,2,3 until m equal to n, use a factor to record the iteration time.
https://leetcode.com/discuss/32115/bit-operation-solution-java
*/