Thread View: comp.std.c++
6 messages
6 total messages
Started by Robert Risack
Wed, 16 Aug 1995 13:28
Why no logical XOR operator?
Author: Robert Risack
Date: Wed, 16 Aug 1995 13:28
Date: Wed, 16 Aug 1995 13:28
18 lines
1185 bytes
1185 bytes
Hello. I'm wondering why there isn't a logical XOR operator (eg. ^^) though there is a bitwise one (ie. ^). The other bitwise operators ( & | and ~ ) have their logical aquivalent ( && || and ! ). And the XOR is IMHO not that uncommon. Bye... --- | '. .. | Robert Risack | Jeder ist Auslaender - fast , s| __/ . <___> __/ o . | _ | ueberall. Everybody is a `--' /.. / / . / / | PObEPT P|/|3AK | foreigner - almost everywhere. Robert Risack - Gebbertstr. 12 - 91054 Erlangen - FR Germany --- [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Why no logical XOR operator?
Author: Shankar Unni
Date: Mon, 21 Aug 1995 21:18
Date: Mon, 21 Aug 1995 21:18
18 lines
1309 bytes
1309 bytes
Robert Risack (rtrisack@faui01.informatik.uni-erlangen.de) wrote:
> I'm wondering why there isn't a logical XOR operator (eg. ^^)
> though there is a bitwise one (ie. ^).
Oh, please!
We've been through exactly this specific battle about once every semester
(hint) on comp.{lang,std}.c ... Anyone care to summarize the arguments?
--
Shankar Unni E-Mail: shankar@sgi.com
Silicon Graphics Inc. Phone: +1-415-390-2072
URL: http://reality.sgi.com/employees/shankar
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Why no logical XOR operator
Author: Warner Losh
Date: Mon, 21 Aug 1995 23:54
Date: Mon, 21 Aug 1995 23:54
39 lines
1723 bytes
1723 bytes
Distribution: : I'm wondering why there isn't a logical XOR operator (eg. ^^) : though there is a bitwise one (ie. ^). The '!=' operator is logically the same as XOR. It produces the same truth table for bool values as you'd expect an xor operator to: a != b a/b true false true false true false true false However, the precidence of this operator is not what you'd want it to be to really use XOR. Eg if (a != b || c !=d) parses "correctly" but if (a != b != c != d) doesn't. This is easy to fix with parens: if ((a != b) != (c != d)). However, since ^ has a lower precedence than !=, you could write something like if (a != b ^ c != d) and have no need for parens. There can be no short circuit done for this operator, since you can't know the result of the operator until you've evaluated both sides. Maybe that is why it isn't in the language. I was once told that C had both | and || because one was optimized and the other wasn't. Since it is impossible to optimize xor, there is no ^^ operator. I'm not sure how true this actually is. Warner --- [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Why no logical XOR operator?
Author: Tim Hollebeek
Date: Tue, 22 Aug 1995 02:22
Date: Tue, 22 Aug 1995 02:22
34 lines
1885 bytes
1885 bytes
In article <40sp1t$lbc@cd4680fs.rrze.uni-erlangen.de>,
Robert Risack <rtrisack@faui01.informatik.uni-erlangen.de> wrote:
>Hello.
>
>I'm wondering why there isn't a logical XOR operator (eg. ^^)
>though there is a bitwise one (ie. ^).
>
>The other bitwise operators ( & | and ~ ) have their logical
>aquivalent ( && || and ! ). And the XOR is IMHO not that uncommon.
There is a crucial difference; the evaluation of && and || can be
shortcut, while no such trick works for (the hypothetical) ^^, making
them quite different beasts.
&& and || are _not_ simply the boolean equivalents of & and |. For
example, the following common code:
if (x && x->foo) { ... }
is perfectly safe, while the bitwise version:
if (!!x & !! x->foo) { ... }
generates a null-pointer reference if x is zero.
--
Tim Hollebeek |"What is love? 'tis not hereafter; Present mirth hath
PChem Grad Student |present laughter; What's to come is still unsure: In
Princeton University |delay there lies no plenty; Then come kiss me, sweet and
---------------------|twenty, Youth's a stuff will not endure." -Twelfth Night
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Why no logical XOR operator?
Author: Doug Moore
Date: Tue, 22 Aug 1995 14:54
Date: Tue, 22 Aug 1995 14:54
29 lines
1914 bytes
1914 bytes
Shankar Unni (shankar@engr.sgi.com) wrote:
: Robert Risack (rtrisack@faui01.informatik.uni-erlangen.de) wrote:
: > I'm wondering why there isn't a logical XOR operator (eg. ^^)
: > though there is a bitwise one (ie. ^).
: Oh, please!
: We've been through exactly this specific battle about once every semester
: (hint) on comp.{lang,std}.c ... Anyone care to summarize the arguments?
While I agree that it is a common and basically pointless question, I
can't believe that a message which is nothing but a flame made it past
moderation.
[ Point well taken. My mistake. -sdc ]
The answer is that there is no such thing as a short-circuiting
exclusive-or. With && and ||, the second argument may not be
evaluated. With the proposed ^^, both arguments would have to be
evaluated. There is already a perfectly good logical exlusive-or
operator for bools: operator !=. If you want the logical exclusive-or
of non-booleans (a dubious proposition IMHO), you have to go for !a !=
!b. Such a strange computation deserves slightly strange notation.
dougm@rice.edu (Doug Moore)
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Why no logical XOR operator?
Author: Robert Newshutz
Date: Tue, 22 Aug 1995 14:54
Date: Tue, 22 Aug 1995 14:54
28 lines
1877 bytes
1877 bytes
In article <40sp1t$lbc@cd4680fs.rrze.uni-erlangen.de>, rtrisack@faui01.informatik.uni-erlangen.de (Robert Risack) writes: |> Hello. |> |> I'm wondering why there isn't a logical XOR operator (eg. ^^) |> though there is a bitwise one (ie. ^). |> |> The other bitwise operators ( & | and ~ ) have their logical |> aquivalent ( && || and ! ). And the XOR is IMHO not that uncommon. |> |> Bye... |> --- |> | '. .. | Robert Risack | Jeder ist Auslaender - fast |> , s| __/ . <___> __/ o . | _ | ueberall. Everybody is a |> `--' /.. / / . / / | PObEPT P|/|3AK | foreigner - almost everywhere. |> Robert Risack - Gebbertstr. 12 - 91054 Erlangen - FR Germany |> --- |> [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. |> Contact address: std-c++-request@ncar.ucar.edu. The moderation policy |> is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ] Use != to get a logical xor function. As long as you are working with true boolean (0 and 1) this works. Rob Newshutz (newshutz@vnet.ibm.com) [ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu. Contact address: std-c++-request@ncar.ucar.edu. The moderation policy is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads