Bitwise AND of Numbers Range
2015-09-09 04:39
246 查看
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.
For example, given the range [5, 7], you should return 4.
public int rangeBitwiseAnd(int m, int n) { if(m == 0){ return 0; } int moveFactor = 1; while(m != n){ //m,n都向右移动一位,直到m=n为止 m >>= 1; n >>= 1; //factor向左移动一位,右边补零 moveFactor <<= 1; } //两者相乘就是结果 return m * moveFactor; } /** * e.g:[125,127] * 1111101 ==> 125 * 1111110 ==> 126 * ------- * 1111100 * 1111111 ==> 127 * ------- * 1111100 ==>124 * * 1111100 = 11111 * * 100 * ------- * 1111100 */http://www.zybang.com/question/06d40cb634d7cbf93a3b4f951d6fa9f7.html
相关文章推荐
- 二进制、八进制、十进制、十六进制之间转换
- opengl 学习笔记(1)setting up opengl
- OpenGL教程
- Java 反射,通过反射调用所有的方法,包括private方法
- 【Linux探索之旅】第三部分第一课:数据处理,慢条斯理
- 【Linux探索之旅】第三部分第一课:数据处理,慢条斯理
- c++常见面试题30道
- Asp.Net+Oracle+EasyUI简单增删查改
- Implement stack using a queue
- Leetcode #42 Trapping Rain Water
- LCT - hdu5002 Tree
- 栈的链式存储结构及实现
- 平台设备驱动中的prob是怎么被调用的?为什么说是bus提供的probe优先调用?
- 最新 Windows 10 应用项目模板发布
- Spiral Matrix I, II
- LCT - hdu4010 Query on The Trees
- 狼性
- centos7下yum安装mysql
- Junit简介与使用
- a nice article about /dev/urandom vs /dev/random