HiveBrain v1.2.0
Get Started
← Back to all entries
patterngoCritical

Is there no XOR operator for booleans in golang?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
forgolangbooleansoperatorxorthere

Problem

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.

Solution

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

Code Snippets

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

Context

Stack Overflow Q#23025694, score: 168

Revisions (0)

No revisions yet.