Leetcode 201: 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.

 

 1 public class Solution {
 2     public int RangeBitwiseAnd(int m, int n) {
 3         if(m == 0){
 4             return 0;
 5         }
 6         
 7         int moveFactor = 1;
 8         while(m != n){
 9             m >>= 1;
10             n >>= 1;
11             moveFactor <<= 1;
12         }
13         
14         return m * moveFactor;   
15     }
16 }

 

posted @ 2017-11-29 07:03  逸朵  阅读(123)  评论(0)    收藏  举报