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.

int rangeBitwiseAnd(int m, int n) {

    int mask = 0xffffffff;

    /* find out the same bits in left side*/

    while (mask != ) {

        if ((m & mask) == (n & mask)) {

            break;

        }

        mask <<= ;

    }

    return m & mask;

}

Idea:

1) we know when a number add one, some of the right bit changes from 0 to 1 or  from 1 to 0

2) if a bit is 0, then AND will cause this bit to 0 eventually.

So, we can just simply check how many left bits are same for m and n.

for example:

5 is 101

6 is 110

when 5 adds 1, then the right two bits are changed.  the result is 100

6 is 110

7 is 111

when 6 adds 1, then the right one bit is changed. the result is 110.

9 is 1001

10 is 1010

11 is 1011

12 is 1100

Comparing from 9 to 12, we can see the first left bit is same, that's result.

201. Bitwise AND of Numbers Range -- 连续整数按位与的和的更多相关文章

  1. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  2. 【LeetCode】201. Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range  Given a range [m, n] where 0 <= m <= n <= 2147483647, return ...

  3. 【刷题-LeetCode】201 Bitwise AND of Numbers Range

    Bitwise AND of Numbers Range Given a range [m, n] where 0 <= m <= n <= 2147483647, return t ...

  4. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  5. Java for LeetCode 201 Bitwise AND of Numbers Range

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  6. 201. Bitwise AND of Numbers Range

    题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all num ...

  7. [LeetCode#201] Bitwise AND of Numbers Range

    Problem: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of al ...

  8. [LeetCode] 201. Bitwise AND of Numbers Range ☆☆☆(数字范围按位与)

    https://leetcode.com/problems/bitwise-and-of-numbers-range/discuss/56729/Bit-operation-solution(JAVA ...

  9. 201. Bitwise AND of Numbers Range (Bit)

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND(按位与) of all nu ...

随机推荐

  1. [HDOJ1016]Prime Ring Problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 原题: A ring is compose of n circles as shown in d ...

  2. Fiddler界面详解

    Statistics 页签 完整页签如下图: Statistics 页签显示当前用户选择的 Sessions 的汇总信息,包括:选择的 Sessions 总数.发送字节数.接收字节数.响应类型的汇总表 ...

  3. 制作一个属于自己的BHO吧!(C#) (转)

    摘自:http://tech.ddvip.com/2013-05/1369758775196257.html BHO(Browser Helper Object)是插件,它寄存在IE浏览器中运行.在咱 ...

  4. 函数对象适配器之ptr_fun的使用示例

    //============================================================================ // Name : CopyInts4.c ...

  5. 九度-剑指Offer

    二维数组中的查找 分析:既然已经给定了每一行从左至右递增,那么对于每一行直接二分查找即可,一开始还想着每一列同样查找一次,后来发现每一行查找一遍就能够遍历所有的元素了. #include <cs ...

  6. 装了maven插件的eclipse中M2_REPO无法编辑、删除(转)

    今天用了新版本的eclipse,用maven在命令行生成了一个普通项目.导入eclipse之后发现本地仓库的路径不正确. 显示的为 user.path/.m2/repository 但是我的仓库早已经 ...

  7. iOS - UIViewController

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIViewController : UIResponder <NSCoding, UIAppearanceC ...

  8. c 函数调用产生的汇编指令和数据在内存情况(2)

    c 函数调用产生的汇编指令和数据在内存情况(1) 一直对函数调用的具体汇编指令和各种变量在内存的具体分配,一知半解.各种资料都很详细,但是不实践,不亲自查看下内存总不能笃定.那就自己做下. 两个目的: ...

  9. windos多线程编程

    随机数滚动发生器 #include <stdio.h> #include <Windows.h> #include <ctime> #include <pro ...

  10. JavaScript变量——栈内存or堆内存

    原文  http://blog.csdn.net/xdd19910505/article/details/41900693 堆和栈这两个字我们已经接触多很多次,那么具体是什么存在栈中什么存在堆中呢?就 ...