473,836 Members | 2,080 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 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>
Sidney Cadot <si****@jigsaw. nl> writes:
<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
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #72
Sidney Cadot wrote:
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.


Alright, how about:

#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.
<http://cbfalconer.home .att.net> USE worldnet address!
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.

Similar topics

4
14416
by: user | last post by:
Hi, Is it possible to override assignment, the way that '+' can be overridden for example? Thanks, Toby
14
2346
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
1379
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
8678
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
2218
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
1541
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
11861
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
3533
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
9811
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
10822
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
10532
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...
1
10577
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
9359
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
7774
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
5812
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3103
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.