[comp.lang.c] XOR operator

ken@aiai.ed.ac.uk (Ken Johnson) (09/11/89)

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
>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@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.'

bazza@hppad.HP.COM (Carlos Bazzarella) (09/13/89)

/ hppad:comp.lang.c / wlp@calmasd.Prime.COM (Walter Peterson) /  5:52 pm  Sep 10, 1989 /

Perhaps someone can answer a question that has bugged me since I first
learned C almost 10 years ago.

C has bitwise operators for AND (&), OR (|) and XOR (^) and boolean 
operator for AND (&&) and OR (||), but not for XOR (^^). Why?
What happened to the boolean XOR operator ?  If && makes sense for the
boolean AND and || makes sense for the boolean OR, why doesn't ^^ make
sense for the boolean XOR ?

Most assemblers that I know have XOR as a single instruction so why
make people go to the trouble of writing something like
(a || b) && (!(a && b)) when a ^^ b is so much "cleaner".

Can anyone tell me why this was left out of the language ?  
Is there any chance that some future version of ANSI-C will have it ?

-- 
Walt Peterson.  Prime - San Diego R&D (Object and Data Management Group)
"The opinions expressed here are my own."
----------

bazza@hppad.HP.COM (Carlos Bazzarella) (09/13/89)

Ops!! response 3 was caused by accident
when I was reading the note. I didn't
know how to recover from the mistake.
I am sorry for the inconvinience.

                Carlos Bazzarella.