473,890 Members | 2,014 Online

# Why isn't there 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). 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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05
80 35192
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.discl aimer>
_
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
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

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_IsUselessBra nch(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
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.hel sinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Make money fast! Don't feed it!"
- Anon
Nov 14 '05 #24

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
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
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
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_IsUselessBra nch(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
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
Christopher Benson-Manica <at***@nospam.c yberspace.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

This thread has been closed and replies have been disabled. Please start a new discussion.