In Java XOR with three true inputs returns true. Why?

前端 未结 7 1001
一个人的身影
一个人的身影 2021-02-08 00:42

The following code

System.out.println(\"1 0 0: \" + (true ^ false ^ false));
System.out.println(\"1 0 1: \" + (true ^ false ^ true));
System.out.println(\"1 1 0:         


        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-08 01:29

    Because true xor true = false, and false xor true is true. xor is associative, so group the values any way you please!

    To decide that only one of them is true, you could add the values together as integers and see if the answer is 1.

    I'm answering this as a general programming question, it really isn't particular to Java.

提交回复
热议问题