1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class BitwiseANDOfNumbersRange { public int rangeBitwiseAnd(int m, int n) { if (m > n || m < 0) { return -1; } int result = m; for (int i = m + 1; i <= n; i++) { result &= i; } return result; } public int rangeBitwiseAnd2(int m, int n) { if (m > n || m < 0) { return -1; } while (n > m) { n = n & (n - 1); } return n & m; } }
|