Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!illusion!marcus From: marcus@illusion.UUCP (Marcus Hall) Newsgroups: comp.lang.c Subject: Re: "logical" xor Message-ID: <170@illusion.UUCP> Date: 10 Jan 88 20:26:28 GMT References: <2946@zeus.TEK.COM> Reply-To: marcus@illusion.UUCP (Marcus Hall) Organization: Magic Numbers Software, Bloomingdale, IL Lines: 39 In article <2946@zeus.TEK.COM> dant@tekla.UUCP (Dan Tilque) writes: >>...I've often >>needed and have had to kludge ((a != 0) ^ (b != 0)). >It's not really a kludge so you can stop thinking of as such. It is a bit >verbose but you can just use (a ^ b) and it will do the same thing (*). >... The "bitwise" xor does exactly what you would want >a "logical" xor to do. >(*) Possibly it won't work where a and b are floats on a machine where >(float) 0 does not have an all null bit pattern. But in that case, neither >will (a ^^ b). Sorry, there is a difference. Suppose that a==2 and b==3? a ^ b == 1, which is considered true. ((a != 0) ^ (b != 0)) gives (1) ^ (1) == 0, which is false. > And there's no way to optimize an exclusive-or operation. True, which is why there wasn't a "short circuiting" operator for exclusive or. The compiler could perhaps generate better code if a or b were simple values already in registers in a limited number of cases, but a good optimizer could probably spot these cases and turn out the same code anyway. (If the time spent looking for this optimization was considered worth the small gain made by the optimization.) It ends up being just a convenience for the programmer, but since this construct isn't used nearly as often as && and || is, that isn't too bad. >The only reason to add ^^ is esthetic. That is, we have || and && as well >as | and & so let's have ^^ along with ^. However, that's a good reason >not to use ^^ as exponentiation: people will get it mixed up with xor too >easily. (Of course, that didn't stop them from the = and == syntax :-) Here I tend to agree with you, but there is a slight gain in not having to optimize as much for the rare use of the ^^ operator. >Dan Tilque Marcus Hall ..!{ihnp4,mcdchg}!illusion!marcus