Click to See Complete Forum and Search --> : Logical XOR in C/C++?


RufusTheDufus
November 20th, 2001, 03:25 PM
What is the LOGICAL (not binary) XOR operator in C++? You know, like (a && b) or (x || y) ... ?

I know I can use ((a || b) && !(a && b)) but as I need to call a fxn and burn cycles to evaluate one of the operands that is a kludge ...

Paul McKenzie
November 20th, 2001, 06:18 PM
There is no logical XOR in C++.

Regards,

Paul McKenzie

Green_Beret
November 21st, 2001, 12:39 AM
The EXOR operator is ^(caret).

Regards,
The Beret.

NMTop40
November 21st, 2001, 03:18 AM
The ^ XOR symbol performs a numeric XOR of two numbers (but not a logical one).

Thus 1 ^ 2 == 3

Whereas a logical XOR(a,b) would return zero if either both or neither of a and b are zero.

For this there is no operator in C++.

James Curran
November 21st, 2001, 08:58 AM
There is no logical XOR in C++, mainly because, unlike AND and OR, XOR cannot be "short-curcuit" evaluated: With AND and OR, once you've evaluated the first operand, half the time you'll know if there's any need to evaluate the second operand. With XOR, this cannot be done. Both operand must always be evaluated.

In other words:
if ( (a==b) & (c==d) )


generates different code from
if ( (a==b) && (c==d) )


while
if ( (a==b) ^ (c==d) )


would generate exact the same code as
if ( (a==b) ^^ (c==d) )


if the latter were a valid syntax.

The solution: Use a bitwise XOR on the boolean operations, as done in the third example above.


Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

NMTop40
November 22nd, 2001, 07:20 AM
your best way to do it would be

(!b1 ^ !b2)


where b1 and b2 are expressions.

That is, assuming that ! will return the same boolean type for both.

int a=1;
int b=2;

assert (! ( !a ^ !b ));

Ulberon
December 5th, 2001, 02:41 AM
bitwise or " | "

bitwise exlusive or " ^ "

logical or " || "

"To have a lack of imagination, not only shows your lack of intelligence, but also keeps the human race in the shadow of weakness"


-Ulberon

Stitch
December 6th, 2001, 11:57 AM
Using boolean algebra heres an XOR, bit messy but, it will do#define XOR(exp1,exp2) (!((exp1)||(exp2)) || (exp1) && (exp2))

where exp1 is expression one, and exp2..well you get the idea.
Sorry about all the brackets but macro functions need them.

The theory if you need it
XOR = ^a.^b + a.b
XOR = ^(a+b) + a.b (deMoivre)
Just a touch more efficient than the first,right now I'm going home to sleep :)

Stitch
====================

NMTop40
December 6th, 2001, 12:17 PM
easier is (!!(a) ^ !!(b))

The first ! will always produce a numeric type, but you can't guarantee the value of "true".

However, the compiler should give the same "true" value for ! on two numeric types that are both 0.
At least I would assume it does. Thus the second one.

It's a shame you cannot define your own binary operators, thus you cannot create one called ^^

nikb
December 8th, 2001, 11:13 AM
What does short-circuit evaluation of logical expressions have to do with the non-implementation of a logical XOR in C++ or C?

-n

James Curran
December 8th, 2001, 11:39 AM
Basically, the purpose of having both logical and bitwise AND and OR is to allow short-circuit evaluation on the logical expressions. Other wise, you could get by with just the bitwise operators.

You'll note that (as far as I know), no other computer language offers both a bitwise and logical XOR (most just offer bitwise AND and OR and use that for logical expressions)

Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.

nikb
December 8th, 2001, 11:49 AM
I stand corrected. Of course XOR cannot be short-circuited, and to have it as a logical operator would require preventing AND and OR from being short-circuited as well, to maintain language semantics, and a consistent behavior across all logical operators.

-n

KotBegemot
October 23rd, 2009, 10:48 AM
You can use the != operator instead of the XOR in C++,
i.e. (a XOR b) is equivalent to (a != b) assuming a and b are boolean operands

nuzzle
October 23rd, 2009, 11:25 AM
You can use the != operator instead of the XOR in C++,
i.e. (a XOR b) is equivalent to (a != b) assuming a and b are boolean operands

That's right.

Anyway you must have broken some unofficial record here. This thread is over 8 years old and that's a long time even for a zombie thread. :)

laserlight
October 24th, 2009, 02:57 AM
Anyway you must have broken some unofficial record here. This thread is over 8 years old and that's a long time even for a zombie thread.
Heheh, but I guess that one thing might have changed after 8 years: the compilers more commonly in use these days are more standard compliant, so NMTop40's point about not being able to guarantee the value of !x where x is of a built-in or pointer type will no longer hold since C++ guarantees that its result will either be true or false, and true is implicitly convertible to one while false is implicitly convertible to zero, while C guarantees that it will either be 1 or 0.

Oh, and we can more confidently replace Stitch's suggestion of a function-style macro with an inline function template.