簡體   English   中英

如何使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM