按位 AND 的奇怪行为
Curious behaviour of bitwise AND
我正在编码,以下代码没有给出所需的输出。 pos&1 应该在 POS 除以 2 时返回余数。当我用pos%2替换pos&1时,一切正常。可能是什么问题?
#include <iostream>
using namespace std;
int main(){
int y;
unsigned long long int pos;
cin>>y;
cin>>pos;
int f=0;
while(y>0){
y--;
if(pos&1==0){
f=1-f;
}
pos=pos/2;
}
if(f==1){
cout<<"bluen";
}
else
cout<<"redn";
return 0;
}
1==0
比pos&1
更优先。试试if((pos&1)==0){
相关文章:
- 将按位if条件转换为普通if条件
- 为什么 std::vector 需要 is_trivial 才能按位移动,而不仅仅是is_trivially_copya
- C++ 中的按位运算符和重载
- 何时使用按位运算而不是算术替代方法?
- 如何使用按位运算将随机uint64_t转换为范围 (0, 1) 的随机双精度
- GCC constexpr 允许添加,但不允许按位添加或带地址
- 为什么在C++(按位运算符)中(~10)等于-11
- 为什么按位 OR 和 AND 逻辑无法按预期工作?
- 使用按位 AND 和 popcount 而不是实际的 int 或浮点数乘法进行大 (0,1) 矩阵乘法
- 了解按位运算 - 移位和 AND
- 按位函数 AND、OR 和 XOR 在 C++ 中使用十六进制字符串
- C++:这些按位 AND 和以下比较有什么作用?
- 按位 AND with if 语句
- 按位 AND 的奇怪行为
- 不清楚按位 AND 赋值的使用
- SSE 内部函数:屏蔽浮点数并使用按位 and
- 按位 AND/OR 运算符正在追加
- C++:将按位 AND 的结果分配给布尔值
- 为什么 GCC 不能优化逻辑/按位 AND 对"x && (x & 4242)" "x & 4242"?
- 使用 int 和移位执行按位 AND