473,889 Members | 2,022 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
Martin Dickopp <ex************ ****@zero-based.org> writes:
"Tom St Denis" <to********@iah u.ca> writes:
or do you mean if a&b != 0 then a^^b == 0 ?


No.


Sorry, that should have been "yes." It would in fact be a property of
the ^^ operator.

Martin
Nov 14 '05 #61
Tom St Denis 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. The intention would be that ^^ would be a
short-circuit (otherwise I don't see the point) and as the others pointed
out you have to evaluate both sides to determine the result (therefore it
cannot short circuit).

What you could do is make ^& and ^| for NAND and NOR (former shorts if left
is zero, latter if left is one) and you'd have all four short-circuits ;-)

Well actually make ^& into ^^ since it's ambiguous with ^&varname, etc...so
you get

^^ = NAND
^| = NOR
|| = OR
&& = AND


I'd prefer a !&& and a !|| operator.

These would go handsomly alongside ||| and &&& operators, but that's
another story.
Regards,

sidney

Nov 14 '05 #62
pete wrote:
Christian Bau wrote:
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?

(!(a) != !(b)) is adequate for the situation.


Well, by the same reasoning we could do away with !=, being as it is that

!((a)==(b)) is adequate for the situation.

The whole point of this discussion is that C obviously lacks a logical
XOR operator, and whether there's a good reason for it. Adequacy doesn't
enter into it, otherwise !, &&, <, and > would suffice.

For my part, I'd be happy to see a ^^ but only if we get logical
"implies" and "equiv" operators as well.

Best regards,

Sidney

Nov 14 '05 #63
Sidney Cadot <si****@jigsaw. nl> writes:
Tom St Denis wrote:

[...]
What you could do is make ^& and ^| for NAND and NOR (former shorts if left
is zero, latter if left is one) and you'd have all four short-circuits ;-)

Well actually make ^& into ^^ since it's ambiguous with ^&varname, etc...so
you get

^^ = NAND
^| = NOR
|| = OR
&& = AND


I'd prefer a !&& and a !|| operator.

These would go handsomly alongside ||| and &&& operators, but that's
another story.


Right, because C doesn't look enough like modem line noise as it is.
8-)}

--
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 #64
Ben Pfaff <bl*@cs.stanfor d.edu> wrote in message news:<87******* *****@pfaff.sta nford.edu>...
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?


Short-circuit evaluation is not a _requirement_ of logical operators.
Many other ('old') languages have logical-and and logical-or operators
which are not short-circuited.

I think a more tangable example for the second case would be better.
Otherwise you seem to be dismissing out of hand the 'kludgy' look of
the != alternative. Perhaps...

((a < 0) != (b < 0)) /* one and only one operator is negative
*/

Thirdly, whilst practical use of logical xor is rare, it's rare
because it's relatively complex (in terms of NOT, AND and OR
operations which _easily_ cover 14 of the 16 binary boolean
operations). But when it is needed, it's absense is felt. It wouldn't
be particularly difficult for implementors to cater for.

Lastly, I think the principle reason is quite simply because Dennis
Ritchie didn't think it was necessary at the time (and possibly not
even now.) But AFAIK, there was no serious issue preventing it from
being put into C90 nor C99.

Keith Thompson's point about the macro xor is a good one, but I think
it simply highlights an error in naming (in C++?)

--
Peter
Nov 14 '05 #65
Martin Dickopp wrote:
"Tom St Denis" <to********@iah u.ca> writes:
.... snip ...
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.


As others have pointed out, all other logical operators
short-circuit. xor cannot, since the second expression absolutely
must be evaluated. Changing this would cause much greater
confusion than "(!(a) != !(b))" and cohorts.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #66
In article
<h_************ *******@twister 01.bloor.is.net .cable.rogers.c om>,
"Tom St Denis" <to********@iah u.ca> wrote:
how is

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

any diff from

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


I would assume that a ^^ b would only check whether a is zero or
nonzero, and whether b is zero or nonzero, and I would assume that a and
b would be allowed to be floating point numbers or pointers.

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 ||.
Nov 14 '05 #67

On Wed, 4 Feb 2004, CBFalconer wrote:

Martin Dickopp wrote:
"Tom St Denis" <to********@iah u.ca> writes:

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


As others have pointed out, all other logical operators
short-circuit. xor cannot, since the second expression absolutely
must be evaluated. Changing this would cause much greater
confusion than "(!(a) != !(b))" and cohorts.


*Nobody* is suggesting changing the short-circuiting semantics
of && or ||. But several people *are* suggesting the addition of
a third logical operator, ^^, which performs a logical XOR on its
two operands (after converting them to "boolean" 0 or 1).
The logical XOR, naturally, cannot short-circuit; there's nowhere
for it to short-circuit "to," if you get my drift. However, that
fact has nothing to do with the short-circuiting or not of && and ||.
& : Computes the bitwise AND of its operands.
| : Computes the bitwise OR of its operands.
^ : Computes the bitwise XOR of its operands.

&& : Computes the logical AND of its operands in an efficient
left-to-right manner.
|| : Computes the logical OR of its operands in an efficient
left-to-right manner.
^^ (proposed): Computes the logical XOR of its operands in an
efficient left-to-right manner.

So AIUI, the logical XOR operator would introduce a sequence
point between the evaluation of its operands (which incidentally
none of the suggested remedies do), and it would compute the
answer in a manner "as short-circuited as possible" -- i.e.,
not short-circuited at all.
As the fact that ^^ cannot short-circuit is apparent to anyone
with the ability to think, I don't think its not short-circuiting
is likely to confuse anyone with the ability to program. :-)

[Again: I'm a supporter of the status quo out of pessimism,
but I will attempt to demolish arguments that support the status
quo by reasoning alone -- because I think the omission of ^^
*was* a bad idea. Why hack up solutions involving != when what
the algorithm demands is a logical XOR? (...Because that's the
only way the language supports, is why. No deeper reason.) ]

-Arthur
Nov 14 '05 #68
CBFalconer wrote:
Martin Dickopp wrote:
"Tom St Denis" <to********@iah u.ca> writes:

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

As others have pointed out, all other logical operators
short-circuit.


Or, as it was once so aptly put: things that are to float must not be
made of stuff that's heavier than water, because the only things we see
now that float are made of stuff that is less heavy than water.
xor cannot, since the second expression absolutely
must be evaluated.
IMO, the short-circuiting property of && and || is incidental; their
being logical operators instead of binary operators is essential.

<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 is an untidy aspect of
the type system; any properly designed language (which, for all its
merits, C is /not/) has a separate Boolean type, and, therefore,
separate operators and/or operator semantics for Boolean values
(although, perhaps, overloaded on operators that are also meaningful on
other types).

In my opinion, the C approach to logical/boolean values is fundamentally
broken. I would applaud any attempt to plug this particular hole by
widening the difference between bitwise operations on integers, and
logical operations on Booleans (such as introduction of a true logical
XOR), although the flaw is essentially at a deeper level than
introduction of a new operator could ever hope to fix.
</rant>

So, the decision on whether to do short-circuit evaluation or not (if at
all possible) is a question to consider /after/ you decide to properly
distinguish integer/boolean types (or bitwise/logical operators), as in
C). These are separate issues, with the former taking precedence over
the latter. At least, that's how I feel about it.
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.
Best regards,

Sidney

Nov 14 '05 #69
Christian Bau <ch***********@ cbau.freeserve. co.uk> writes:
In article
<h_************ *******@twister 01.bloor.is.net .cable.rogers.c om>,
"Tom St Denis" <to********@iah u.ca> wrote:
how is

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

any diff from

if (a ^ b) { .... }
I would assume that a ^^ b would only check whether a is zero or
nonzero, and whether b is zero or nonzero, and I would assume that a and
b would be allowed to be floating point numbers or pointers.


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

If I were designing the C language from scratch, I'd probably add a
"^^" operator just for symmetry. But it's too late now. Even if we
could convince the committee to add "^^" to the C200Y standard, we'd
probably be stuck for the next 10 to 20 years with old compilers that
don't implement it. Any code that uses the new operator either
wouldn't be portable to the older compilers, or would have to use #if
tricks to use a C99-compatible XOR on compilers that don't support
"^^" -- which would be far uglier than any of the workarounds we've
already seen here.

Inertia, like gravity, sucks.

<OT>
Perl, whose expression syntax is heavily influenced by C, has "^" as a
bitwise xor operator and "xor" as a logical xor operator.
</OT>

--
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 #70

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
9970
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
9810
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
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
10443
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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.
3
3259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.