C history question

Richard Tobin richard at aiai.ed.ac.uk
Tue Sep 12 03:21:00 AEST 1989


In article <575 at calmasd.Prime.COM> wlp at calmasd.Prime.COM (Walter Peterson) writes:
>C has bitwise operators for AND (&), OR (|) and XOR (^) and boolean 
>operator for AND (&&) and OR (||), but not for XOR (^^). Why?

Well, && differs from & in two ways: it treats its arguments just as
zero or non-zero (rather than bitwise) and it does "short-circuit"
evaluation (that is, in A && B, if A is false B will not be evaluated).

Certainly a non-bitwise xor makes sense, but a short-circuit one doesn't.

>Most assemblers that I know have XOR as a single instruction 

Ah, but what they have is a bitwise-xor instruction, which is what C
*does* provide.

>so why make people go to the trouble of writing something like
>(a || b) && (!(a && b)) when a ^^ b is so much "cleaner".

I'd guess it's fairly rare compared with && and ||.  If you want it,
((a==0) != (b==0)) will give the result you want, and probably not be
any less efficient than it should be.

Note that C also doesn't provide a bitwise equivalence operator
"exclusive nor").  As an aside, I was amused by the code produced by
gcc for ~(a^b) - while cc produces (on a Sparc) the xnor instruction,
gcc produces this:

        xor %i0,%i1,%i0
        xnor %g0,%i0,%i0

(it's using xnor just to do the negation).

-- Richard
-- 
Richard Tobin,                       JANET: R.Tobin at uk.ac.ed             
AI Applications Institute,           ARPA:  R.Tobin%uk.ac.ed at nsfnet-relay.ac.uk
Edinburgh University.                UUCP:  ...!ukc!ed.ac.uk!R.Tobin



More information about the Comp.lang.c mailing list