leetcode 201. Bitwise AND of Numbers Range

来源:互联网 发布:个性字体设计软件 编辑:程序博客网 时间:2024/06/06 17:12

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.


求公共的2进制前缀。

5 - 0101  

7 - 0111

5到7之间的数公共的位就是 0100 -> 4


class Solution {public:    int rangeBitwiseAnd(int m, int n)     {        int count = 0;        while ( m != n )        {            m>>=1;            n>>=1;            count++;        }        return m<<count;    }};


原创粉丝点击