2024-02-21 Daily Challenge

Today I have done leetcode's February LeetCoding Challenge with cpp.

February LeetCoding Challenge 21

Description

Bitwise AND of Numbers Range

Given two integers left and right that represent the range [left, right], return the bitwise AND of all numbers in this range, inclusive.

 

Example 1:

Input: left = 5, right = 7
Output: 4

Example 2:

Input: left = 0, right = 0
Output: 0

Example 3:

Input: left = 1, right = 2147483647
Output: 0

 

Constraints:

  • 0 <= left <= right <= 231 - 1

Solution

constexpr int highbit(int x) {
  x |= x >> 16;
  x |= x >> 8;
  x |= x >> 4;
  x |= x >> 2;
  x |= x >> 1;
  x ^= x >> 1;
  return x;
}
class Solution {
public:
  int rangeBitwiseAnd(int left, int right) {
    if(left == right) return left;
    int result = left & right;
    int answer = 0;
    int bit = highbit(right);
    while(bit) {
      if(bit & result) {
        answer |= bit;
      } else if((bit & left) || (bit & right)) {
        break;
      }
      bit >>= 1;
    }
    return answer;
  }
};

// Accepted
// 8268/8268 cases passed (0 ms)
// Your runtime beats 100 % of cpp submissions
// Your memory usage beats 77.49 % of cpp submissions (5.9 MB)