코테준비 - Bitwise AND of Numbers Range

정상화·2023년 2월 26일

LeetCode

목록 보기
171/222

Bitwise AND of Numbers Range

class Solution {
public:
    int rangeBitwiseAnd(int left, int right) {
        long cnt = (long)right - left + 1;
        long res = left&right;
        long x = 1;
        for (unsigned i = 0; i < 32; i++, x <<= 1) {
            if (x >= cnt) {
                break;
            }
        }
        res &= -x;

        return res;
    }
};
profile
백엔드 희망

0개의 댓글