简体   繁体   中英

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.

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.

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

When a has the value of 1, a ^ 1 --> 0 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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