Path: utzoo!utgpu!water!watmath!clyde!ima!haddock!karl From: karl@haddock.ISC.COM (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: "logical" xor Message-ID: <2237@haddock.ISC.COM> Date: 13 Jan 88 00:46:53 GMT References: <2946@zeus.TEK.COM> <195@fxgrp.UUCP> <2800@killer.UUCP> Reply-To: karl@haddock.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Boston Lines: 23 Fact 1: "^" is a bitwise XOR operator. When restricted to normalized boolean operands, it is equivalent to logical XOR. Fact 2: "!=" compares two integers. When restricted to normalized boolean operands, it is equivalent to logical XOR. Fact 3: "^^" is a hypothetical logical XOR operator. It would return 0 if its operands are both zero or both nonzero, and 1 otherwise. Unlike the "&&" and "||" operators, there is no short-circuit possible; both operands are evaluated. Conclusion 1: In most applications (and in all applications written by those of us who distinguish between booleans and integers), "^^" is unnecessary; one or both of the alternatives will suffice. Conclusion 2: If the compiler knows that both operands are normalized booleans, then "x^y" and "x!=y" can generate the same code. Depending on the hardware, the best code might be the machine's equivalent of "x^y", "x!=y", or "x ? !y : y". Would any compiler-writers out there care to comment on whether they've implemented this optimization? Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint