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

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

    Here's a Java 8 way of determining if exactly one boolean is true:

    Stream.of(b1, b2, b3, ...)
            .filter(b -> b)
            .count() == 1;
    
    0 讨论(0)
提交回复
热议问题