
November 20th, 2001, 03:25 PM
#1
Logical XOR in C/C++?
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 ...

November 20th, 2001, 06:18 PM
#2
Re: Logical XOR in C/C++?
There is no logical XOR in C++.
Regards,
Paul McKenzie

November 21st, 2001, 12:39 AM
#3
Re: Logical XOR in C/C++?
The EXOR operator is ^(caret).
Regards,
The Beret.

November 21st, 2001, 03:18 AM
#4
Re: Logical XOR in C/C++?
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++.

November 21st, 2001, 08:58 AM
#5
Re: Logical XOR in C/C++?
There is no logical XOR in C++, mainly because, unlike AND and OR, XOR cannot be "shortcurcuit" 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.

November 22nd, 2001, 07:20 AM
#6
Re: Logical XOR in C/C++?
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 ));

December 5th, 2001, 02:41 AM
#7
Re: Logical XOR in C/C++?
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

December 6th, 2001, 11:57 AM
#8
Re: Logical XOR in C/C++?
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
====================

December 6th, 2001, 12:17 PM
#9
Re: Logical XOR in C/C++?
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 ^^

December 8th, 2001, 11:13 AM
#10
Re: Logical XOR in C/C++?
What does shortcircuit evaluation of logical expressions have to do with the nonimplementation of a logical XOR in C++ or C?
n

December 8th, 2001, 11:39 AM
#11
Re: Logical XOR in C/C++?
Basically, the purpose of having both logical and bitwise AND and OR is to allow shortcircuit 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.

December 8th, 2001, 11:49 AM
#12
Re: Logical XOR in C/C++?
I stand corrected. Of course XOR cannot be shortcircuited, and to have it as a logical operator would require preventing AND and OR from being shortcircuited as well, to maintain language semantics, and a consistent behavior across all logical operators.
n

October 23rd, 2009, 10:48 AM
#13
Re: Logical XOR in C/C++?
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

October 23rd, 2009, 11:25 AM
#14
Re: Logical XOR in C/C++?
Originally Posted by KotBegemot
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.

October 24th, 2009, 02:57 AM
#15
Re: Logical XOR in C/C++?
Originally Posted by nuzzle
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 builtin 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 functionstyle macro with an inline function template.
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!
