C++ uint64_t 按位与检查偶数
C++ uint64_t bitwise AND check for even number
我有以下代码可以简单地检查 uint64_t 是否为偶数,我打算使用按位 AND 运算来检查但它似乎不起作用。
这是我认为首先可用的代码:
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;
如果数字是奇数或偶数,此代码总是打印出 0 或 1,但 if 语句总是 returns false。我究竟做错了什么?谢谢
x&1ULL==0ULL
等价于 x&(1ULL==0ULL)
。你需要 (x&1ULL)==0ULL
.
#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;
}
我认为这对我来说应该有用它有用
我有以下代码可以简单地检查 uint64_t 是否为偶数,我打算使用按位 AND 运算来检查但它似乎不起作用。
这是我认为首先可用的代码:
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;
如果数字是奇数或偶数,此代码总是打印出 0 或 1,但 if 语句总是 returns false。我究竟做错了什么?谢谢
x&1ULL==0ULL
等价于 x&(1ULL==0ULL)
。你需要 (x&1ULL)==0ULL
.
#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;
}
我认为这对我来说应该有用它有用