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 ...
Printable View
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 ...
There is no logical XOR in C++.
Regards,
Paul McKenzie
The EXOR operator is ^(caret).
Regards,
The Beret.
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++.
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.
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 ));
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
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
====================
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 ^^
What does short-circuit evaluation of logical expressions have to do with the non-implementation of a logical XOR in C++ or C?
-n
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.
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
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
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.Quote:
Originally Posted by nuzzle
Oh, and we can more confidently replace Stitch's suggestion of a function-style macro with an inline function template.
If we use the rules from C: (!(-1) ^ !(-2)) = (0 ^ 0) = 0. This is correct.Quote:
Originally Posted by juncode
I do not see how that matters here since the proposed alternative expression is not a simplification.Quote:
Originally Posted by juncode
It was already pointed out that this would not work in general, back in post #4, some 12 years ago.Quote:
Originally Posted by juncode
Of course. I confused ^ with &&. Clearly !bn gives a result of type bool, which can be then correctly used with bitwise xor ^.
Sorry and thanks for pointing that out!
It depends how you define 'correctly'
the result of 2 bools being bitwise xor'ed is an int. if you expect the result being of type bool, then you may not be getting the result you want. the "correctest" approach for logical xor behaviour is really using a != operator.
with != either of the 2 operators is evaluated exactly once, and the result is a bool.
Using a != will probably also better convey what the code is supposed to do, making it more readable. The only exception I can think of to this is probably trying to actually model logic gates in a circuit.
I'm curious as to whether ...
if ((a == b) != (c == d))
is equivalent to:
if ((a == b) ^^ (c == d))
were the '^^' operator to exist (i.e. the elusive exclusive or operator is just '!=')?
For logical bool != we have the truth table
0 0 -> 0
1 0 -> 1
0 1 -> 1
1 1 -> 0
This is indeed the logical exclusive or (^^ as defined above in post #20)
So in this case, (a == b) != (c ==d)
would give the same logical bool result as (a == b) ^^ (c == d) if ^^ was defined as the logical bool exclusive or.
Note that in this case, the operator ^^ is operating on 2 bools so in this case the bitwise exclusive or (^) would produce the same result
so (a== b) != (c == d)
gives the same result as
(a==b) ^ (c==d)
Note this is only true when the 2 values being compared with != are bool. For any other type ^ is not equal to !=.
Thanks for your reply 2kaud ...
You are correct and also pointing out this was implied in post 20 - I did not intend to plagiarise - I just stumbled on this thread after working it out (and trying it out) for myself.
I guess the key point (answering your last) is that &&, ||, etc. are logical operators intended for use with Boolean variables anyway ...
So the answer to the post in it's simplest form:
"Logical XOR in C/C++?" is...
Yes it's '!='.
... furthermore '==' is a Logical XNOR.
:)