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.

Example 1:

Input: [5,7]
Output: 4

Example 2:

Input: [0,1]
Output: 0

思路:
and 的话必须两个都是1才是1.
先看最后一位。
101
110
111

抹掉,再看下一位
10
11
11
第一位,都是1,一共抹掉2个0,所以 1<<2
1
1
1

 1 class Solution {
 2 public:
 3     int rangeBitwiseAnd(int m,int n) {
 4         int cnt = 0;
 5         while(m!=n){
 6             m>>=1;
 7             n>>=1;
 8             cnt++;
 9         }
10         return n<<cnt;
11         
12     }
13 };

相关文章

自1998年我国取消了福利分房的政策后,房地产市场迅速开展蓬...
文章目录获取数据查看数据结构获取数据下载数据可以直接通过...
网上商城系统MySql数据库设计
26个来源的气象数据获取代码
在进入21世纪以来,中国电信业告别了20世纪最后阶段的高速发...