简体   繁体   English

如何使用C中的按位运算符判断数字是否等于另一个数字

[英]How to tell if a number equals another number using bitwise operators in C

I am writing a program where I have an infinite loop doing something with a stopping condition to break if some variable a == 1 using only bitwise operators. 我正在编写一个程序,我有一个无限循环做一些停止条件,如果一些变量a == 1只使用按位运算符。

How would I do this? 我该怎么做?

Example code: 示例代码:

while(1){
    int a;
    //do some work
    if (a==1){ // how do I say this with bitwise operators and no "!"
        break;
    }
}

Since OP's stopping condition is at the end of the loop, code can use a do() loop. 由于OP的停止条件是在循环结束时,代码可以使用do()循环。

int a;
do {
  // some work that sets `a`
} while (a^1);

When a has the value of 1, a ^ 1 --> 0 . a的值为1时, a ^ 1 - > 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM