leetcode--Bitwise AND of Numbers Range

来源:互联网 发布:xps和eds的区别 知乎 编辑:程序博客网 时间:2024/06/15 23:52

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.

[java] view plain copy
  1. public class Solution {  
  2.     public int rangeBitwiseAnd(int m, int n) {  
  3.         int i = 0;  
  4.         while(n!=m){  
  5.             n = n>>1;  
  6.             m = m>>1;  
  7.             i++;  
  8.         }  
  9.         return m<<i;  
  10.     }  

原文链接http://blog.csdn.net/crazy__chen/article/details/46575045

原创粉丝点击