[comp.lang.c] \"logical\" xor

bard@THEORY.LCS.MIT.EDU (Bard Bloom) (01/12/88)

In article <2946@zeus.TEK.COM> dant@tekla.UUCP (Dan Tilque) writes:
> ...
>Tell me, what is the ^^ supposed to add that is not already done by ^ ?
>The answer is nothing.  The "bitwise" xor does exactly what you would want
>a "logical" xor to do.  In fact, the terms "bitwise" and "logical" are
>misnomers.  They should be "unoptimized" and "optimized".  And there's no
>way to optimize an exclusive-or operation.
>
>The only reason to add ^^ is esthetic.  ...
> ...

How about

   011             011
 ^ 110          ^^ 110
--------        ------
   101             000

The bitwise xor operator ^ treats its arguments as bit strings; it can happen
that the xor of two non-zero bit strings is non-zero.  

The logical xor operator ^^ treats its arguments as logical values.  The xor
of two non-zero (i.e., TRUE) values is zero. 

Not only do they yeild different values, they yeild different truth values.
So, if you use the wrong one in a conditional, the conditional can take the
wrong branch.

Perhaps Tilque intended to say that they agree on single bits, and he's
right about that.  In fact, the following equation holds:

  a ^^ b   ===  (a==0) ^ (b==0)
           ===  !!a    ^ !!b

-- Bard Bloom