问题描述:
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.
示例:given the range [5, 7], you should return 4.
问题分析:
寻找n和m的最左侧的公共位。关键点在于利用n = n & (n - 1) ( n > m) 去寻找n和m的最左侧公共位。
过程详见代码:
class Solution {
public:
int rangeBitwiseAnd(int m, int n) {
while(m<n) n = n & (n-1);
return n;
}
};