Game with java operators (XOR and AND / OR)

In a program, I try to check two booleans (returned by a function); condition to check:
- only if any of the returned value is true and the other is false, I have a problem;
- else, if both are true or false, I am ready to move on to the next step.

Which of the following two examples would be an effective way to check the status, or is there a better solution?
a and b are integer values ​​on which I check the correctness condition in the isCorrect function, and it returns true or false.

1.

  // checking for the correctness of both a and b if ((isCorrect(a) && !isCorrect(b)) || (!isCorrect(a) && isCorrect(b))) { // a OR b is incorrect } else { // a AND b are both correct or incorrect } 

2.

  // checking for the correctness of both a and b if (! (isCorrect(a) ^ isCorrect(b))) { // a OR b is incorrect } else { // a AND b are correct or incorrect } 


Thanks
Ivar

PS: code readability is not a problem. EDIT: I wanted to have XOR in the second option. In addition, I agree with the parameters == and! =, But what if I had to use logical operators?

+4
source share
4 answers
 if (isCorrect(a) != isCorrect(b)) { // a OR b is incorrect } else { // a AND b are correct or incorrect } 
+6
source

Your test does not need logical operators, only this:

 if (isCorrect(a) == isCorrect(b)) { // they both have the same value } else { // they don't ... } 

EDIT. I deliberately did not use the same comments to reflect that the main purpose of the comment should be to describe the intention, and not a specific implementation. In this case, the simplest statement of intent is that both a and b get the same result.

+6
source

just:

  if (isCorrect(a) == isCorrect(b)) { // a AND b are both correct or incorrect } else { // a OR b is incorrect } 
+2
source

How about this?

 if(isCorrect(a) != isCorrect(b)) { //problem } else { //not a problem } 

You can also use XOR, but! = Works fine and is readable if you are dealing with boolean values, IMO.

+1
source

Source: https://habr.com/ru/post/1336040/


All Articles