golang中没有布尔运算符吗? [英] Is there no XOR operator for booleans in golang?

查看:944
本文介绍了golang中没有布尔运算符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在尝试做类似于 b1 ^ b2 但它表示它没有为布尔值定义。

解决方案

没有。 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

这篇关于golang中没有布尔运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆