473,854 Members | 1,762 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 35183
"Tom St Denis" <to********@iah u.ca> writes:
"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?

You cannot short circuit the XOR logical op. So what's the point?

I already explained in <bv************ *@news.t-online.com> why I would
like to have a ^^ operator even without short-circuiting logic.

Martin
Nov 14 '05 #51

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40******** ********@news.i ndividual.net.. .
"Tom St Denis" <to********@iah u.ca> wrote:
"Jeremy Yallop" <je****@jdyallo p.freeserve.co. uk> wrote in message
news:sl******** ***********@heh e.cl.cam.ac.uk. ..
stelios xanthakis wrote:
> And another problem is multiple xors. What happens then??
>
> a ^^ b ^^ c ????
>
> So xor is rather special

No more than (e.g.) == or < in this respect as far as I can see.

== is a short circuit though.

No, it's not.

hehehe I meant to write "isn't" oops.

Tom
Nov 14 '05 #52

"Martin Dickopp" <ex************ ****@zero-based.org> wrote in message
news:bv******** *****@news.t-online.com...
"Tom St Denis" <to********@iah u.ca> writes:
"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?

You cannot short circuit the XOR logical op. So what's the point?

I already explained in <bv************ *@news.t-online.com> why I would
like to have a ^^ operator even without short-circuiting logic.

how is

if (a ^^ b) { ... }

any diff from

if (a ^ b) { .... }

Even in a stmt you could write

(a^b)?1:0

Tom
Nov 14 '05 #53
> You say that logical xor is used much more rarely than logical and/or.
Question: Has anybody _ever_ found a situation where they would have
wanted a logical xor?

Yes. It involved taking a string, consisting of astericks and spaces,
and making another string of asteriks and spaces, based on some rules.
I think it involved finite automata, or something like that. My
recollection is fuzzy.

And would the !a != !b thing be able to benefit from short circuiting?
If not, then there would be no harm in a logical xor, would there?
Nov 14 '05 #54
In <40***********@ mindspring.com> pete <pf*****@mindsp ring.com> writes:
Dan Pop wrote:

In <i8************ *************** *****@4ax.com> Leor Zolman <le**@bdsoft.co m> writes:
>On 3 Feb 2004 16:04:18 GMT, Joona I Palaste <pa*****@cc.hel sinki.fi>
>wrote:
>
>>xarax <xa***@email.co m> scribbled the following:
>>> "nrk" <ra*********@de vnull.verizon.n et> wrote in message
>>>> Christopher Benson-Manica wrote:
>>>> > 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?
>>>> >
>>>> If you write your conditions to be all logical (that is avoid the if ( x )
>>>> style idioms), then using the existing xor operator would achieve exactly
>>>> the same thing as your proposed ^^, wouldn't it? I mean, there can be no
>>>> short-circuit evaluation of an xor operator.
>>
>>> (a ^^ b) would be something like ((!!a) != (!!b)). Could
>>> be macrotized rather easily? Could the double bang be
>>> reduced to a single bang on each side?
>>
>>Yes, ((!!a) != (!!b)) is equivalent to ((!a) != (!b)).
>
>Huh? That's saying !!a is equivalent to !a. I think not.
>Applying de Morgan's Theorem,
> ((!!a) != (!!b))
>can be rewritten as:
> ! (!a == !b)
>though.

And, guess what? ! (!a == !b) can be rewritten as !a != !b. However,
given that the OP wanted a logical XOR operator, !a ^ !b reflects his
intention better.

However, (!a != !b) reflects the intent of the code,
which is the logical relationship between a and b, better.

Not being the OP, I can't figure out the intent of his code.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #55
In <bv**********@c hessie.cirr.com > Christopher Benson-Manica <at***@nospam.c yberspace.org> writes:
Keith Thompson <ks***@mib.or g> spoke thus:
#define XOR(a, b) (!(a) ^ !(b))

Finding an XOR in code would be rather annoying (to track down the
header file where it's defined), I would think.

Why bother, the semantics should be obvious!

Finding definitions in headers is a piece of cake to anyone with the
right tools. People that maintain software they haven't written have to
do this all the time.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #56
"Tom St Denis" <to********@iah u.ca> writes:
"Martin Dickopp" <ex************ ****@zero-based.org> wrote in message
news:bv******** *****@news.t-online.com...
"Tom St Denis" <to********@iah u.ca> writes:
"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?

You cannot short circuit the XOR logical op. So what's the point?

I already explained in <bv************ *@news.t-online.com> why I would
like to have a ^^ operator even without short-circuiting logic.

how is

if (a ^^ b) { ... }

any diff from

if (a ^ b) { .... }

The assumed semantics of the ^^ operator is that it yields zero if both
operands have (possibly different) non-zero values.

Martin
Nov 14 '05 #57

"Martin Dickopp" <ex************ ****@zero-based.org> wrote in message
news:bv******** *****@news.t-online.com...
"Tom St Denis" <to********@iah u.ca> writes:
"Martin Dickopp" <ex************ ****@zero-based.org> wrote in message
news:bv******** *****@news.t-online.com...
"Tom St Denis" <to********@iah u.ca> writes:

> "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?
>
> You cannot short circuit the XOR logical op. So what's the point?

I already explained in <bv************ *@news.t-online.com> why I would
like to have a ^^ operator even without short-circuiting logic.
how is

if (a ^^ b) { ... }

any diff from

if (a ^ b) { .... }

The assumed semantics of the ^^ operator is that it yields zero if both
operands have (possibly different) non-zero values.

Hmm? I don't get what that means. so

a^^a == 1?
a^^b == 0 if a != b?

or do you mean if a&b != 0 then a^^b == 0 ?

To me this just doesn't seem like a well thought out idea.

Tom

Martin

Nov 14 '05 #58
Tom St Denis wrote:

"Martin Dickopp" <ex************ ****@zero-based.org> wrote in message
news:bv******** *****@news.t-online.com...
"Tom St Denis" <to********@iah u.ca> writes:
> "Martin Dickopp" <ex************ ****@zero-based.org> wrote in message
> news:bv******** *****@news.t-online.com...
> > "Tom St Denis" <to********@iah u.ca> writes:
> >
> > > "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?
> > >
> > > You cannot short circuit the XOR logical op. So what's the point?
> >
> > I already explained in <bv************ *@news.t-online.com> why I
> > would like to have a ^^ operator even without short-circuiting logic.
>
> how is
>
> if (a ^^ b) { ... }
>
> any diff from
>
> if (a ^ b) { .... }
The assumed semantics of the ^^ operator is that it yields zero if both
operands have (possibly different) non-zero values.

Hmm? I don't get what that means. so

a^^a == 1?

a ^^ a would always be expected to be 0.
a^^b == 0 if a != b?

Yes, but only if both a != 0 and b != 0 as well. If a == 10, b == 20, you
would still expect a ^^ b to be 0. That's why its called the "logical" xor
operator. However, the bitwise xor is not 0.
or do you mean if a&b != 0 then a^^b == 0 ?

Yep.
To me this just doesn't seem like a well thought out idea.

Nope. Its precisely how one would expect "logical" xor to function. This
is why there are several messages in this thread suggesting alternatives to
the logical xor such as:
!a != !b
!a ^ !b
!!a ^ !!b

You seem to have some mis-contrued notion of what a logical xor operator is
supposed to do. Consider it as something which maps both its operands to 1
if they are non-zero, 0 otherwise, and then performs a bitwise xor on them.

-nrk.
Tom

Martin

--
Remove devnull for email
Nov 14 '05 #59
"Tom St Denis" <to********@iah u.ca> writes:
"Martin Dickopp" <ex************ ****@zero-based.org> wrote in message
news:bv******** *****@news.t-online.com...
"Tom St Denis" <to********@iah u.ca> writes:
"Martin Dickopp" <ex************ ****@zero-based.org> wrote in message
news:bv******** *****@news.t-online.com...
> "Tom St Denis" <to********@iah u.ca> writes:
>
> > "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?
> >
> > You cannot short circuit the XOR logical op. So what's the point?
>
> I already explained in <bv************ *@news.t-online.com> why I would
> like to have a ^^ operator even without short-circuiting logic.

how is

if (a ^^ b) { ... }

any diff from

if (a ^ b) { .... }
The assumed semantics of the ^^ operator is that it yields zero if both
operands have (possibly different) non-zero values.

Hmm? I don't get what that means. so

a^^a == 1?

No.
a^^b == 0 if a != b?
No.
or do you mean if a&b != 0 then a^^b == 0 ?
No.
To me this just doesn't seem like a well thought out idea.

Is "logical exclusive OR" really that hard to understand? The operator
would yield 1 if one and only one of its operands is 0, and it would
yield 0 otherwise.

Martin
Nov 14 '05 #60

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