363,924 Members | 2595 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

Why isn't there a logical XOR operator?

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

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


Joona I Palaste
P: n/a
Joona I Palaste
Christopher Benson-Manica <ataru@nospam.cyberspace.org> scribbled the following:[color=blue]
> 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?[/color]

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 (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
Nov 14 '05 #2

Joona I Palaste
P: n/a
Joona I Palaste
Joona I Palaste <palaste@cc.helsinki.fi> scribbled the following:[color=blue]
> Christopher Benson-Manica <ataru@nospam.cyberspace.org> scribbled the following:[color=green]
>> 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?[/color][/color]
[color=blue]
> 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.[/color]

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

--
/-- Joona Palaste (palaste@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

nrk
P: n/a
nrk
Christopher Benson-Manica wrote:
[color=blue]
> 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?
>[/color]
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

Christopher Benson-Manica
P: n/a
Christopher Benson-Manica
Joona I Palaste <palaste@cc.helsinki.fi> spoke thus:
[color=blue]
> 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.[/color]

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

Christopher Benson-Manica
P: n/a
Christopher Benson-Manica
Joona I Palaste <palaste@cc.helsinki.fi> spoke thus:
[color=blue]
> Also if there were an unary ^ operator, we could write Japanese smileys
> into our code. For example:
> sashimi ^=^ kawaii;[/color]

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

xarax
P: n/a
xarax
"nrk" <ram_nrk2000@devnull.verizon.net> wrote in message
news:maPTb.47$EH5.13@nwrddc01.gnilink.net...[color=blue]
> Christopher Benson-Manica wrote:
>[color=green]
> > 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?
> >[/color]
> 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.
>[/color]

(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

Joona I Palaste
P: n/a
Joona I Palaste
xarax <xarax@email.com> scribbled the following:[color=blue]
> "nrk" <ram_nrk2000@devnull.verizon.net> wrote in message
> news:maPTb.47$EH5.13@nwrddc01.gnilink.net...[color=green]
>> Christopher Benson-Manica wrote:[color=darkred]
>> > 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?
>> >[/color]
>> 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.[/color][/color]
[color=blue]
> (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?[/color]

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

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

CBFalconer
P: n/a
CBFalconer
Christopher Benson-Manica wrote:[color=blue]
>
> 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?[/color]

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

c = !!a ^ !!b;

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


Nov 14 '05 #9

nrk
P: n/a
nrk
xarax wrote:
[color=blue]
> "nrk" <ram_nrk2000@devnull.verizon.net> wrote in message
> news:maPTb.47$EH5.13@nwrddc01.gnilink.net...[color=green]
>> Christopher Benson-Manica wrote:
>>[color=darkred]
>> > 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?
>> >[/color]
>> 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.
>>[/color]
>
> (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?[/color]

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

Dan Pop
P: n/a
Dan Pop
In <bvodmk$3pe$1@chessie.cirr.com> Christopher Benson-Manica <ataru@nospam.cyberspace.org> writes:
[color=blue]
>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).[/color]

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.
[color=blue]
>Am I the only one who would find it useful?[/color]

Are you still convinced that it would be useful?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de
Nov 14 '05 #11

Leor Zolman
P: n/a
Leor Zolman
On 3 Feb 2004 16:04:18 GMT, Joona I Palaste <palaste@cc.helsinki.fi>
wrote:
[color=blue]
>xarax <xarax@email.com> scribbled the following:[color=green]
>> "nrk" <ram_nrk2000@devnull.verizon.net> wrote in message
>> news:maPTb.47$EH5.13@nwrddc01.gnilink.net...[color=darkred]
>>> 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.[/color][/color]
>[color=green]
>> (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?[/color]
>
>Yes, ((!!a) != (!!b)) is equivalent to ((!a) != (!b)).[/color]

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
leor@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

Leor Zolman
P: n/a
Leor Zolman
[color=blue][color=green]
>>
>>Yes, ((!!a) != (!!b)) is equivalent to ((!a) != (!b)).[/color]
>
>Huh? That's saying !!a is equivalent to !a. I think not.[/color]

Oops, pounced too quick, sorry. Actually they're _all_ equivalent ;-)
-leor
[color=blue]
>Applying de Morgan's Theorem,
> ((!!a) != (!!b))
>can be rewritten as:
> ! (!a == !b)
>though.
> -leor
>
>
>Leor Zolman
>BD Software
>leor@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[/color]

Leor Zolman
BD Software
leor@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

CBFalconer
P: n/a
CBFalconer
Leor Zolman wrote:[color=blue]
> Joona I Palaste <palaste@cc.helsinki.fi> wrote:
>[/color]
.... snip ...[color=blue][color=green]
> >
> >Yes, ((!!a) != (!!b)) is equivalent to ((!a) != (!b)).[/color]
>
> 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.[/color]

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

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


Nov 14 '05 #14

Dan Pop
P: n/a
Dan Pop
In <i8iv10tm9gtl9q1iduapaeggrdjrgjd9bt@4ax.com> Leor Zolman <leor@bdsoft.com> writes:
[color=blue]
>On 3 Feb 2004 16:04:18 GMT, Joona I Palaste <palaste@cc.helsinki.fi>
>wrote:
>[color=green]
>>xarax <xarax@email.com> scribbled the following:[color=darkred]
>>> "nrk" <ram_nrk2000@devnull.verizon.net> wrote in message
>>> news:maPTb.47$EH5.13@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.[/color]
>>[color=darkred]
>>> (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?[/color]
>>
>>Yes, ((!!a) != (!!b)) is equivalent to ((!a) != (!b)).[/color]
>
>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.[/color]

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: Dan.Pop@ifh.de
Nov 14 '05 #15

xarax
P: n/a
xarax
"nrk" <ram_nrk2000@devnull.verizon.net> wrote in message
news:vJPTb.336$9a7.228@nwrddc02.gnilink.net...[color=blue]
> xarax wrote:
>[color=green]
> > "nrk" <ram_nrk2000@devnull.verizon.net> wrote in message
> > news:maPTb.47$EH5.13@nwrddc01.gnilink.net...[color=darkred]
> >> 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.
> >>[/color]
> >
> > (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?[/color]
>
> 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.[/color]

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

Keith Thompson
P: n/a
Keith Thompson
nrk <ram_nrk2000@devnull.verizon.net> writes:[color=blue]
> Christopher Benson-Manica wrote:[color=green]
> > 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?
> >[/color]
> 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.[/color]

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) kst-u@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

nrk
P: n/a
nrk
Keith Thompson wrote:
[color=blue]
> nrk <ram_nrk2000@devnull.verizon.net> writes:[color=green]
>> Christopher Benson-Manica wrote:[color=darkred]
>> > 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?
>> >[/color]
>> 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.[/color]
>
> 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.
>[/color]

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

pete
P: n/a
pete
Dan Pop wrote:[color=blue]
>
> In <i8iv10tm9gtl9q1iduapaeggrdjrgjd9bt@4ax.com> Leor Zolman <leor@bdsoft.com> writes:
>[color=green]
> >On 3 Feb 2004 16:04:18 GMT, Joona I Palaste <palaste@cc.helsinki.fi>
> >wrote:
> >[color=darkred]
> >>xarax <xarax@email.com> scribbled the following:
> >>> "nrk" <ram_nrk2000@devnull.verizon.net> wrote in message
> >>> news:maPTb.47$EH5.13@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)).[/color]
> >
> >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.[/color]
>
> 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.[/color]

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

Kevin D. Quitt
P: n/a
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.


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

Kevin D. Quitt
P: n/a
Kevin D. Quitt
On 3 Feb 2004 16:09:18 GMT, Dan.Pop@cern.ch (Dan Pop) wrote:[color=blue]
>Are you still convinced that it would be useful?[/color]

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

José de Paula
P: n/a
José de Paula
Em Tue, 03 Feb 2004 17:05:14 +0000, CBFalconer escreveu:
[color=blue]
> Christopher Benson-Manica wrote:[/color]
<snip>
[color=blue]
> if (!!a ^ !!b) ....
>
> c = !!a ^ !!b;[/color]

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

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

On Tue, 3 Feb 2004, Dan Pop wrote:[color=blue]
>
> Christopher Benson-Manica writes:[/color]
[re: introducing a logical XOR operator][color=blue][color=green]
> >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).[/color]
>
> 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.[/color]

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?)
[color=blue]
> If a and b are already
> logical expressions (i.e. expressions that evaluate to 0 or 1), a ^ b is
> enough.
>[color=green]
> >Am I the only one who would find it useful?[/color]
>
> Are you still convinced that it would be useful?[/color]

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

Joona I Palaste
P: n/a
Joona I Palaste
José de Paula <jose_de_paula@ig.com.br> scribbled the following:[color=blue]
> Em Tue, 03 Feb 2004 17:05:14 +0000, CBFalconer escreveu:[color=green]
>> Christopher Benson-Manica wrote:[/color]
> <snip>[color=green]
>> if (!!a ^ !!b) ....
>>
>> c = !!a ^ !!b;[/color][/color]
[color=blue]
> Why !!a ? Doesn't this have the same effect of a? Like
> if (a ^ b) ....
> c = a ^ b;[/color]
[color=blue]
> Could you enlighten us?[/color]

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

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

On Tue, 3 Feb 2004, [iso-8859-1] José de Paula wrote:[color=blue]
>
> Em Tue, 03 Feb 2004 17:05:14 +0000, CBFalconer escreveu:[color=green]
> > Christopher Benson-Manica wrote:[/color]
>[color=green]
> > if (!!a ^ !!b) ....
> >
> > c = !!a ^ !!b;[/color]
>
> Why !!a ? Doesn't this have the same effect of a? Like
> if (a ^ b) ....
> c = a ^ b;
>
> Could you enlighten us?[/color]

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

80 Replies

Post your reply

Help answer this question



Didn't find the answer to your C / C++ question?

You can also browse similar questions: C / C++ c xor