473,890 Members | 1,977 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 35192
Kevin D. Quitt <KQ**********@I EEIncUNMUNG.com > wrote in message news:<gd******* *************** **********@4ax. com>...
I officially nominate this question as a FAQ, and request that the first
update to the FAQ since February 7, 1999 be made.


I agree.
Nov 14 '05 #31
Kevin D. Quitt <KQ**********@i eeincunmung.com > wrote:

Yes, for the same reason as || and &&, albeit there's no shortcutting. It
makes it *obvious* that a purely boolean expression is involved. !a ^ !b
isn't obviously purely boolean; the central operator is not boolean.


Then use the other exclusive-or operator: !a != !b.

-Larry Jones

I don't need to improve! Everyone ELSE does! -- Calvin
Nov 14 '05 #32
nrk <ra*********@de vnull.verizon.n et> writes:
Keith Thompson wrote:
nrk <ra*********@de vnull.verizon.n et> writes:
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.


The result will be an expression that breaks the first time you feed
it an operand that doesn't have the value 0 or 1. This will happen at
the most inconvenient possible time.


Read carefully. What I meant was that, you couldn't write:
if ( x ^ y ),
but you could write:
if ( (x != 0) ^ (y != 0) )


Yeah, that would work. (I think it's ugly, but it would work.)

If I really had a need for logical xor, I'd probably use a macro,
something like:

#define XOR(a, b) (!(a) ^ !(b))

(I think that's right.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #33
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.

--
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 #34
Kevin D. Quitt <KQ**********@I EEIncUNMUNG.com > writes:
I officially nominate this question as a FAQ, and request that the first
update to the FAQ since February 7, 1999 be made.


For what it's worth, I've added a page to my own personal FAQ
list:
http://www.msu.edu/~pfaffben/writing...gical-xor.html
Anyone have anything to add or dispute?
--
"It would be a much better example of undefined behavior
if the behavior were undefined."
--Michael Rubenstein
Nov 14 '05 #35

On Tue, 3 Feb 2004, Ben Pfaff wrote:

For what it's worth, I've added a page to my own personal FAQ
list:
http://www.msu.edu/~pfaffben/writing...gical-xor.html
Anyone have anything to add or dispute?


Nit: "Far more rarely useful"? :) How about "far less often"?

And I would appreciate a clarification about how !a != !b interacts
with doubles, and some newbie no doubt would appreciate a clarification
about how it interacts with pointers. (!NULL == (int)1, !foo == (int)0;
but I am still waiting for someone more often correct than I to address
the question of !NaN.)

-Arthur
Nov 14 '05 #36
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> writes:
On Tue, 3 Feb 2004, Ben Pfaff wrote:

For what it's worth, I've added a page to my own personal FAQ
list:
http://www.msu.edu/~pfaffben/writing...gical-xor.html
Anyone have anything to add or dispute?
Nit: "Far more rarely useful"? :) How about "far less often"?


Yes, that's much better phrasing. I will update it.
And I would appreciate a clarification about how !a != !b interacts
with doubles, and some newbie no doubt would appreciate a clarification
about how it interacts with pointers. (!NULL == (int)1, !foo == (int)0;
but I am still waiting for someone more often correct than I to address
the question of !NaN.)


I'm not sure what you'd like me to say about those cases. It
seems to me that, with the possible exception of NaN, they work
out okay.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Nov 14 '05 #37
nrk
Ben Pfaff wrote:
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> writes:
On Tue, 3 Feb 2004, Ben Pfaff wrote:

For what it's worth, I've added a page to my own personal FAQ
list:
http://www.msu.edu/~pfaffben/writing...gical-xor.html
Anyone have anything to add or dispute?


Nit: "Far more rarely useful"? :) How about "far less often"?


Yes, that's much better phrasing. I will update it.
And I would appreciate a clarification about how !a != !b interacts
with doubles, and some newbie no doubt would appreciate a clarification
about how it interacts with pointers. (!NULL == (int)1, !foo == (int)0;
but I am still waiting for someone more often correct than I to address
the question of !NaN.)


I'm not sure what you'd like me to say about those cases. It
seems to me that, with the possible exception of NaN, they work
out okay.


I am not sure if its in the C standard, but I believe NaN is to compare
unequal to any other number and even to NaN itself. Since ! yields 0 if
the operand is not equal to 0, if the above were true, one would expect
!NaN to yield 0.

-nrk.

--
Remove devnull for email
Nov 14 '05 #38
Ben Pfaff <bl*@cs.stanfor d.edu> writes:
Kevin D. Quitt <KQ**********@I EEIncUNMUNG.com > writes:
I officially nominate this question as a FAQ, and request that the first
update to the FAQ since February 7, 1999 be made.


For what it's worth, I've added a page to my own personal FAQ
list:
http://www.msu.edu/~pfaffben/writing...gical-xor.html
Anyone have anything to add or dispute?


Other than the URL, there's no mention of the common abbreviation "xor";
there probably should be.

Another thing I just noticed: <iso646.h> defines the following
macros:

and &&
and_eq &=
bitand &
bitor |
compl ~
not !
not_eq !=
or ||
or_eq |=
xor ^
xor_eq ^=

The "and", "or", and "not" macros refer to the logical operators, but
the "xor" macro refers to the bitwise operator. If a logical-xor "^^"
operator were added, the asymmetry would be painful.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #39
> Ben Pfaff <bl*@cs.stanfor d.edu> writes:
Kevin D. Quitt <KQ**********@I EEIncUNMUNG.com > writes:
I officially nominate this question as a FAQ, and request that the first
update to the FAQ since February 7, 1999 be made.


For what it's worth, I've added a page to my own personal FAQ
list:
http://www.msu.edu/~pfaffben/writing...gical-xor.html
Anyone have anything to add or dispute?


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?
Nov 14 '05 #40

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

Similar topics

4
14422
by: user | last post by:
Hi, Is it possible to override assignment, the way that '+' can be overridden for example? Thanks, Toby
14
2348
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
1381
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
8686
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
2228
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
1543
by: SpreadTooThin | last post by:
Can these operators be overloaded? If so. How?
18
3242
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
6
11865
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
3540
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
11207
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...
1
10896
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9612
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
8000
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
7153
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
5830
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
6031
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4652
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.