473,387 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05
80 35006
"Tom St Denis" <to********@iahu.ca> writes:
"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?


I already explained in <bv*************@news.t-online.com> why I would
like to have a ^^ operator even without short-circuiting logic.

Martin
Nov 14 '05 #51

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40****************@news.individual.net...
"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.


hehehe I meant to write "isn't" oops.

Tom
Nov 14 '05 #52

"Martin Dickopp" <ex****************@zero-based.org> wrote in message
news:bv*************@news.t-online.com...
"Tom St Denis" <to********@iahu.ca> writes:
"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?


I already explained in <bv*************@news.t-online.com> why I would
like to have a ^^ operator even without short-circuiting logic.


how is

if (a ^^ b) { ... }

any diff from

if (a ^ b) { .... }

Even in a stmt you could write

(a^b)?1:0

Tom
Nov 14 '05 #53
> 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. It involved taking a string, consisting of astericks and spaces,
and making another string of asteriks and spaces, based on some rules.
I think it involved finite automata, or something like that. My
recollection is fuzzy.

And would the !a != !b thing be able to benefit from short circuiting?
If not, then there would be no harm in a logical xor, would there?
Nov 14 '05 #54
In <40***********@mindspring.com> pete <pf*****@mindspring.com> writes:
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.


Not being the OP, I can't figure out the intent of his code.

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


Why bother, the semantics should be obvious!

Finding definitions in headers is a piece of cake to anyone with the
right tools. People that maintain software they haven't written have to
do this all the time.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #56
"Tom St Denis" <to********@iahu.ca> writes:
"Martin Dickopp" <ex****************@zero-based.org> wrote in message
news:bv*************@news.t-online.com...
"Tom St Denis" <to********@iahu.ca> writes:
"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?


I already explained in <bv*************@news.t-online.com> why I would
like to have a ^^ operator even without short-circuiting logic.


how is

if (a ^^ b) { ... }

any diff from

if (a ^ b) { .... }


The assumed semantics of the ^^ operator is that it yields zero if both
operands have (possibly different) non-zero values.

Martin
Nov 14 '05 #57

"Martin Dickopp" <ex****************@zero-based.org> wrote in message
news:bv*************@news.t-online.com...
"Tom St Denis" <to********@iahu.ca> writes:
"Martin Dickopp" <ex****************@zero-based.org> wrote in message
news:bv*************@news.t-online.com...
"Tom St Denis" <to********@iahu.ca> writes:

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

I already explained in <bv*************@news.t-online.com> why I would
like to have a ^^ operator even without short-circuiting logic.
how is

if (a ^^ b) { ... }

any diff from

if (a ^ b) { .... }


The assumed semantics of the ^^ operator is that it yields zero if both
operands have (possibly different) non-zero values.


Hmm? I don't get what that means. so

a^^a == 1?
a^^b == 0 if a != b?

or do you mean if a&b != 0 then a^^b == 0 ?

To me this just doesn't seem like a well thought out idea.

Tom

Martin

Nov 14 '05 #58
nrk
Tom St Denis wrote:

"Martin Dickopp" <ex****************@zero-based.org> wrote in message
news:bv*************@news.t-online.com...
"Tom St Denis" <to********@iahu.ca> writes:
> "Martin Dickopp" <ex****************@zero-based.org> wrote in message
> news:bv*************@news.t-online.com...
> > "Tom St Denis" <to********@iahu.ca> writes:
> >
> > > "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?
> >
> > I already explained in <bv*************@news.t-online.com> why I
> > would like to have a ^^ operator even without short-circuiting logic.
>
> how is
>
> if (a ^^ b) { ... }
>
> any diff from
>
> if (a ^ b) { .... }
The assumed semantics of the ^^ operator is that it yields zero if both
operands have (possibly different) non-zero values.


Hmm? I don't get what that means. so

a^^a == 1?


a ^^ a would always be expected to be 0.
a^^b == 0 if a != b?

Yes, but only if both a != 0 and b != 0 as well. If a == 10, b == 20, you
would still expect a ^^ b to be 0. That's why its called the "logical" xor
operator. However, the bitwise xor is not 0.
or do you mean if a&b != 0 then a^^b == 0 ?

Yep.
To me this just doesn't seem like a well thought out idea.

Nope. Its precisely how one would expect "logical" xor to function. This
is why there are several messages in this thread suggesting alternatives to
the logical xor such as:
!a != !b
!a ^ !b
!!a ^ !!b

You seem to have some mis-contrued notion of what a logical xor operator is
supposed to do. Consider it as something which maps both its operands to 1
if they are non-zero, 0 otherwise, and then performs a bitwise xor on them.

-nrk.
Tom

Martin


--
Remove devnull for email
Nov 14 '05 #59
"Tom St Denis" <to********@iahu.ca> writes:
"Martin Dickopp" <ex****************@zero-based.org> wrote in message
news:bv*************@news.t-online.com...
"Tom St Denis" <to********@iahu.ca> writes:
"Martin Dickopp" <ex****************@zero-based.org> wrote in message
news:bv*************@news.t-online.com...
> "Tom St Denis" <to********@iahu.ca> writes:
>
> > "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?
>
> I already explained in <bv*************@news.t-online.com> why I would
> like to have a ^^ operator even without short-circuiting logic.

how is

if (a ^^ b) { ... }

any diff from

if (a ^ b) { .... }
The assumed semantics of the ^^ operator is that it yields zero if both
operands have (possibly different) non-zero values.


Hmm? I don't get what that means. so

a^^a == 1?


No.
a^^b == 0 if a != b?
No.
or do you mean if a&b != 0 then a^^b == 0 ?
No.
To me this just doesn't seem like a well thought out idea.


Is "logical exclusive OR" really that hard to understand? The operator
would yield 1 if one and only one of its operands is 0, and it would
yield 0 otherwise.

Martin
Nov 14 '05 #60
Martin Dickopp <ex****************@zero-based.org> writes:
"Tom St Denis" <to********@iahu.ca> writes:
or do you mean if a&b != 0 then a^^b == 0 ?


No.


Sorry, that should have been "yes." It would in fact be a property of
the ^^ operator.

Martin
Nov 14 '05 #61
Tom St Denis 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. 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


I'd prefer a !&& and a !|| operator.

These would go handsomly alongside ||| and &&& operators, but that's
another story.
Regards,

sidney

Nov 14 '05 #62
pete wrote:
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.


Well, by the same reasoning we could do away with !=, being as it is that

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

The whole point of this discussion is that C obviously lacks a logical
XOR operator, and whether there's a good reason for it. Adequacy doesn't
enter into it, otherwise !, &&, <, and > would suffice.

For my part, I'd be happy to see a ^^ but only if we get logical
"implies" and "equiv" operators as well.

Best regards,

Sidney

Nov 14 '05 #63
Sidney Cadot <si****@jigsaw.nl> writes:
Tom St Denis wrote:

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


I'd prefer a !&& and a !|| operator.

These would go handsomly alongside ||| and &&& operators, but that's
another story.


Right, because C doesn't look enough like modem line noise as it is.
8-)}

--
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 #64
Ben Pfaff <bl*@cs.stanford.edu> wrote in message news:<87************@pfaff.stanford.edu>...
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?


Short-circuit evaluation is not a _requirement_ of logical operators.
Many other ('old') languages have logical-and and logical-or operators
which are not short-circuited.

I think a more tangable example for the second case would be better.
Otherwise you seem to be dismissing out of hand the 'kludgy' look of
the != alternative. Perhaps...

((a < 0) != (b < 0)) /* one and only one operator is negative
*/

Thirdly, whilst practical use of logical xor is rare, it's rare
because it's relatively complex (in terms of NOT, AND and OR
operations which _easily_ cover 14 of the 16 binary boolean
operations). But when it is needed, it's absense is felt. It wouldn't
be particularly difficult for implementors to cater for.

Lastly, I think the principle reason is quite simply because Dennis
Ritchie didn't think it was necessary at the time (and possibly not
even now.) But AFAIK, there was no serious issue preventing it from
being put into C90 nor C99.

Keith Thompson's point about the macro xor is a good one, but I think
it simply highlights an error in naming (in C++?)

--
Peter
Nov 14 '05 #65
Martin Dickopp wrote:
"Tom St Denis" <to********@iahu.ca> writes:
.... snip ...
To me this just doesn't seem like a well thought out idea.


Is "logical exclusive OR" really that hard to understand? The
operator would yield 1 if one and only one of its operands is 0,
and it would yield 0 otherwise.


As others have pointed out, all other logical operators
short-circuit. xor cannot, since the second expression absolutely
must be evaluated. Changing this would cause much greater
confusion than "(!(a) != !(b))" and cohorts.

--
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 #66
In article
<h_*******************@twister01.bloor.is.net.cabl e.rogers.com>,
"Tom St Denis" <to********@iahu.ca> wrote:
how is

if (a ^^ b) { ... }

any diff from

if (a ^ b) { .... }


I would assume that a ^^ b would only check whether a is zero or
nonzero, and whether b is zero or nonzero, and I would assume that a and
b would be allowed to be floating point numbers or pointers.

I might assume that a is evaluated completely before b, with a sequence
point in between, so that ^^ would behave as close as possible like &&
or ||.
Nov 14 '05 #67

On Wed, 4 Feb 2004, CBFalconer wrote:

Martin Dickopp wrote:
"Tom St Denis" <to********@iahu.ca> writes:

... snip ...
To me this just doesn't seem like a well thought out idea.


Is "logical exclusive OR" really that hard to understand? The
operator would yield 1 if one and only one of its operands is 0,
and it would yield 0 otherwise.


As others have pointed out, all other logical operators
short-circuit. xor cannot, since the second expression absolutely
must be evaluated. Changing this would cause much greater
confusion than "(!(a) != !(b))" and cohorts.


*Nobody* is suggesting changing the short-circuiting semantics
of && or ||. But several people *are* suggesting the addition of
a third logical operator, ^^, which performs a logical XOR on its
two operands (after converting them to "boolean" 0 or 1).
The logical XOR, naturally, cannot short-circuit; there's nowhere
for it to short-circuit "to," if you get my drift. However, that
fact has nothing to do with the short-circuiting or not of && and ||.
& : Computes the bitwise AND of its operands.
| : Computes the bitwise OR of its operands.
^ : Computes the bitwise XOR of its operands.

&& : Computes the logical AND of its operands in an efficient
left-to-right manner.
|| : Computes the logical OR of its operands in an efficient
left-to-right manner.
^^ (proposed): Computes the logical XOR of its operands in an
efficient left-to-right manner.

So AIUI, the logical XOR operator would introduce a sequence
point between the evaluation of its operands (which incidentally
none of the suggested remedies do), and it would compute the
answer in a manner "as short-circuited as possible" -- i.e.,
not short-circuited at all.
As the fact that ^^ cannot short-circuit is apparent to anyone
with the ability to think, I don't think its not short-circuiting
is likely to confuse anyone with the ability to program. :-)

[Again: I'm a supporter of the status quo out of pessimism,
but I will attempt to demolish arguments that support the status
quo by reasoning alone -- because I think the omission of ^^
*was* a bad idea. Why hack up solutions involving != when what
the algorithm demands is a logical XOR? (...Because that's the
only way the language supports, is why. No deeper reason.) ]

-Arthur
Nov 14 '05 #68
CBFalconer wrote:
Martin Dickopp wrote:
"Tom St Denis" <to********@iahu.ca> writes:

... snip ...
To me this just doesn't seem like a well thought out idea.


Is "logical exclusive OR" really that hard to understand? The
operator would yield 1 if one and only one of its operands is 0,
and it would yield 0 otherwise.

As others have pointed out, all other logical operators
short-circuit.


Or, as it was once so aptly put: things that are to float must not be
made of stuff that's heavier than water, because the only things we see
now that float are made of stuff that is less heavy than water.
xor cannot, since the second expression absolutely
must be evaluated.
IMO, the short-circuiting property of && and || is incidental; their
being logical operators instead of binary operators is essential.

<rant>
Due to unfortunate historical accident, the concept of logical/boolean
values has ended up in C as a derived property of expressions, where it
should have been a proper type in itself. This is an untidy aspect of
the type system; any properly designed language (which, for all its
merits, C is /not/) has a separate Boolean type, and, therefore,
separate operators and/or operator semantics for Boolean values
(although, perhaps, overloaded on operators that are also meaningful on
other types).

In my opinion, the C approach to logical/boolean values is fundamentally
broken. I would applaud any attempt to plug this particular hole by
widening the difference between bitwise operations on integers, and
logical operations on Booleans (such as introduction of a true logical
XOR), although the flaw is essentially at a deeper level than
introduction of a new operator could ever hope to fix.
</rant>

So, the decision on whether to do short-circuit evaluation or not (if at
all possible) is a question to consider /after/ you decide to properly
distinguish integer/boolean types (or bitwise/logical operators), as in
C). These are separate issues, with the former taking precedence over
the latter. At least, that's how I feel about it.
Changing this would cause much greater
confusion than "(!(a) != !(b))" and cohorts.


Surely, you jest.

If I ever were to encounter "(!(a) != !(b))" in production code, well,
suffice it to say I would not be pleased.
Best regards,

Sidney

Nov 14 '05 #69
Christian Bau <ch***********@cbau.freeserve.co.uk> writes:
In article
<h_*******************@twister01.bloor.is.net.cabl e.rogers.com>,
"Tom St Denis" <to********@iahu.ca> wrote:
how is

if (a ^^ b) { ... }

any diff from

if (a ^ b) { .... }
I would assume that a ^^ b would only check whether a is zero or
nonzero, and whether b is zero or nonzero, and I would assume that a and
b would be allowed to be floating point numbers or pointers.


Agreed.
I might assume that a is evaluated completely before b, with a sequence
point in between, so that ^^ would behave as close as possible like &&
or ||.


The "&&" and "||" operators have a sequence point *because* they
short-circuit. Since "^^" doesn't (and can't) short-circuit, adding a
sequence point would be more confusing than not doing so.

If I were designing the C language from scratch, I'd probably add a
"^^" operator just for symmetry. But it's too late now. Even if we
could convince the committee to add "^^" to the C200Y standard, we'd
probably be stuck for the next 10 to 20 years with old compilers that
don't implement it. Any code that uses the new operator either
wouldn't be portable to the older compilers, or would have to use #if
tricks to use a C99-compatible XOR on compilers that don't support
"^^" -- which would be far uglier than any of the workarounds we've
already seen here.

Inertia, like gravity, sucks.

<OT>
Perl, whose expression syntax is heavily influenced by C, has "^" as a
bitwise xor operator and "xor" as a logical xor operator.
</OT>

--
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 #70
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
Christian Bau <ch***********@cbau.freeserve.co.uk> writes:
I might assume that a is evaluated completely before b, with a sequence
point in between, so that ^^ would behave as close as possible like &&
or ||.


The "&&" and "||" operators have a sequence point *because* they
short-circuit. Since "^^" doesn't (and can't) short-circuit, adding a
sequence point would be more confusing than not doing so.


Here is the reasoning for the sequence point: && doesn't short circuit
if the left part is true, and || doesn't short circuit if the left part
is false. ^^ should behave as much as possible like an && or || that
doesn't short circuit because of the particular value of the left part.

The sequence point in && and || also makes code like this defined:

if (++i == 2 || i == 3) ...
if (++i >= 10 && i <= 20) ...

Without the sequence point there would be undefined behavior.
Nov 14 '05 #71
In article <bv**********@news.tudelft.nl>
Sidney Cadot <si****@jigsaw.nl> writes:
<rant>
Due to unfortunate historical accident, the concept of logical/boolean
values has ended up in C as a derived property of expressions, where it
should have been a proper type in itself.
This may indeed be the case -- as Dennis Himself :-) has noted
before, the reason the & and | operators have the "wrong precedence"
(really, their odd position in the ANSI C grammar) is due to a very
early version of the language having no separate "&&" and "||"
operators. Instead, when handling an "if" expression (and perhaps
all "boolean context" expressions such as for and while loops as
well), the compiler treated binary "&" as a logical AND operator.
In other expression contexts, the compiler treated "&" as a bitwise
AND operator.

The operators were split well before 1978 to clean up the situation,
but the grammar was never adjusted to put & and | at "more appropriate"
levels.

(This may well be what you meant; I just thought I would expand
upon the "historical accident" a bit.)

[much snippage]
If I ever were to encounter "(!(a) != !(b))" in production code, well,
suffice it to say I would not be pleased.


If I wanted logical XOR, I would probably do this:

/* LXOR: logical xor. Like && and ||, guarantees left to right
evaluation. Both operands are always evaluated, of course.
The result is the "boolean normalization" of a xor'ed with
the "boolean normalization" of b. */
#define LXOR(a, b) ((a) ? !(b) : !!(b))

and then just use LXOR(expr1, expr2) as needed.

(If I did not care about the order guarantee I might "weaken" the
macro to (!(a) ^ !(b)), and adjust the comment.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #72
Sidney Cadot wrote:
CBFalconer wrote:
.... snip ...
Changing this would cause much greater
confusion than "(!(a) != !(b))" and cohorts.


Surely, you jest.

If I ever were to encounter "(!(a) != !(b))" in production code,
well, suffice it to say I would not be pleased.


Alright, how about:

#include <iso646.h>
#define logicalvalue(x) (not(x))
....
if (logicalvalue(a) != logicalvalue(b)) ....
or
if (logicalvalue(a) xor logicalvalue(b)) ....

:-)

However I do not jest that the built in expectation of short
circuit performance of && and || should not be confused by yet
another exception for ^^. Bad idea.

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

Martin


The problem is just that this is logically confusing
by itself.

a ^^ b ^^ c

means "not c if a and b are different, and c if a and b are the same".
This is clearly a mess so my point was that although a single ^^
might be useful some times, sequential ^^s are so rare that we
can consider them impossible. Unlike || and && where multiple
such are very frequently used.

A *logical* expression a ^^ b ^^ c ^^ d ^^ e
is beyond the understanding of the average human being.

But no real problem though...

Stelios
Nov 14 '05 #74
ma****@freemail.gr (stelios xanthakis) wrote:
A *logical* expression a ^^ b ^^ c ^^ d ^^ e
is beyond the understanding of the average human being.


Not really. It means "an odd number of these expressions are true". Of
course, the usefulness of that test is debatable, but it's quite
understandable.

Richard
Nov 14 '05 #75
In article <8a**************************@posting.google.com > ma****@freemail.gr (stelios xanthakis) writes:
....
A *logical* expression a ^^ b ^^ c ^^ d ^^ e
is beyond the understanding of the average human being.


It is actually quite simple. It is 1 if there are an odd number of
a to e non-zero, 0 otherwise.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #76


Dik T. Winter wrote:
In article <8a**************************@posting.google.com > ma****@freemail.gr (stelios xanthakis) writes:
...
> A *logical* expression a ^^ b ^^ c ^^ d ^^ e
> is beyond the understanding of the average human being.


It is actually quite simple. It is 1 if there are an odd number of
a to e non-zero, 0 otherwise.


Which, by the way, could be very different from what someone might want to
express: 'I want an /exclusive/ OR over 5 logical expressions'. That would
sound to me like an expression that yields 1 if exactly one of the 5 logical
expressions is non-zero and 0 otherwise :-) The only simple way I see to
express that in C is: !!(a) + !!(b) + !!(c) + !!(d) + !!(e) == 1.

In other words: while 2-operand logical AND and OR are straightforward to
extend to n-operand, and easy to express using C's && and || operators, an
n-operand (logical) XOR may not be so well defined in everyone's mind and
depending on the chosen definition might not be easy to express with ^ (or
with the imaginary/proposed ^^ operator).

So, although Stelios' expression is well within the understanding of human
beings (I will count myself as average at any rate), it is certainly for me
counterintuitive.

Oh boy, is this getting off topic?

--
ir. H.J.H.N. Kenter ^^
Electronic Design & Tools oo ) Philips Research Labs
Building WAY 3.23 =x= \ ar**********@philips.com
Prof. Holstlaan 4 (WAY31) | \ tel. +31 40 27 45334
5656 AA Eindhoven /|__ \ tfx. +31 40 27 44626
The Netherlands (____)_/ http://www.kenter.demon.nl/

Famous last words: Segmentation Fault (core dumped)

Nov 14 '05 #77
In article <40**************@philips.com.dontspamme> Arjan Kenter <ar**********@philips.com.dontspamme> writes:
Dik T. Winter wrote:
In article <8a**************************@posting.google.com > ma****@freemail.gr (stelios xanthakis) writes:
...
> A *logical* expression a ^^ b ^^ c ^^ d ^^ e
> is beyond the understanding of the average human being.
It is actually quite simple. It is 1 if there are an odd number of
a to e non-zero, 0 otherwise.


Which, by the way, could be very different from what someone might want to
express: 'I want an /exclusive/ OR over 5 logical expressions'.


In that case he wants a quinary operator not a chain of binary operators
(he should look at lisp...).

Note that the above chain can be written as such by virtue of the
associativity of the operator. If the operator is not associative
(as multiplication on octonions), it *should* be written with
parenthesis. (And in mathematics that is the definition.)
Oh boy, is this getting off topic?


In a sense.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 14 '05 #78
stelios xanthakis wrote:
A *logical* expression a ^^ b ^^ c ^^ d ^^ e
is beyond the understanding of the average human being.


Maybe "average human beings" shouldn't try to write or
maintain non-trivial C programs.

That expression is true if an odd number of a, b, c, d,
and e are true.

- Larry
Nov 14 '05 #79
In article <8a**************************@posting.google.com >,
ma****@freemail.gr (stelios xanthakis) wrote:
A *logical* expression a ^^ b ^^ c ^^ d ^^ e
is beyond the understanding of the average human being.


It means "one, three or five of these are true".
Nov 14 '05 #80
Larry Doolittle wrote:
stelios xanthakis wrote:
A *logical* expression a ^^ b ^^ c ^^ d ^^ e
is beyond the understanding of the average human being.


Maybe "average human beings" shouldn't try to write or
maintain non-trivial C programs.

That expression is true if an odd number of a, b, c, d,
and e are true.

- Larry


#define _(x) !!(x)

now your expression, including ensuring logical values, becomes:

_(a) ^ _b) ^ _(c) ^ _(d) ^ _(d) ^ _(e)
or
a ^ b ^ c ^ d ^ e

when the values are preknown to be logical, i.e. 0 or 1. In that
case you can also use:

(a + b + c + d + e) & 1

(and I believe the single _ to be a valid identifier)

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

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

Similar topics

4
by: user | last post by:
Hi, Is it possible to override assignment, the way that '+' can be overridden for example? Thanks, Toby
14
by: lutorm | last post by:
Hi everyone, I'm trying to use istream_iterators to read a file consisting of pairs of numbers. To do this, I wrote the following: #include <fstream> #include <vector> #include <iterator> ...
5
by: kenny Nguyen | last post by:
Hi, Does anyone know the operator "=+"? If you do, what special method is related to this operator? Is it the __iadd__ method, but I think this is related to "+=" operator. Thanks, Kenny
67
by: carlos | last post by:
Curious: Why wasnt a primitive exponentiation operator not added to C99? And, are there requests to do so in the next std revision? Justification for doing so: C and C++ are increasingly used...
12
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
33
by: SpreadTooThin | last post by:
Can these operators be overloaded? If so. How?
18
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
6
by: Rudi | last post by:
Hello! I'm searching like a similar syntax for if(): int i=5; if (i in ) doSomething; e.g. enum TaskStates {Idle,Proc1,Proc2, ... ProcN}
19
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.