XOR of three values

前端 未结 8 927
执念已碎
执念已碎 2020-12-02 23:04

What is the simplest way to do a three-way exclusive OR?

In other words, I have three values, and I want a statement that evaluates to true IFF only one of

8条回答
  •  無奈伤痛
    2020-12-02 23:33

    a^b^c is only 1 if an uneven number of variables is 1 (two '1' would cancel each other out). So you just need to check for the case "all three are 1":

    result = (a^b^c) && !(a&&b&&c)
    

提交回复
热议问题