blob: ce137c129119a48b379c3fdc0afda2b5f9f529bf (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
// 201. Bitwise AND of Numbers Range
#include "leetcode.h"
/*
* given two integers 'm' and 'n' that represent the range '[m,
* n]', return the bitwise and of all numbers in this range, inclusive.
*/
int rangeBitwiseAnd(int m, int n) {
int k = 0;
while (m != n) {
m >>= 1;
n >>= 1;
++k;
}
return n << k;
}
int main() {
printf("%d\n", rangeBitwiseAnd(5, 7)); // expect: 4
printf("%d\n", rangeBitwiseAnd(0, 0)); // expect: 0
printf("%d\n", rangeBitwiseAnd(1, 2147483647)); // expect: 0
}
|