🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #1718

Re: Why no logical Xor operator

#1718
From: D'Arcy J.M. Cain
Date: Mon, 17 Sep 1990 15:05
50 lines
2237 bytes
>from: darcy@druid.uucp (D'Arcy J.M. Cain)

In article <1990Sep12.154515.18460@druid.uucp> I wrote:
>Recently I had occasion to write something like the following code:
>
>	if ((!x && (y & z)) || (x && !(y & z))
>		something();
>
>Which would have been simpler as:
>
>	if (x ^^ (y & z))
>		something();
>
>If there was such a thing as a '^^' operator.  Does anyone know why C left
>out the logical XOR operator?

Many thanks to the following for their input

mks!tj@watmath.uucp (Trevor John Thompson)
David Adrien Tanguay <datanguay@watmath.uucp>
chris@mimsy.umd.edu (Chris Torek)
mcrware!jejones@uunet.uucp (James Jones)
leafusa!io!florida!jar@uunet.uucp (Jim Roskind x5570)
henry@zoo.toronto.edu (Henry Spencer)
gwyn@smoke.BRL.MIL (Doug Gwyn)
mit-eddie!ai.mit.edu!tmb@gatech.uucp (Thomas M. Breuel)

The responses generally suggested a better way to code the above as well
as opining about the reasons for not including the '^^' operator in C.

The consensus on the best way to code can best be expressed by Chris Torek's
suggestion which is in the form of the following macro:

	#define XOR(a, b) (((a) != 0) != ((b) != 0))
or the more terse:
	#define XOR(a, b) (!(a) != !(b))

As for the rationale, there were two points which were generally made.  The
operator would not add any functionality to the language and there is no
opportunity to short-circuit the test.  While I certainly understand the
reasons, I can't help but think that simplifying the programmer's task
should be considered as well.  The macro, while helpful, isn't as intuitive
as projecting the use of '&&' and '||' to '^^' and the latter seems to
be a more consistent method from a stylistic point of view.

-- 
D'Arcy J.M. Cain (darcy@druid)     |
D'Arcy Cain Consulting             |   MS-DOS:  The Andrew Dice Clay
West Hill, Ontario, Canada         |   of operating systems.
+ 416 281 6094                     |
Technical Details
Message-ID: <1990Sep17.150551.9797@druid.uucp>
Path: arc1-ca.pugleaf.net!utzoo!attcan!telly!druid!darcy

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!telly!druid!darcy
From: darcy@druid.uucp (D'Arcy J.M. Cain)
Newsgroups: comp.std.c
Subject: Re: Why no logical Xor operator
Message-ID: <1990Sep17.150551.9797@druid.uucp>
Date: 17 Sep 90 15:05:51 GMT
Organization: D'Arcy Cain Consulting, West Hill, Ontario
Lines: 50