Search code examples
cbit-manipulationbitwise-operatorsbitwise-anduint64

C++ uint64_t bitwise AND check for even number


I have the following code that simply checks if a uint64_t is even, I intended on using a bitwise AND operation to check but it doesn't seem to be working.

This is the code I thought would work first:

int n;
scanf("%d",&n);
for(int i = 0; i < n; i++){
    uint64_t s,d;
    scanf("%llu %llu",&s,&d);
    //try for x
    uint64_t x;
    bool stop = false;
    x = s + d;
    printf("%llu",x&1ULL); \\ This prints 0 when the number is even but
    if(x&1ULL==0ULL){ \\ This check always returns false
        printf("%llu",x);
        x/= 2;

This code always prints out 0 or 1 if the the number is odd or even but the if statement always returns false. What am I doing wrong? Thanks


Solution

  • x&1ULL==0ULL is equivalent to x&(1ULL==0ULL). You need (x&1ULL)==0ULL.