Why isn't there a logical XOR operator?

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

Of course one can get the effect with appropriate use of existing
operators, but a ^^ operator would make for nice symmetry (as well as
useful to me in something I'm working on). Am I the only one who
would find it useful?
 
J

Joona I Palaste

Christopher Benson-Manica said:
Of course one can get the effect with appropriate use of existing
operators, but a ^^ operator would make for nice symmetry (as well as
useful to me in something I'm working on). Am I the only one who
would find it useful?

You mean an operator that returns 1 if exactly one of its operands is
nonzero, and 0 otherwise? Yes, that would be useful. But forget about
the short-circuit semantics. Think about it and you'll see why.
 
J

Joona I Palaste

You mean an operator that returns 1 if exactly one of its operands is
nonzero, and 0 otherwise? Yes, that would be useful. But forget about
the short-circuit semantics. Think about it and you'll see why.

Also if there were an unary ^ operator, we could write Japanese smileys
into our code. For example:
sashimi ^=^ kawaii;
 
C

Christopher Benson-Manica

Joona I Palaste said:
You mean an operator that returns 1 if exactly one of its operands is
nonzero, and 0 otherwise? Yes, that would be useful. But forget about
the short-circuit semantics. Think about it and you'll see why.

Hm, that's true. A ^^ B would have to evaluate A and B... I hadn't
thought of that, and I suppose that was a greater good than &/&&
and |/|| symmetry. Interesting.
 
N

nrk

Christopher said:
Of course one can get the effect with appropriate use of existing
operators, but a ^^ operator would make for nice symmetry (as well as
useful to me in something I'm working on). Am I the only one who
would find it useful?
If you write your conditions to be all logical (that is avoid the if ( x )
style idioms), then using the existing xor operator would achieve exactly
the same thing as your proposed ^^, wouldn't it? I mean, there can be no
short-circuit evaluation of an xor operator.

-nrk.
 
C

Christopher Benson-Manica

Joona I Palaste said:
Also if there were an unary ^ operator, we could write Japanese smileys
into our code. For example:
sashimi ^=^ kawaii;

This assumes, of course, that C authors are familiar with the Japanese
emoticon paradigm ^_~
 
X

xarax

nrk said:
If you write your conditions to be all logical (that is avoid the if ( x )
style idioms), then using the existing xor operator would achieve exactly
the same thing as your proposed ^^, wouldn't it? I mean, there can be no
short-circuit evaluation of an xor operator.

(a ^^ b) would be something like ((!!a) != (!!b)). Could
be macrotized rather easily? Could the double bang be
reduced to a single bang on each side?
 
J

Joona I Palaste

(a ^^ b) would be something like ((!!a) != (!!b)). Could
be macrotized rather easily? Could the double bang be
reduced to a single bang on each side?

Yes, ((!!a) != (!!b)) is equivalent to ((!a) != (!b)).
 
C

CBFalconer

Christopher said:
Of course one can get the effect with appropriate use of existing
operators, but a ^^ operator would make for nice symmetry (as well as
useful to me in something I'm working on). Am I the only one who
would find it useful?

if (!!a ^ !!b) ....

c = !!a ^ !!b;
 
D

Dan Pop

In said:
Of course one can get the effect with appropriate use of existing
operators, but a ^^ operator would make for nice symmetry (as well as
useful to me in something I'm working on).

Because ^ is good enough, given the fact that a logical XOR operator
couldn't have any shortcircuiting semantics. !a ^ !b yields exactly
the same result as the hypothetical ^^ operator. If a and b are already
logical expressions (i.e. expressions that evaluate to 0 or 1), a ^ b is
enough.
Am I the only one who would find it useful?

Are you still convinced that it would be useful?

Dan
 
N

nrk

xarax said:
(a ^^ b) would be something like ((!!a) != (!!b)). Could
be macrotized rather easily? Could the double bang be
reduced to a single bang on each side?

Yes, as x xor y is the same as x' xor y'. However, while this is pretty
neat, I think it makes the code harder to comprehend than using a straight
xor operation on the logical conditions (maybe its me that gets confused
when seeing negation all over the place).

And for those who believe that the xor shouldn't be used in combining
logical conditions, there is one situation where I have found this to be
eminently useful: when processing command line arguments where two
different options are mutually incompatible, but atleast one of them must
have been specified.

-nrk.
 
L

Leor Zolman

Yes, ((!!a) != (!!b)) is equivalent to ((!a) != (!b)).

Huh? That's saying !!a is equivalent to !a. I think not.
Applying de Morgan's Theorem,
((!!a) != (!!b))
can be rewritten as:
! (!a == !b)
though.
-leor


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
L

Leor Zolman

Huh? That's saying !!a is equivalent to !a. I think not.

Oops, pounced too quick, sorry. Actually they're _all_ equivalent ;-)
-leor
Applying de Morgan's Theorem,
((!!a) != (!!b))
can be rewritten as:
! (!a == !b)
though.
-leor


Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html

Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
D

Dan Pop

In said:
Huh? That's saying !!a is equivalent to !a. I think not.
Applying de Morgan's Theorem,
((!!a) != (!!b))
can be rewritten as:
! (!a == !b)
though.

And, guess what? ! (!a == !b) can be rewritten as !a != !b. However,
given that the OP wanted a logical XOR operator, !a ^ !b reflects his
intention better.

Dan
 
C

CBFalconer

Leor said:
.... snip ...

Huh? That's saying !!a is equivalent to !a. I think not.
Applying de Morgan's Theorem,
((!!a) != (!!b))
can be rewritten as:
! (!a == !b)
though.

Huh? Look again. Write out a truth table or Karnaugh map.
 
X

xarax

nrk said:
Yes, as x xor y is the same as x' xor y'. However, while this is pretty
neat, I think it makes the code harder to comprehend than using a straight
xor operation on the logical conditions (maybe its me that gets confused
when seeing negation all over the place).

And for those who believe that the xor shouldn't be used in combining
logical conditions, there is one situation where I have found this to be
eminently useful: when processing command line arguments where two
different options are mutually incompatible, but atleast one of them must
have been specified.

If you think that's bad, try the following with
A,B:{0,1}. First column is the intended operation.
Middle column is the arithmetic equivalent. Last
column is an alternative logical expression. The
variables A,B have values from the set {0,1}.


NOT(A): 1-A
A AND B: A*B : NOT(NOT(A) OR NOT(B))
A NAND B: 1-A*B : NOT(A) OR NOT(B)
A OR B: A+B-A*B : NOT(NOT(A) AND NOT(B))
A NOR B: 1-A-B+A*B : NOT(A) AND NOT(B)
A == B: 1+2*A*B-A-B : A XNOR B
A <> B: A+B-2*A*B : A XOR B
A < B: B-B*A : NOT(A) AND B
A > B: A-A*B : A AND NOT(B)
A <= B: 1-A+A*B : NOT(A) OR B
A >= B: 1-B+A*B : A OR NOT(B)
 
K

Keith Thompson

nrk said:
If you write your conditions to be all logical (that is avoid the if ( x )
style idioms), then using the existing xor operator would achieve exactly
the same thing as your proposed ^^, wouldn't it? I mean, there can be no
short-circuit evaluation of an xor operator.

The result will be an expression that breaks the first time you feed
it an operand that doesn't have the value 0 or 1. This will happen at
the most inconvenient possible time.
 
N

nrk

Keith said:
The result will be an expression that breaks the first time you feed
it an operand that doesn't have the value 0 or 1. This will happen at
the most inconvenient possible time.

Read carefully. What I meant was that, you couldn't write:
if ( x ^ y ),
but you could write:
if ( (x != 0) ^ (y != 0) )

-nrk.
 
P

pete

Dan said:
And, guess what? ! (!a == !b) can be rewritten as !a != !b. However,
given that the OP wanted a logical XOR operator, !a ^ !b reflects his
intention better.

However, (!a != !b) reflects the intent of the code,
which is the logical relationship between a and b, better.

In any situation where I could write
if (!a ^ !b) {
I think I would rather write
if (!a != !b) {

Would you ever even write
if (!a ^ !b) {
?

That's the sort of thing that you would rather express as
if ((!a ^ !b) != 0) {
isn't it ?
 
K

Kevin D. Quitt

I officially nominate this question as a FAQ, and request that the first
update to the FAQ since February 7, 1999 be made.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top