Topic: All this talk about adding operators to the language


Author: jgp@fct.unl.pt (Jose Goncalo Pedro)
Date: 9 Apr 92 16:40:19 GMT
Raw View
In article <1992Apr1.163806.10318@prl.dec.com> weikart@prl.dec.com (Chris Weikart) writes:
   In article <1992Mar31.225519.1589@prl.dec.com>, weikart@prl.dec.com (Chris Weikart) writes:
   > In article <1992Mar4.002846.18098@wdl.loral.com>, bard@cutter.ssd.loral.com (James Woodyatt) writes:
   > > ...is making me wonder how come there's no `^^' operator to perform
   > > logical exclusive OR? It's not often I wish for one, but most often
   > > it's because I'm trying to decipher what some bozo hacked up to do
   > > the job of a `^^' operator without explaining himself clearly.
   > >
   > > Usually, I use something like this:
   > >
   > > if (!!a ^ !!b) { ... }
   > >
   > > ...
   >
   > I *always* use something like this:
   >
   >  if (a != b) { ... }
   >
   > 8-}
   >

   If a and b are integer, and not strictly boolean, then something like this:

    if ((a != 0) != (b != 0)) { ... }

   should do the trick.

   -------------------------------------------------------------------------------
   Chris Weikart <weikart@prl.dec.com> .............. DEC Paris Research Lab (PRL)

You should try
 if (!a != !b)
or
 if (!a == !b)
for logical exclusive NOR (NXOR)

bye
-jgp
--
Jose Goncalo Pedro                    BITNET/Internet: jgp@fct.unl.pt
 +---------------------------------+               UUCP: jgp@unl.uucp
 |   Departamento de Informatica   +----------------------------------+
 |   Universidade Nova de Lisboa      2825 Monte Caparica, PORTUGAL   |
 +--------------------------------------------------------------------+




Author: bard@cutter.ssd.loral.com (James Woodyatt)
Date: Wed, 4 Mar 1992 00:28:46 GMT
Raw View
...is making me wonder how come there's no `^^' operator to perform
logical exclusive OR? It's not often I wish for one, but most often
it's because I'm trying to decipher what some bozo hacked up to do
the job of a `^^' operator without explaining himself clearly.

Usually, I use something like this:

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

But some of the contortions I've seen are ridiculous.

It seems like it would be natural to define `a ^^ b' as yielding the
same result as `!!a ^ !!b'.

I'm sure there's a good reason why it's conspicuously absent -- I'm
just too dumb to deduce it. Would someone please enlighten me?

Thank you.


--
James Woodyatt  Space Systems/Loral   Palo Alto, CA   bard@cutter.ssd.loral.com

i am the messanger and here is the message




Author: Bruce.Hoult@bbs.actrix.gen.nz
Date: Sat, 7 Mar 1992 05:55:42 GMT
Raw View
In article <1992Mar4.002846.18098@wdl.loral.com> bard@cutter.ssd.loral.com (James Woodyatt) writes:
> ...is making me wonder how come there's no `^^' operator to perform
> logical exclusive OR? It's not often I wish for one, but most often
> it's because I'm trying to decipher what some bozo hacked up to do
> the job of a `^^' operator without explaining himself clearly.


This doesn't seem to make any sense.  The point of && and || is that
in each case you often don't need to evaluate the second half -- if
the LHS of && is false then the whole thing is false and you can
forget the RHS, if the LHS of || is true then the wholething is true
and you can forget the RHS.

No similar possibilities exist for a possible ^^ operator.


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

That's horrible!  For valid logical values !!A == A, making this trick
quite counter-intuitive.  Besides, on simple compilers it will take more
code than will the far more obvious "if ((a!=0) ^ (b!=0)) { ... }". (gcc
on the 680x0 produces the same code in either case, but Apple's MPW C
does much better on my code.

--
Bruce.Hoult@bbs.actrix.gen.nz   Twisted pair: +64 4 477 2116
BIX: brucehoult                 Last Resort:  PO Box 4145 Wellington, NZ
"Cray's producing a 200 MIPS personal computer with 64MB RAM and a 1 GB
hard disk that fits in your pocket!"   "Great!  Is it PC compatable?"




Author: johnkal@microsoft.com (John Kallen)
Date: 13 Mar 92 23:12:16 GMT
Raw View
In article <1992Mar4.002846.18098@wdl.loral.com> bard@cutter.ssd.loral.com (James Woodyatt) writes:
:...is making me wonder how come there's no `^^' operator to perform
:logical exclusive OR? It's not often I wish for one, but most often
:it's because I'm trying to decipher what some bozo hacked up to do
:the job of a `^^' operator without explaining himself clearly.
:
:Usually, I use something like this:
:
:if (!!a ^ !!b) { ... }
:
:But some of the contortions I've seen are ridiculous.
:
:It seems like it would be natural to define `a ^^ b' as yielding the
:same result as `!!a ^ !!b'.
:
:I'm sure there's a good reason why it's conspicuously absent -- I'm
:just too dumb to deduce it. Would someone please enlighten me?
:
:Thank you.

I've wondered about this too. Look at || and && -- the short-ciruiting
logical operators. A || can short circuit when it sees the first 'true'
value, and a && can short circuit when it sees the first 'false' value.
The ^^ operator would have to look at both operands to determine the
XOR, which  makes its semantical difference from ^ very small: ^^
operates with logical values, ^ operates with bit arithmetic.

:--
:James Woodyatt  Space Systems/Loral   Palo Alto, CA
bard@cutter.ssd.loral.com : :i am the messanger and here is the message
_______________________________________________________________________________
 | |   |   |    |\ ||  /|\ |    "Stonehenge is very nutritious" - Migato Gami
 | |\ \|/ \|  * |/ ||/| |  |
 | |\ /|\  |\ * |\ |  | |  |    Microsoft couldn't care less about what I say.
_|_|___|___|____|_\|__|_|__|____johnkal@microsoft.com__________________________