473,836 Members | 2,080 Online

# Why isn't there a logical XOR operator?

Of course one can get the effect with appropriate use of existing
operators, but a ^^ operator would make for nice symmetry (as well as
useful to me in something I'm working on). Am I the only one who
would find it useful?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05
80 35166
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.or g> wrote:
Christian Bau <ch***********@ cbau.freeserve. co.uk> writes:
I might assume that a is evaluated completely before b, with a sequence
point in between, so that ^^ would behave as close as possible like &&
or ||.

The "&&" and "||" operators have a sequence point *because* they
short-circuit. Since "^^" doesn't (and can't) short-circuit, adding a
sequence point would be more confusing than not doing so.

Here is the reasoning for the sequence point: && doesn't short circuit
if the left part is true, and || doesn't short circuit if the left part
is false. ^^ should behave as much as possible like an && or || that
doesn't short circuit because of the particular value of the left part.

The sequence point in && and || also makes code like this defined:

if (++i == 2 || i == 3) ...
if (++i >= 10 && i <= 20) ...

Without the sequence point there would be undefined behavior.
Nov 14 '05 #71
In article <bv**********@n ews.tudelft.nl>
<rant>
Due to unfortunate historical accident, the concept of logical/boolean
values has ended up in C as a derived property of expressions, where it
should have been a proper type in itself.
This may indeed be the case -- as Dennis Himself :-) has noted
before, the reason the & and | operators have the "wrong precedence"
(really, their odd position in the ANSI C grammar) is due to a very
early version of the language having no separate "&&" and "||"
operators. Instead, when handling an "if" expression (and perhaps
all "boolean context" expressions such as for and while loops as
well), the compiler treated binary "&" as a logical AND operator.
In other expression contexts, the compiler treated "&" as a bitwise
AND operator.

The operators were split well before 1978 to clean up the situation,
but the grammar was never adjusted to put & and | at "more appropriate"
levels.

(This may well be what you meant; I just thought I would expand
upon the "historical accident" a bit.)

[much snippage]
If I ever were to encounter "(!(a) != !(b))" in production code, well,
suffice it to say I would not be pleased.

If I wanted logical XOR, I would probably do this:

/* LXOR: logical xor. Like && and ||, guarantees left to right
evaluation. Both operands are always evaluated, of course.
The result is the "boolean normalization" of a xor'ed with
the "boolean normalization" of b. */
#define LXOR(a, b) ((a) ? !(b) : !!(b))

and then just use LXOR(expr1, expr2) as needed.

(If I did not care about the order guarantee I might "weaken" the
macro to (!(a) ^ !(b)), and adjust the comment.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #72
CBFalconer wrote:
.... snip ...
Changing this would cause much greater
confusion than "(!(a) != !(b))" and cohorts.

Surely, you jest.

If I ever were to encounter "(!(a) != !(b))" in production code,
well, suffice it to say I would not be pleased.

#include <iso646.h>
#define logicalvalue(x) (not(x))
....
if (logicalvalue(a ) != logicalvalue(b) ) ....
or
if (logicalvalue(a ) xor logicalvalue(b) ) ....

:-)

However I do not jest that the built in expectation of short
circuit performance of && and || should not be confused by yet
another exception for ^^. Bad idea.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
Nov 14 '05 #73
Martin Dickopp <ex************ ****@zero-based.org> wrote in message news:<bv******* ******@news.t-online.com>...
ma****@freemail .gr (stelios xanthakis) writes:
And another problem is multiple xors. What happens then??

a ^^ b ^^ c ????

I guess it would have left-to-right associativity just like &&
and ||, i.e. behave like (a ^^ b) ^^ c. But even if it behaved
like a ^^ (b ^^ c), the result would be the same. Where do you
see a problem?

Martin

The problem is just that this is logically confusing
by itself.

a ^^ b ^^ c

means "not c if a and b are different, and c if a and b are the same".
This is clearly a mess so my point was that although a single ^^
might be useful some times, sequential ^^s are so rare that we
can consider them impossible. Unlike || and && where multiple
such are very frequently used.

A *logical* expression a ^^ b ^^ c ^^ d ^^ e
is beyond the understanding of the average human being.

But no real problem though...

Stelios
Nov 14 '05 #74
ma****@freemail .gr (stelios xanthakis) wrote:
A *logical* expression a ^^ b ^^ c ^^ d ^^ e
is beyond the understanding of the average human being.

Not really. It means "an odd number of these expressions are true". Of
course, the usefulness of that test is debatable, but it's quite
understandable.

Richard
Nov 14 '05 #75
In article <8a************ **************@ posting.google. com> ma****@freemail .gr (stelios xanthakis) writes:
....
A *logical* expression a ^^ b ^^ c ^^ d ^^ e
is beyond the understanding of the average human being.

It is actually quite simple. It is 1 if there are an odd number of
a to e non-zero, 0 otherwise.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #76

Dik T. Winter wrote:
In article <8a************ **************@ posting.google. com> ma****@freemail .gr (stelios xanthakis) writes:
...
> A *logical* expression a ^^ b ^^ c ^^ d ^^ e
> is beyond the understanding of the average human being.

It is actually quite simple. It is 1 if there are an odd number of
a to e non-zero, 0 otherwise.

Which, by the way, could be very different from what someone might want to
express: 'I want an /exclusive/ OR over 5 logical expressions'. That would
sound to me like an expression that yields 1 if exactly one of the 5 logical
expressions is non-zero and 0 otherwise :-) The only simple way I see to
express that in C is: !!(a) + !!(b) + !!(c) + !!(d) + !!(e) == 1.

In other words: while 2-operand logical AND and OR are straightforward to
extend to n-operand, and easy to express using C's && and || operators, an
n-operand (logical) XOR may not be so well defined in everyone's mind and
depending on the chosen definition might not be easy to express with ^ (or
with the imaginary/proposed ^^ operator).

So, although Stelios' expression is well within the understanding of human
beings (I will count myself as average at any rate), it is certainly for me
counterintuitiv e.

Oh boy, is this getting off topic?

--
ir. H.J.H.N. Kenter ^^
Electronic Design & Tools oo ) Philips Research Labs
Building WAY 3.23 =x= \ ar**********@ph ilips.com
Prof. Holstlaan 4 (WAY31) | \ tel. +31 40 27 45334
5656 AA Eindhoven /|__ \ tfx. +31 40 27 44626
The Netherlands (____)_/ http://www.kenter.demon.nl/

Famous last words: Segmentation Fault (core dumped)

Nov 14 '05 #77
In article <40************ **@philips.com. dontspamme> Arjan Kenter <ar**********@p hilips.com.dont spamme> writes:
Dik T. Winter wrote:
In article <8a************ **************@ posting.google. com> ma****@freemail .gr (stelios xanthakis) writes:
...
> A *logical* expression a ^^ b ^^ c ^^ d ^^ e
> is beyond the understanding of the average human being.
It is actually quite simple. It is 1 if there are an odd number of
a to e non-zero, 0 otherwise.

Which, by the way, could be very different from what someone might want to
express: 'I want an /exclusive/ OR over 5 logical expressions'.

In that case he wants a quinary operator not a chain of binary operators
(he should look at lisp...).

Note that the above chain can be written as such by virtue of the
associativity of the operator. If the operator is not associative
(as multiplication on octonions), it *should* be written with
parenthesis. (And in mathematics that is the definition.)
Oh boy, is this getting off topic?

In a sense.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #78
stelios xanthakis wrote:
A *logical* expression a ^^ b ^^ c ^^ d ^^ e
is beyond the understanding of the average human being.

Maybe "average human beings" shouldn't try to write or
maintain non-trivial C programs.

That expression is true if an odd number of a, b, c, d,
and e are true.

- Larry
Nov 14 '05 #79
In article <8a************ **************@ posting.google. com>,
ma****@freemail .gr (stelios xanthakis) wrote:
A *logical* expression a ^^ b ^^ c ^^ d ^^ e
is beyond the understanding of the average human being.

It means "one, three or five of these are true".
Nov 14 '05 #80

This thread has been closed and replies have been disabled. Please start a new discussion.