Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!uakari.primate.wisc.edu!aplcen!ginosko!uunet!mcsun!ukc!edcastle!aiai!richard From: richard@aiai.ed.ac.uk (Richard Tobin) Newsgroups: comp.lang.c Subject: Re: C history question Keywords: C design, XOR Message-ID: <868@skye.ed.ac.uk> Date: 11 Sep 89 17:21:00 GMT References: <575@calmasd.Prime.COM> Reply-To: richard@aiai.UUCP (Richard Tobin) Organization: AIAI, University of Edinburgh, Scotland Lines: 37 In article <575@calmasd.Prime.COM> wlp@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@uk.ac.ed AI Applications Institute, ARPA: R.Tobin%uk.ac.ed@nsfnet-relay.ac.uk Edinburgh University. UUCP: ...!ukc!ed.ac.uk!R.Tobin