Java XNOR operator

后端 未结 4 847
难免孤独
难免孤独 2021-01-07 16:56

I do some research and I wrote some simple programs in java that suits my needs. I used logical operators AND,OR,XOR on integers but I miss XNOR operator. That what I\'m loo

4条回答
  •  忘掉有多难
    2021-01-07 17:16

    if (!a ^ b) doSomething();
    

    It gives the same result so you do not have to use brackets.

    And I would prefer an XOR gate more since a mistake is likely to be made if you accidentally use a = at somewhere which supposed to be a ==.

    if (a = b) doSomething(); // performs like "if (b) doSomething();"
    

    A bitwise version as well.

    someInt = ~a ^ b
    

提交回复
热议问题