Is there no XOR operator for booleans in golang?

前端 未结 2 976
清歌不尽
清歌不尽 2021-02-01 00:38

Is there no XOR operator for booleans in golang?

I was trying to do something like b1^b2 but it said it wasn\'t defined for booleans.

相关标签:
2条回答
  • 2021-02-01 01:04

    There is not. Go does not provide a logical exclusive-OR operator (i.e. XOR over booleans) and the bitwise XOR operator applies only to integers.

    However, an exclusive-OR can be rewritten in terms of other logical operators. When re-evaluation of the expressions (X and Y) is ignored,

    X xor Y -> (X || Y) && !(X && Y)
    

    Or, more trivially as Jsor pointed out,

    X xor Y <-> X != Y
    
    0 讨论(0)
  • 2021-02-01 01:05

    With booleans an xor is simply:

    if boolA != boolB {
    
    }
    

    In this context not equal to performs the same function as xor: the statement can only be true if one of the booleans is true and one is false.

    0 讨论(0)
提交回复
热议问题