Why no logical Xor operator
D'Arcy J.M. Cain
darcy at druid.uucp
Tue Sep 18 01:05:51 AEST 1990
>from: darcy at druid.uucp (D'Arcy J.M. Cain)
In article <1990Sep12.154515.18460 at 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 at watmath.uucp (Trevor John Thompson)
David Adrien Tanguay <datanguay at watmath.uucp>
chris at mimsy.umd.edu (Chris Torek)
mcrware!jejones at uunet.uucp (James Jones)
leafusa!io!florida!jar at uunet.uucp (Jim Roskind x5570)
henry at zoo.toronto.edu (Henry Spencer)
gwyn at smoke.BRL.MIL (Doug Gwyn)
mit-eddie!ai.mit.edu!tmb at 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 at druid) |
D'Arcy Cain Consulting | MS-DOS: The Andrew Dice Clay
West Hill, Ontario, Canada | of operating systems.
+ 416 281 6094 |
More information about the Comp.std.c
mailing list