Problem Description
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.
[https://leetcode.com/problems/bitwise-and-of-numbers-range/]
思路
例如,111001到111101 ,辣么111001&111000==111101&111000
就能得到答案。。。
Code
package q201;
public class Solution {
public static int rangeBitwiseAnd(int m, int n) {
int test=Integer.MAX_VALUE;
while((m&test)!=(n&test)) test=test<<1;
return n&test;
}
// public static void main(String[] args) {
// System.out.print(rangeBitwiseAnd(110, 111));
// }
}