By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,431 Members | 1,811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,431 IT Pros & Developers. It's quick & easy.

operator^^()

P: n/a
Why there is no logical xor operator?
I often miss it in mutually-exclusive if-statements.

--
Vladimir

Nov 22 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
* Vladimir:

Why there is no logical xor operator?
That's better asked in [comp.std.c++].

I often miss it in mutually-exclusive if-statements.


You can use '!='. When the argumens are guaranteed boolean you can also
use '^'. And you can use functional notation,

inline bool xor( bool a, bool b ) { return a ^ b; }

...
if( xor( 5, 0 ) ) ...
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #2

P: n/a
Alf P. Steinbach wrote:
* Vladimir:
Why there is no logical xor operator?

That's better asked in [comp.std.c++].
I often miss it in mutually-exclusive if-statements.

You can use '!='. When the argumens are guaranteed boolean you can also
use '^'. And you can use functional notation,

inline bool xor( bool a, bool b ) { return a ^ b; }

...
if( xor( 5, 0 ) ) ...


Cannot do that. "xor" is a keyword, which cannot be used as a function
name (2.11/2).

V
Nov 22 '05 #3

P: n/a
Vladimir wrote:
Why there is no logical xor operator?
To compute the result of XOR, you have to evaluate both operands.

For evaluating both operands with a sequence point in between, there
already exists the comma operator.

The virtue of && and || is that they perform short-circuited
left-to-right evaluation. They are not just logical functions, but
evaluation-controlling operators.
I often miss it in mutually-exclusive if-statements.


XOR is not as useful as you might think in testing for mutually
exclusive alternatives.

The result of a binary XOR, if true, tells you that exactly one of the
two constituents is true. But a nested XOR among three or more
constituents does not compute that: it computes that an odd number of
the terms is true!

If you have N logical expressions, where N > 2, and you want to test
that exactly one of them is true, you need a specially-designed,
variable-argument XOR.

But note that the binary && and || operators do combine in a useful
way. If you had an && that could take three or more operands, it would
be equivalent to a nesting of multiple binary && operators.

So in other words:

A && B && C && D

does the intuitive thing. It keeps evaluating the terms left to right
until it finds one that yields true.

What would it mean to write:

A ^^ B ^^ C ^^ D

It would have to evaluate all of the terms: no short circuiting. And
the return value would be true if any odd number of these terms yielded
true: if exactly one term is true, or if any three are true.

Nov 22 '05 #4

P: n/a
* Victor Bazarov:
* Alf P. Steinbach:

inline bool xor( bool a, bool b ) { return a ^ b; }


Cannot do that. "xor" is a keyword, which cannot be used as a function
name (2.11/2).


Thanks. What was I thinking of? Well, truth be told, I've never used
the alternative tokens, and wouldn't know what e.g. '<:' is if it was
thrown in my face repeatedly with threats of CIA torture and worse. ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #5

P: n/a
Kaz Kylheku wrote:

XOR is not as useful as you might think in testing for mutually
exclusive alternatives.

The result of a binary XOR, if true, tells you that exactly one of the
two constituents is true. But a nested XOR among three or more
constituents does not compute that: it computes that an odd number of
the terms is true!


The two are perfectly consistent. Binary XOR tells you if an odd number
of the terms is true. <g>

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Nov 22 '05 #6

P: n/a

Alf P. Steinbach wrote:
Why there is no logical xor operator?
I often miss it in mutually-exclusive if-statements.


You can use '!='.


unfortunately I can't:

2 != 3 is 1
but
2 ^^ 3 would be 0

--
Vladimir

Nov 22 '05 #7

P: n/a
Vladimir wrote:
Alf P. Steinbach wrote:
Why there is no logical xor operator?
I often miss it in mutually-exclusive if-statements.


You can use '!='.


unfortunately I can't:

2 != 3 is 1
but
2 ^^ 3 would be 0


But neither 2 nor 3 are logical expressions. What you need is
to use != but make sure that on both sides you have values of
the type 'bool'. You can achieve that by using ! :

!a != !b

It produces exactly the same effect as your imaginary ^^.

V
Nov 22 '05 #8

P: n/a
* Vladimir:

Alf P. Steinbach wrote:
Why there is no logical xor operator?
I often miss it in mutually-exclusive if-statements.


You can use '!='.


unfortunately I can't:

2 != 3 is 1
but
2 ^^ 3 would be 0


I see that Victor has already answered, but just for the record: using
only '!=' you can write (2 != 0) != (3 != 0).
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 22 '05 #9

P: n/a

Victor Bazarov wrote:
But neither 2 nor 3 are logical expressions.


Well, logical operators implicitly convert their arguments to type bool
which allows any integer to act as a logical expression for them.

--
Vladimir

Nov 22 '05 #10

P: n/a

"Vladimir" <vl*********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

Victor Bazarov wrote:
But neither 2 nor 3 are logical expressions.


Well, logical operators implicitly convert their arguments to type bool
which allows any integer to act as a logical expression for them.


I believe that is exactly Victor's point. What is yours?

-Mike
Nov 22 '05 #11

P: n/a

Kaz Kylheku wrote:
So in other words:

A && B && C && D

does the intuitive thing. It keeps evaluating the terms left to right
until it finds one that yields true.


..... yields false (or returns true if ALL of them are true upon
evaluation).

Nov 22 '05 #12

P: n/a
Mike Wahler wrote:
"Vladimir" <vl*********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

Victor Bazarov wrote:
But neither 2 nor 3 are logical expressions.


Well, logical operators implicitly convert their arguments to type
bool which allows any integer to act as a logical expression for
them.


I believe that is exactly Victor's point. What is yours?


I think Vladimir's point is that if there _were_ the ^^ operator, it
would convert both sides to type bool before applying what essentially
is the '!='. Hence the 'not equal' operator is not a good replacement
to the logical XOR.

V
Nov 22 '05 #13

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:2e******************************@comcast.com. ..
Mike Wahler wrote:
"Vladimir" <vl*********@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

Victor Bazarov wrote:

But neither 2 nor 3 are logical expressions.

Well, logical operators implicitly convert their arguments to type
bool which allows any integer to act as a logical expression for
them.


I believe that is exactly Victor's point. What is yours?


I think Vladimir's point is that if there _were_ the ^^ operator, it
would convert both sides to type bool before applying what essentially
is the '!='. Hence the 'not equal' operator is not a good replacement
to the logical XOR.


OK, that make sense. Thanks.

Vladimir: sorry, I think I misunderstood you.

-Mike
Nov 22 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.