Bitwise AND of Numbers Range

2015.4.17 06:30

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.

Solution:

  Make it fast and simple.

Accepted code:

 1 // 1AC, easy
 2 class Solution {
 3 public:
 4     int rangeBitwiseAnd(int m, int n) {
 5         while (n > m) {
 6             n = (n & n - 1);
 7         }
 8         return m & n;
 9     }
10 };

 

相关文章:

  • 2021-12-31
  • 2022-03-03
  • 2021-09-11
  • 2021-12-22
  • 2022-02-03
猜你喜欢
  • 2021-12-08
  • 2022-01-07
  • 2022-12-23
  • 2021-06-12
  • 2021-10-09
  • 2021-12-26
  • 2021-09-10
相关资源
相似解决方案