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

前端 未结 7 1004
一个人的身影
一个人的身影 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:32

    I don't know is it discovered and highlighted, but I noticed a thing that if we add all values together (no matter how many are there) and see what's left after division by 2 we can notice the result is false if 0 left and true if 1 left.

    Example:

    1 ^ 0 ^ 1 ^ 1 = 1 and (1+0+1+1)%2 = 1

    They are the same. Please, correct or guide me who has a clue about this case.

提交回复
热议问题