且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

golang中没有布尔运算符吗?

更新时间:2022-10-23 13:22:55

没有。 Go不会提供逻辑 异或运算符(即XOR over booleans)和 按位异或运算符仅适用于整数。



但是,独家OR 可以根据其他逻辑运算符进行重写。当重新评估表达式(X和Y)时,

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

或者,更像Jsor指出的那样微不足道,

  X xor Y  -   - > X!= Y 


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.

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