Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

Just picture the shifting process in your mind..

class Solution {
public:
    int rangeBitwiseAnd(int m, int n) {
        if (m == n) return m;
        int r = 0;
        int cnt = 0;
        while (m && m < n)
        {    
            cnt++;
            m >>= 1;
            n >>= 1;
        }
        if (m == n && m > 0)
            r = m << cnt;
        return r;
    }
};
posted on 2015-04-16 12:52  Tonix  阅读(117)  评论(0)    收藏  举报