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. ARM处理器启动流程

    根据<<芯片手册>>查看相关内容: 1.启动方式 2.地址布局 3.启动流程

  2. iOS - MVP 架构模式

    1.MVP 从字面意思来理解,MVP 即 Modal View Presenter(模型 视图 协调器),MVP 实现了 Cocoa 的 MVC 的愿景.MVP 的协调器 Presenter 并没有对 ...

  3. 关于JAVA中URL传递中文参数,取值是乱码的解决办法

    前几天看到有网友在问URLDecoder和URLEncoder方面的使用问题,突然想起,原来我刚遇到这两个类时,也觉得很神密,由此可以想想初学者的心情,于是便有了今天的这篇文章. 其实,这两个类的使用 ...

  4. 12 Using_explain_plan

    The row source tree is the core of the execution plan. The tree shows the following information: An ...

  5. Java JDBC连接数据库 Access连接数据库

    1.加载JDBC驱动程序:  在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机),再通过java.lang.Class类的静态方法forName(String  classN ...

  6. Data Deduplication in Windows Server 2012

    https://blogs.technet.microsoft.com/filecab/2012/05/20/introduction-to-data-deduplication-in-windows ...

  7. eclipse 下的 merge 是如何实现的

    1 我从eclipse里面新建一个workspace2 新建一个分支3 再新建另外一个分支4 回到原分支, 修改某一行,比如a.txt的85行5 提交git add commit6 切换到新分支7 修 ...

  8. 为什么删不掉date模块

    显示是field pending deletion一看report里面的field list并没有xxx_date_xxx,只好跑到数据库才看到一个field_date_test当时并没有把这个字段当 ...

  9. Linux 命令速查

    学生信,Linux是最最基本的技能,要尽量将自己的工作平台转移到Linux,编程写脚本,这样会极大的提升工作效率,找工作时也不会太怂.Linux所有的任务都是通过命令来完成的,具有高度的统一性.Lin ...

  10. Docker 使用指南 (二)—— 搭建本地仓库

    版权声明:本文由田飞雨原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/94 来源:腾云阁 https://www.qclou ...