0

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

tadman
  • 208,517
  • 23
  • 234
  • 262

2 Answers2

5

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

Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
0
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <stdint.h>



int main()
{
    int n;
    scanf_s("%d", &n);
    for (int i = 0; i < n; i++) {
        uint16_t  s, d;
        scanf_s("%llu %llu", &s, &d);
        //try for x
        uint16_t x;
        bool stop = false;
        x = s + d;
        printf("%llu", x & 1ULL);
        if ((x & 1ULL) == 0ULL) {

            printf("%llu", x);

            x /= 2;
        }
    }
    return 0;
}

I think this should work anyway for me it works