C history question

John R. Levine johnl at esegue.segue.boston.ma.us
Tue Sep 12 00:42:27 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?

Because it's nowhere near as useful.  The && and || operators are useful
because they guarantee left to right short circuit evaluation.  You can't
find the value of an XOR without evaluating both operands, so short circuit
evaluation is meaningless.

>[Why have to write] (a || b) && (!(a && b)) when a ^^ b is so much "cleaner"?

If you know that a and b actually have the values 0 or 1, typically because
they are comparison expressions, you can write either of:

	a ^ b
	a != b

and get exactly what you want.  If they're not strictly 1 or 0, these will do:

	!a ^ !b
	!a != !b

If this is too ugly, you can always write an XOR(a,b) macro.
-- 
John R. Levine, Segue Software, POB 349, Cambridge MA 02238, +1 617 492 3869
johnl at esegue.segue.boston.ma.us, {ima|lotus}!esegue!johnl, Levine at YALE.edu
Massachusetts has 64 licensed drivers who are over 100 years old.  -The Globe



More information about the Comp.lang.c mailing list