Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there no XOR operator for booleans in golang?

Tags:

go

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.

like image 322
Charlie Parker Avatar asked Apr 12 '14 03:04

Charlie Parker


People also ask

Is XOR a Boolean operator?

XOR is one of the sixteen possible binary operations on Boolean operands. That means that it takes 2 inputs (it's binary) and produces one output (it's an operation), and the inputs and outputs may only take the values of TRUE or FALSE (it's Boolean) – see Figure 1.

What is XOR in Boolean?

XOR gate (sometimes EOR, or EXOR and pronounced as Exclusive OR) is a digital logic gate that gives a true (1 or HIGH) output when the number of true inputs is odd. An XOR gate implements an exclusive or ( ) from mathematical logic; that is, a true output results if one, and only one, of the inputs to the gate is true.

Is XOR a Boolean operator in Python?

XOR Operator in Python is also known as “exclusive or” that compares two binary numbers bitwise if two bits are identical XOR outputs as 0 and when two bits are different then XOR outputs as 1. XOR can even be used on booleans.

What is the operator for XOR?

XOR is a bitwise operator, and it stands for "exclusive or." It performs logical operation. If input bits are the same, then the output will be false(0) else true(1).


2 Answers

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 
like image 192
user2864740 Avatar answered Oct 13 '22 23:10

user2864740


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.

like image 44
Alasdair Avatar answered Oct 13 '22 21:10

Alasdair