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.

  1. int rangeBitwiseAnd(int m, int n) {
  2.  
  3. int mask = 0xffffffff;
  4.  
  5. /* find out the same bits in left side*/
  6.  
  7. while (mask != ) {
  8.  
  9. if ((m & mask) == (n & mask)) {
  10.  
  11. break;
  12.  
  13. }
  14.  
  15. mask <<= ;
  16.  
  17. }
  18.  
  19. return m & mask;
  20.  
  21. }

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. hdu 2112 (最短路+map)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)  ...

  2. Python-爬虫初学

    #爬取网站中的图片 1 import re #正则表达式库 import urllib #url链接库 def getHtml(url): page = urllib.urlopen(url) #打开 ...

  3. Java Abstract class and Interface

    Abstract Class 在定义class的时候必须有abstract 关键字 抽象方法必须有abstract关键字. 可以有已经实现的方法. 可以定义static final 的常量. 可以实现 ...

  4. Class create, device create, device create file (转)

    来自:http://www.hovercool.com/en/Class_create,_device_create,_device_create_file 开始写Linux设备驱动程序的时候,很多时 ...

  5. Python学习(7)数字

    目录 Python 数字 Python 数字类型转换 Python 数学函数 Python 随机数函数 Python 三角函数 Python 数学常量 Python 数字 Python 数字数据类型用 ...

  6. Java中ArrayDeque,栈与队列

    package ch8; import java.util.*; /** * Created by Jiqing on 2016/11/27. */ public class ArrayDequeSt ...

  7. Python学习笔记6-字典

    定义 使用键值对, >>> person = {"name":"keven","age":15,"gender& ...

  8. POJ 2484 A Funny Game(神题!)

    一开始看这道博弈题的时候我就用很常规的思路去分析了,首先先手取1或者2个coin后都会使剩下的coin变成线性排列的长条,然后无论双方如何操作都是把该线条分解为若干个子线条而已,即分解为若干个子游戏而 ...

  9. Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅

      Android 高手进阶(21)  版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处http://blog.csdn.net/xiaanming/article/details ...

  10. Linux大文件分割split和合并cat使用方法

    本文主要介绍linux下两个命令:split和cat.其中,相信大家都熟悉cat命令,一般用来查看一个文件的内容,但是它还其它的功能,比如这里要介绍的文件合并功能,它可把多个文件内容合并到一个文件中. ...