如何在 C 中使用按位运算符判断一个数字是否等于另一个数字
How to tell if a number equals another number using bitwise operators in C
我正在编写一个程序,其中我有一个无限循环,如果某些变量 a == 1
仅使用按位运算符,则执行带有停止条件的操作。
我该怎么做?
示例代码:
while(1){
int a;
//do some work
if (a==1){ // how do I say this with bitwise operators and no "!"
break;
}
}
由于 OP 的停止条件是在循环结束时,代码可以使用 do()
循环。
int a;
do {
// some work that sets `a`
} while (a^1);
当a
值为1时,a ^ 1
--> 0
.
我正在编写一个程序,其中我有一个无限循环,如果某些变量 a == 1
仅使用按位运算符,则执行带有停止条件的操作。
我该怎么做?
示例代码:
while(1){
int a;
//do some work
if (a==1){ // how do I say this with bitwise operators and no "!"
break;
}
}
由于 OP 的停止条件是在循环结束时,代码可以使用 do()
循环。
int a;
do {
// some work that sets `a`
} while (a^1);
当a
值为1时,a ^ 1
--> 0
.