XOR operator (was Re: C history question)

Ken Johnson ken at aiai.ed.ac.uk
Tue Sep 12 02:25:15 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
>make people go to the trouble of writing something like
>(a || b) && (!(a && b)) when a ^^ b is so much "cleaner".

I can't tell you why ^^ isn't in the language, but note that A^^B would
probably have a property that the above longer expression does not have,
namely: A && B and A || B each evaluate the second argument only if
necessary, and at most once. 

In some cases, evaluating A ^^ B by your suggested method would require
evaluating A or B more than once.  So a more suitable expression might
be

int X1, Y1;
if ((X1 = A) && (!(X2 = B))) || (X2 && !X1) {...}

Or you could just use goto's -- I think they would be justified here if
you explain what they do. 

-- 
Ken Johnson, AI Applications Institute, 80 South Bridge, Edinburgh EH1 1HN
E-mail ken at aiai.ed.ac.uk, phone 031-225 4464 extension 212
`I have read your article, Mr.  Johnson, and I am no wiser than when I
started.' -- `Possibly not, sir, but far better informed.'



More information about the Comp.lang.c mailing list