位运算一道题

在这里插入图片描述
在这里插入图片描述

#include <bits/stdc++.h>

using namespace std;
int A[32];
int a, x, t;
int ans, c, bit, bit2;

int main() {
    
    
    for (int i = 0; i < 31; ++i)
        A[i] = pow(2, i);
    scanf("%d", &t);
    while (t--) {
    
    
        scanf("%d%d", &a, &x);
        for (bit = (1 << 30), ans = 0; bit; bit >>= 1) {
    
    
            if ((bit & x) == 0) continue;
            for (bit2 = (bit >> 1), c = 0; bit2; bit2 >>= 1)
                if ((bit2 & a) == 0) c++;
            ans += A[c];
            if (bit & a) break;
        }
        if ((a | x) == (a + x)) ans++;
        printf("%d\n", ans - 1);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_46264636/article/details/112478249