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

Why isn't there a logical XOR operator?

P: n/a
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?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
Share this Question
Share on Google+
80 Replies


P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> scribbled the following:
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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
Nov 14 '05 #2

P: n/a
Joona I Palaste <pa*****@cc.helsinki.fi> scribbled the following:
Christopher Benson-Manica <at***@nospam.cyberspace.org> scribbled the following:
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.


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

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"War! Huh! Good God, y'all! What is it good for? We asked Mayor Quimby."
- Kent Brockman
Nov 14 '05 #3

P: n/a
nrk
Christopher Benson-Manica wrote:
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.
--
Remove devnull for email
Nov 14 '05 #4

P: n/a
Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #5

P: n/a
Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:
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 ^_~

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #6

P: n/a
"nrk" <ra*********@devnull.verizon.net> wrote in message
news:ma*************@nwrddc01.gnilink.net...
Christopher Benson-Manica wrote:
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.


(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?

Nov 14 '05 #7

P: n/a
xarax <xa***@email.com> scribbled the following:
"nrk" <ra*********@devnull.verizon.net> wrote in message
news:ma*************@nwrddc01.gnilink.net...
Christopher Benson-Manica wrote:
> 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.

(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)).

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"It was, er, quite bookish."
- Horace Boothroyd
Nov 14 '05 #8

P: n/a
Christopher Benson-Manica wrote:

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;

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #9

P: n/a
nrk
xarax wrote:
"nrk" <ra*********@devnull.verizon.net> wrote in message
news:ma*************@nwrddc01.gnilink.net...
Christopher Benson-Manica wrote:
> 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.


(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.

--
Remove devnull for email
Nov 14 '05 #10

P: n/a
In <bv**********@chessie.cirr.com> Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11

P: n/a
On 3 Feb 2004 16:04:18 GMT, Joona I Palaste <pa*****@cc.helsinki.fi>
wrote:
xarax <xa***@email.com> scribbled the following:
"nrk" <ra*********@devnull.verizon.net> wrote in message
news:ma*************@nwrddc01.gnilink.net...
Christopher Benson-Manica wrote:
> 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.

(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)).


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
le**@bdsoft.com
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
Nov 14 '05 #12

P: n/a

Yes, ((!!a) != (!!b)) is equivalent to ((!a) != (!b)).
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
le**@bdsoft.com
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
le**@bdsoft.com
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
Nov 14 '05 #13

P: n/a
Leor Zolman wrote:
Joona I Palaste <pa*****@cc.helsinki.fi> wrote:

.... snip ...

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.


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

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #14

P: n/a
In <i8********************************@4ax.com> Leor Zolman <le**@bdsoft.com> writes:
On 3 Feb 2004 16:04:18 GMT, Joona I Palaste <pa*****@cc.helsinki.fi>
wrote:
xarax <xa***@email.com> scribbled the following:
"nrk" <ra*********@devnull.verizon.net> wrote in message
news:ma*************@nwrddc01.gnilink.net...
Christopher Benson-Manica wrote:
> 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.

(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)).


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #15

P: n/a
"nrk" <ra*********@devnull.verizon.net> wrote in message
news:vJ***************@nwrddc02.gnilink.net...
xarax wrote:
"nrk" <ra*********@devnull.verizon.net> wrote in message
news:ma*************@nwrddc01.gnilink.net...
Christopher Benson-Manica wrote:

> 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.


(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.


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)

Nov 14 '05 #16

P: n/a
nrk <ra*********@devnull.verizon.net> writes:
Christopher Benson-Manica wrote:
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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #17

P: n/a
nrk
Keith Thompson wrote:
nrk <ra*********@devnull.verizon.net> writes:
Christopher Benson-Manica wrote:
> 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.


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.
--
Remove devnull for email
Nov 14 '05 #18

P: n/a
Dan Pop wrote:

In <i8********************************@4ax.com> Leor Zolman <le**@bdsoft.com> writes:
On 3 Feb 2004 16:04:18 GMT, Joona I Palaste <pa*****@cc.helsinki.fi>
wrote:
xarax <xa***@email.com> scribbled the following:
"nrk" <ra*********@devnull.verizon.net> wrote in message
news:ma*************@nwrddc01.gnilink.net...
> Christopher Benson-Manica wrote:
> > 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.

(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)).


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.


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 ?

--
pete
Nov 14 '05 #19

P: n/a
I officially nominate this question as a FAQ, and request that the first
update to the FAQ since February 7, 1999 be made.
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 14 '05 #20

P: n/a
On 3 Feb 2004 16:09:18 GMT, Da*****@cern.ch (Dan Pop) wrote:
Are you still convinced that it would be useful?


Yes, for the same reason as || and &&, albeit there's no shortcutting. It
makes it *obvious* that a purely boolean expression is involved. !a ^ !b
isn't obviously purely boolean; the central operator is not boolean.
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 14 '05 #21

P: n/a
Em Tue, 03 Feb 2004 17:05:14 +0000, CBFalconer escreveu:
Christopher Benson-Manica wrote: <snip>
if (!!a ^ !!b) ....

c = !!a ^ !!b;


Why !!a ? Doesn't this have the same effect of a? Like
if (a ^ b) ....
c = a ^ b;

Could you enlighten us?

--
Quidquid latine dictum sit altum viditur

Nov 14 '05 #22

P: n/a

On Tue, 3 Feb 2004, Dan Pop wrote:

Christopher Benson-Manica writes: [re: introducing a logical XOR operator]
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.


Possibly dumb, and possibly OT, question: What if 'a' and/or 'b' are
doubles with the IEEE value NaN, that screws up comparisons? Does
the !a^!b <==> a^^b equivalence still hold? (Or do NaN semantics already
make nonsense of certain operations?)
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?


Well, a logical xor operator wouldn't add anything to the capabilities
of the language, but then neither does having a special operator for
subtraction. I have, on occasion, run across situations where an XOR
operator would have been useful. Christopher mentioned command-line
argument parsing, such as:

[...]
switch (argv[i][j]) {
case 'K': KNRStyle = 1; break;
case 'A': AnsiStyle = 1; break;
}
[...]
if (KNRStyle ^^ AnsiStyle)
process(infp, outfp);
else
do_error("You must specify exactly one of -K, -A!\n");

Here the ^^ operator would capture the spirit of the condition better
than the != alternative, as well as being slightly more robust. The
suggested alternative,

if (!KNRStyle != !AnsiStyle)

is IMO so obfuscated as to be actually worse than simply enumerating
the cases:

if (KNRStyle && !AnsiStyle || AnsiStyle && !KNRStyle)
I also submit for consideration, as I've done before, the usefulness
of logical operators when dealing with pointers:

struct btnode {
int value;
struct btnode *left;
struct btnode *right;
};

int bt_IsUselessBranch(struct btnode *p)
{
return (p->value == 0) && (p->left ^^ p->right);
}

versus

return (p->value == 0) && (!(p->left) != !(p->right));

Of course, both cases are pretty ugly, here. But I haven't yet
come up with a good example. ;-)
And while I think it would have been cool if C had been designed
with a ^^ operator from the start, IMHO it's much too late now to
try adding it to the standard language. C doesn't need any more
syntax, and even if it did, I don't think the new syntax would find
many willing users. A new version of C would IMHO suffer the same
problems as C99 still suffers w.r.t. market share, only worse.
Now, a *new* language... :-)

-Arthur
Nov 14 '05 #23

P: n/a
Josť de Paula <jo***********@ig.com.br> scribbled the following:
Em Tue, 03 Feb 2004 17:05:14 +0000, CBFalconer escreveu:
Christopher Benson-Manica wrote: <snip>
if (!!a ^ !!b) ....

c = !!a ^ !!b;

Why !!a ? Doesn't this have the same effect of a? Like
if (a ^ b) ....
c = a ^ b; Could you enlighten us?


No, it's not the same effect. The outcome is mostly the same, but
there are differences.
Suppose a is 1 and b is 2. a ^ b will now be 3, which is different
from 0, and thus C considers it true.
However !!a will be 1, and !!b will also be 1, so !!a ^ !!b will be
0, and thus C considers it false.
The ! operator is defined as returning 1 if its operand is 0, or
returning 0 if its operand is anything other. !! is two ! operators
together, they return 0 if their operand is 0, or 1 if their operand
is anything other.
This is often used as a neat trick to "force" non-zero values to 1,
making sure bitwise operators work right in place of logical operators.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Make money fast! Don't feed it!"
- Anon
Nov 14 '05 #24

P: n/a

On Tue, 3 Feb 2004, [iso-8859-1] Josť de Paula wrote:

Em Tue, 03 Feb 2004 17:05:14 +0000, CBFalconer escreveu:
Christopher Benson-Manica wrote:

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

c = !!a ^ !!b;


Why !!a ? Doesn't this have the same effect of a? Like
if (a ^ b) ....
c = a ^ b;

Could you enlighten us?


What is the value of 42?
What is the value of !!42?
What is the value of 42 ^ 6?
What is the value of !!42 ^ !!6?

-Arthur,
rhetorically
Nov 14 '05 #25

P: n/a
Josť de Paula wrote:
Em Tue, 03 Feb 2004 17:05:14 +0000, CBFalconer escreveu:

Christopher Benson-Manica wrote:


<snip>
if (!!a ^ !!b) ....

c = !!a ^ !!b;

Why !!a ? Doesn't this have the same effect of a? Like
if (a ^ b) ....
c = a ^ b;

Could you enlighten us?

Assume
a = 2;
b = 7;

Then
a ^ b == 2 ^ 7
== 5

which evaluates false in an if() statement

However
!a == 0

!!a == ! (!a)
== 1

!b == 0

!!b == ! (!b)
== 1

!!a ^ !!b == 1 ^ 1
== 0

which evaluates true in an if() statement
--
Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Nov 14 '05 #26

P: n/a
Gak!! Another fingercheck/brainfart

Lew Pitcher wrote:
Josť de Paula wrote:
Em Tue, 03 Feb 2004 17:05:14 +0000, CBFalconer escreveu:

Christopher Benson-Manica wrote:

<snip>
if (!!a ^ !!b) ....

c = !!a ^ !!b;


Why !!a ? Doesn't this have the same effect of a? Like
if (a ^ b) ....
c = a ^ b;

Could you enlighten us?


Assume
a = 2;
b = 7;

Then
a ^ b == 2 ^ 7
== 5

which evaluates false in an if() statement

which evaluates /true/ in an if() statement

However
!a == 0

!!a == ! (!a)
== 1

!b == 0

!!b == ! (!b)
== 1

!!a ^ !!b == 1 ^ 1
== 0

which evaluates true in an if() statement

which evaluates /false/ in an if() statement

--
Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Nov 14 '05 #27

P: n/a
Arthur J. O'Dwyer wrote:

On Tue, 3 Feb 2004, Dan Pop wrote:

Christopher Benson-Manica writes:

[re: introducing a logical XOR operator]
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.


Possibly dumb, and possibly OT, question: What if 'a' and/or 'b' are
doubles with the IEEE value NaN, that screws up comparisons? Does
the !a^!b <==> a^^b equivalence still hold? (Or do NaN semantics already
make nonsense of certain operations?)
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?


Well, a logical xor operator wouldn't add anything to the capabilities
of the language, but then neither does having a special operator for
subtraction. I have, on occasion, run across situations where an XOR
operator would have been useful. Christopher mentioned command-line
argument parsing, such as:

[...]
switch (argv[i][j]) {
case 'K': KNRStyle = 1; break;
case 'A': AnsiStyle = 1; break;
}
[...]
if (KNRStyle ^^ AnsiStyle)
process(infp, outfp);
else
do_error("You must specify exactly one of -K, -A!\n");

Here the ^^ operator would capture the spirit of the condition better
than the != alternative, as well as being slightly more robust. The
suggested alternative,

if (!KNRStyle != !AnsiStyle)

is IMO so obfuscated as to be actually worse than simply enumerating
the cases:

if (KNRStyle && !AnsiStyle || AnsiStyle && !KNRStyle)

I also submit for consideration, as I've done before, the usefulness
of logical operators when dealing with pointers:

struct btnode {
int value;
struct btnode *left;
struct btnode *right;
};

int bt_IsUselessBranch(struct btnode *p)
{
return (p->value == 0) && (p->left ^^ p->right);
}

versus

return (p->value == 0) && (!(p->left) != !(p->right));

Of course, both cases are pretty ugly, here.


.... without the extra parentheses:
return (p->value == 0) && (!p->left != !p->right);

explicit comparisons ...
((p->left == NULL) != (p->right == NULL));

With explicit comparisons against NULL,
as long as you have an odd number of nots, it's all the same.

((p->left == NULL) != (p->right == NULL));
((p->left != NULL) != (p->right != NULL));
((p->left != NULL) == (p->right == NULL));
--
pete
Nov 14 '05 #28

P: n/a
Em Tue, 03 Feb 2004 21:36:42 +0000, Joona I Palaste escreveu:
<snip>


I'm enlightened, thanks!

--
Quidquid latine dictum sit altum viditur
Josť - Threading The Long Path to Nerdvana

Nov 14 '05 #29

P: n/a
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
# 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?

#define logical_equiv(p,q) (!(p)==!(q))
#define logical_xor(p,q) (!(p)!=!(q))
#define logical_implies(p,q) (!(p)<=!(q))

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I have no respect for people with no shopping agenda.
Nov 14 '05 #30

P: n/a
Kevin D. Quitt <KQ**********@IEEIncUNMUNG.com> wrote in message news:<gd********************************@4ax.com>. ..
I officially nominate this question as a FAQ, and request that the first
update to the FAQ since February 7, 1999 be made.


I agree.
Nov 14 '05 #31

P: n/a
Kevin D. Quitt <KQ**********@ieeincunmung.com> wrote:

Yes, for the same reason as || and &&, albeit there's no shortcutting. It
makes it *obvious* that a purely boolean expression is involved. !a ^ !b
isn't obviously purely boolean; the central operator is not boolean.


Then use the other exclusive-or operator: !a != !b.

-Larry Jones

I don't need to improve! Everyone ELSE does! -- Calvin
Nov 14 '05 #32

P: n/a
nrk <ra*********@devnull.verizon.net> writes:
Keith Thompson wrote:
nrk <ra*********@devnull.verizon.net> writes:
Christopher Benson-Manica wrote:
> 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.


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) )


Yeah, that would work. (I think it's ugly, but it would work.)

If I really had a need for logical xor, I'd probably use a macro,
something like:

#define XOR(a, b) (!(a) ^ !(b))

(I think that's right.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #33

P: n/a
Keith Thompson <ks***@mib.org> spoke thus:
#define XOR(a, b) (!(a) ^ !(b))


Finding an XOR in code would be rather annoying (to track down the
header file where it's defined), I would think.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #34

P: n/a
Kevin D. Quitt <KQ**********@IEEIncUNMUNG.com> writes:
I officially nominate this question as a FAQ, and request that the first
update to the FAQ since February 7, 1999 be made.


For what it's worth, I've added a page to my own personal FAQ
list:
http://www.msu.edu/~pfaffben/writing...gical-xor.html
Anyone have anything to add or dispute?
--
"It would be a much better example of undefined behavior
if the behavior were undefined."
--Michael Rubenstein
Nov 14 '05 #35

P: n/a

On Tue, 3 Feb 2004, Ben Pfaff wrote:

For what it's worth, I've added a page to my own personal FAQ
list:
http://www.msu.edu/~pfaffben/writing...gical-xor.html
Anyone have anything to add or dispute?


Nit: "Far more rarely useful"? :) How about "far less often"?

And I would appreciate a clarification about how !a != !b interacts
with doubles, and some newbie no doubt would appreciate a clarification
about how it interacts with pointers. (!NULL == (int)1, !foo == (int)0;
but I am still waiting for someone more often correct than I to address
the question of !NaN.)

-Arthur
Nov 14 '05 #36

P: n/a
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:
On Tue, 3 Feb 2004, Ben Pfaff wrote:

For what it's worth, I've added a page to my own personal FAQ
list:
http://www.msu.edu/~pfaffben/writing...gical-xor.html
Anyone have anything to add or dispute?
Nit: "Far more rarely useful"? :) How about "far less often"?


Yes, that's much better phrasing. I will update it.
And I would appreciate a clarification about how !a != !b interacts
with doubles, and some newbie no doubt would appreciate a clarification
about how it interacts with pointers. (!NULL == (int)1, !foo == (int)0;
but I am still waiting for someone more often correct than I to address
the question of !NaN.)


I'm not sure what you'd like me to say about those cases. It
seems to me that, with the possible exception of NaN, they work
out okay.
--
"Some programming practices beg for errors;
this one is like calling an 800 number
and having errors delivered to your door."
--Steve McConnell
Nov 14 '05 #37

P: n/a
nrk
Ben Pfaff wrote:
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:
On Tue, 3 Feb 2004, Ben Pfaff wrote:

For what it's worth, I've added a page to my own personal FAQ
list:
http://www.msu.edu/~pfaffben/writing...gical-xor.html
Anyone have anything to add or dispute?


Nit: "Far more rarely useful"? :) How about "far less often"?


Yes, that's much better phrasing. I will update it.
And I would appreciate a clarification about how !a != !b interacts
with doubles, and some newbie no doubt would appreciate a clarification
about how it interacts with pointers. (!NULL == (int)1, !foo == (int)0;
but I am still waiting for someone more often correct than I to address
the question of !NaN.)


I'm not sure what you'd like me to say about those cases. It
seems to me that, with the possible exception of NaN, they work
out okay.


I am not sure if its in the C standard, but I believe NaN is to compare
unequal to any other number and even to NaN itself. Since ! yields 0 if
the operand is not equal to 0, if the above were true, one would expect
!NaN to yield 0.

-nrk.

--
Remove devnull for email
Nov 14 '05 #38

P: n/a
Ben Pfaff <bl*@cs.stanford.edu> writes:
Kevin D. Quitt <KQ**********@IEEIncUNMUNG.com> writes:
I officially nominate this question as a FAQ, and request that the first
update to the FAQ since February 7, 1999 be made.


For what it's worth, I've added a page to my own personal FAQ
list:
http://www.msu.edu/~pfaffben/writing...gical-xor.html
Anyone have anything to add or dispute?


Other than the URL, there's no mention of the common abbreviation "xor";
there probably should be.

Another thing I just noticed: <iso646.h> defines the following
macros:

and &&
and_eq &=
bitand &
bitor |
compl ~
not !
not_eq !=
or ||
or_eq |=
xor ^
xor_eq ^=

The "and", "or", and "not" macros refer to the logical operators, but
the "xor" macro refers to the bitwise operator. If a logical-xor "^^"
operator were added, the asymmetry would be painful.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #39

P: n/a
> Ben Pfaff <bl*@cs.stanford.edu> writes:
Kevin D. Quitt <KQ**********@IEEIncUNMUNG.com> writes:
I officially nominate this question as a FAQ, and request that the first
update to the FAQ since February 7, 1999 be made.


For what it's worth, I've added a page to my own personal FAQ
list:
http://www.msu.edu/~pfaffben/writing...gical-xor.html
Anyone have anything to add or dispute?


You say that logical xor is used much more rarely than logical and/or.
Question: Has anybody _ever_ found a situation where they would have
wanted a logical xor?
Nov 14 '05 #40

P: n/a
Christian Bau <ch***********@cbau.freeserve.co.uk> writes:
Ben Pfaff <bl*@cs.stanford.edu> writes:
Kevin D. Quitt <KQ**********@IEEIncUNMUNG.com> writes:

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

For what it's worth, I've added a page to my own personal FAQ
list:
http://www.msu.edu/~pfaffben/writing...gical-xor.html
Anyone have anything to add or dispute?


You say that logical xor is used much more rarely than logical and/or.
Question: Has anybody _ever_ found a situation where they would have
wanted a logical xor?


Yes. I often use the bits of an `unsigned int' struct member to
represent a number of two-state attributes of an object, and
to test for (a combination of) attributes, I use the idiom

if (x->flags & FLAG_FOO) ...
if ((x->flags & FLAG_BAR) || (y->flags & FLAG_BAZ)) ...

where the tokens starting with FLAG_ are macros that expand to an
unsigned int constant expression with one and only one bit that is
set to 1. For symmetry reasons, I would prefer it if I could also
write

if ((x->flags & FLAG_BLAH) ^^ (y->flags & FLAG_BLUBB)) ...

IMHO, that would look nicer (i.e., more like similar expressions
involving && or ||) than the alternatives.

Martin
Nov 14 '05 #41

P: n/a
In article <bv*************@news.t-online.com>,
Martin Dickopp <ex****************@zero-based.org> wrote:
Christian Bau <ch***********@cbau.freeserve.co.uk> writes:
Ben Pfaff <bl*@cs.stanford.edu> writes:
> Kevin D. Quitt <KQ**********@IEEIncUNMUNG.com> writes:
>
> > I officially nominate this question as a FAQ, and request that the
> > first
> > update to the FAQ since February 7, 1999 be made.
>
> For what it's worth, I've added a page to my own personal FAQ
> list:
> http://www.msu.edu/~pfaffben/writing...gical-xor.html
> Anyone have anything to add or dispute?


You say that logical xor is used much more rarely than logical and/or.
Question: Has anybody _ever_ found a situation where they would have
wanted a logical xor?


Yes. I often use the bits of an `unsigned int' struct member to
represent a number of two-state attributes of an object, and
to test for (a combination of) attributes, I use the idiom

if (x->flags & FLAG_FOO) ...
if ((x->flags & FLAG_BAR) || (y->flags & FLAG_BAZ)) ...

where the tokens starting with FLAG_ are macros that expand to an
unsigned int constant expression with one and only one bit that is
set to 1. For symmetry reasons, I would prefer it if I could also
write

if ((x->flags & FLAG_BLAH) ^^ (y->flags & FLAG_BLUBB)) ...

IMHO, that would look nicer (i.e., more like similar expressions
involving && or ||) than the alternatives.


Yes, but have you actually needed it? In a real life program? Has it
happened that you wanted to take some action if exactly one of the flags
was set, but not both?

(A relatively common case would be where you believe that two conditions
that look different should have the same truth values and you use an
assert to either confirm this or find errors in the program or your
understanding of the program. But in this case I would prefer == or !=
because they express this clearer. For example, you might have two
pointers src and dst, and you know that if one is not NULL then the
other is not NULL as well, so you might write assert ((src != NULL) ==
(dst != NULL)) .)
Nov 14 '05 #42

P: n/a
Christian Bau wrote:
Ben Pfaff <bl*@cs.stanford.edu> writes:
Kevin D. Quitt <KQ**********@IEEIncUNMUNG.com> writes:

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

For what it's worth, I've added a page to my own personal FAQ
list:
http://www.msu.edu/~pfaffben/writing...gical-xor.html
Anyone have anything to add or dispute?


You say that logical xor is used much more rarely than logical and/or.
Question: Has anybody _ever_ found a situation where they would have
wanted a logical xor?


(!(a) != !(b)) is adequate for the situation.

unsigned char bit_rev(unsigned char byte)
{
unsigned hi_mask, lo_mask;

hi_mask = ((unsigned char)-1 >> 1) + 1;
lo_mask = 1;
do {
if (!(byte & hi_mask) != !(byte & lo_mask)) {
byte ^= hi_mask | lo_mask;
}
hi_mask >>= 1;
lo_mask <<= 1;
} while (hi_mask > lo_mask);
return byte;
}

--
pete
Nov 14 '05 #43

P: n/a
Christian Bau <ch***********@cbau.freeserve.co.uk> writes:
In article <bv*************@news.t-online.com>,
Martin Dickopp <ex****************@zero-based.org> wrote:
Christian Bau <ch***********@cbau.freeserve.co.uk> writes:
> Ben Pfaff <bl*@cs.stanford.edu> writes:
> > Kevin D. Quitt <KQ**********@IEEIncUNMUNG.com> writes:
> >
> > > I officially nominate this question as a FAQ, and request that the
> > > first
> > > update to the FAQ since February 7, 1999 be made.
> >
> > For what it's worth, I've added a page to my own personal FAQ
> > list:
> > http://www.msu.edu/~pfaffben/writing...gical-xor.html
> > Anyone have anything to add or dispute?

You say that logical xor is used much more rarely than logical and/or.
Question: Has anybody _ever_ found a situation where they would have
wanted a logical xor?
Yes. I often use the bits of an `unsigned int' struct member to
represent a number of two-state attributes of an object, and
to test for (a combination of) attributes, I use the idiom

if (x->flags & FLAG_FOO) ...
if ((x->flags & FLAG_BAR) || (y->flags & FLAG_BAZ)) ...

where the tokens starting with FLAG_ are macros that expand to an
unsigned int constant expression with one and only one bit that is
set to 1. For symmetry reasons, I would prefer it if I could also
write

if ((x->flags & FLAG_BLAH) ^^ (y->flags & FLAG_BLUBB)) ...

IMHO, that would look nicer (i.e., more like similar expressions
involving && or ||) than the alternatives.


Yes, but have you actually needed it?


Yes. (Well, "needed" is too strong, but I would have liked to have it.)
In a real life program?
Not in a program I have been paid to write. In fact, not in a program
I have published yet. But in a program that is supposed to be applied to
a real life problem - it's not some kind of contrived example program.
Has it happened that you wanted to take some action if exactly one of
the flags was set, but not both?


One flag describes whether an object is "active" in some sense (for
example, a GUI element which can be "selected" or "unselected"). The
other flag describes what the former flag should be set to. If and only
if the former flag will change, i.e. the object is inactive and will be
activated or it is active and will be deactivated, some actions are to
be performed (e.g., a GUI element will be redrawn).

Martin
Nov 14 '05 #44

P: n/a
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> wrote in message news:<Pi***********************************@unix41 .andrew.cmu.edu>...
Of course, both cases are pretty ugly, here. But I haven't yet
come up with a good example. ;-)
And while I think it would have been cool if C had been designed
with a ^^ operator from the start, IMHO it's much too late now to
try adding it to the standard language. C doesn't need any more
syntax, and even if it did, I don't think the new syntax would find
many willing users. A new version of C would IMHO suffer the same
problems as C99 still suffers w.r.t. market share, only worse.
Now, a *new* language... :-)


This is what I do when I need xor:

static inline int xor (int a, int b)
{
return a ? !b : b;
}

OR implement this with the binary '^' operator (I think it's slower
if you optimize).

The thing is that we couldn't do this with || and &&, because
these have early termination. And that's probably the reason for
their existance. In xor both must be evaluated.

And another problem is multiple xors. What happens then??

a ^^ b ^^ c ????

So xor is rather special
Stelios
Nov 14 '05 #45

P: n/a
stelios xanthakis wrote:
And another problem is multiple xors. What happens then??

a ^^ b ^^ c ????

So xor is rather special


No more than (e.g.) == or < in this respect as far as I can see.

Jeremy.
Nov 14 '05 #46

P: n/a

"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote in message
news:sl*******************@hehe.cl.cam.ac.uk...
stelios xanthakis wrote:
And another problem is multiple xors. What happens then??

a ^^ b ^^ c ????

So xor is rather special


No more than (e.g.) == or < in this respect as far as I can see.


== is a short circuit though. The intention would be that ^^ would be a
short-circuit (otherwise I don't see the point) and as the others pointed
out you have to evaluate both sides to determine the result (therefore it
cannot short circuit).

What you could do is make ^& and ^| for NAND and NOR (former shorts if left
is zero, latter if left is one) and you'd have all four short-circuits ;-)

Well actually make ^& into ^^ since it's ambiguous with ^&varname, etc...so
you get

^^ = NAND
^| = NOR
|| = OR
&& = AND

Tom
Nov 14 '05 #47

P: n/a
ma****@freemail.gr (stelios xanthakis) writes:
And another problem is multiple xors. What happens then??

a ^^ b ^^ c ????


I guess it would have left-to-right associativity just like &&
and ||, i.e. behave like (a ^^ b) ^^ c. But even if it behaved
like a ^^ (b ^^ c), the result would be the same. Where do you
see a problem?

Martin
Nov 14 '05 #48

P: n/a

"Martin Dickopp" <ex****************@zero-based.org> wrote in message
news:bv*************@news.t-online.com...
ma****@freemail.gr (stelios xanthakis) writes:
And another problem is multiple xors. What happens then??

a ^^ b ^^ c ????


I guess it would have left-to-right associativity just like &&
and ||, i.e. behave like (a ^^ b) ^^ c. But even if it behaved
like a ^^ (b ^^ c), the result would be the same. Where do you
see a problem?


You cannot short circuit the XOR logical op. So what's the point?

Tom
Nov 14 '05 #49

P: n/a
"Tom St Denis" <to********@iahu.ca> wrote:
"Jeremy Yallop" <je****@jdyallop.freeserve.co.uk> wrote in message
news:sl*******************@hehe.cl.cam.ac.uk...
stelios xanthakis wrote:
And another problem is multiple xors. What happens then??

a ^^ b ^^ c ????

So xor is rather special


No more than (e.g.) == or < in this respect as far as I can see.


== is a short circuit though.


No, it's not.

a == b

needs to evaluate both a and b, and

a == b == c

needs to evaluate all three.

Richard
Nov 14 '05 #50

80 Replies

This discussion thread is closed

Replies have been disabled for this discussion.