If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Logical XOR in C/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.

Re: Logical XOR in C/C++?

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.

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 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.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.