473,854 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
>>> news:ma******** *****@nwrddc01. gnilink.net...
>>>> 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
nrk
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.

Similar topics

4
14417
by: user | last post by:
Hi, Is it possible to override assignment, the way that '+' can be overridden for example? Thanks, Toby
14
2347
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> using namespace std;
5
1380
by: kenny Nguyen | last post by:
Hi, Does anyone know the operator "=+"? If you do, what special method is related to this operator? Is it the __iadd__ method, but I think this is related to "+=" operator. Thanks, Kenny
67
8682
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used in low-level numerical computations, replacing Fortran in newer projects. Check, for example, sourceforge.net or freshmeat.net But neither language offers a primitive exp operator.
12
2223
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid overloading of == and != but instead translate each call of objA==objB automatically in System.Object.Equals(objA, objB). This would remove inconsistencies like myString1==myString2
33
1542
by: SpreadTooThin | last post by:
Can these operators be overloaded? If so. How?
18
3238
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
6
11864
by: Rudi | last post by:
Hello! I'm searching like a similar syntax for if(): int i=5; if (i in ) doSomething; e.g. enum TaskStates {Idle,Proc1,Proc2, ... ProcN}
19
3537
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class called MyString) and an integer i.e. std::map<MyString, int>. Whenever I insert the elements in the map using the subscript operator, I noticed that the copy constructor for MyString is invoked more number of times than if I do it using the insert()...
0
9903
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11041
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10692
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9525
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7921
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7083
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5753
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5946
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.