Topic: bool and xor
Author: ark@research.att.com (Andrew Koenig)
Date: 1997/12/15 Raw View
In article <349232EA.9A89CE7D@csrd.uiuc.edu>,
Nicholas Stavrakos <stavrako@csrd.uiuc.edu> wrote:
> To my surprise it seems that the ^ (xor) operator works only for
> integral types (or more specifically not for bools).
> Any reason for this?
Yes. People often use ^ as a way of doing bitwise computations on
integers, and it is important that C programs that do so not break.
(0==0)^2 is 3 in C, so it had better have the same value in C++.
--
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/12/15 Raw View
Nicholas Stavrakos <stavrako@csrd.uiuc.edu> writes:
> To my surprise it seems that the ^ (xor) operator works only for
> integral types (or more specifically not for bools).
>
> Any reason for this?
It's also a surprise for me that &, | and ^ are misssing
(from FDIS). I don't know if it's intentionnal or not.
The consequence is that the following code will rely on
bool convertions:
bool a, b, c = a & b, d = a | b, e = a ^ b;
This is IMO unfortunate, especially if a compiler output
warnings for bool convertions, or if you output the
result:
cout << a ^ b;
will print 0 or 1, not true or false (assumming we are
in the right ios mode).
Suggested fix: add pseudo proto:
bool operator^ (bool, bool)
bool operator& (bool, bool)
bool operator| (bool, bool)
under 13.6/24, but it may be too late.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: stephen.clamage_nospam@eng.Sun.COM (Steve Clamage)
Date: 1997/12/15 Raw View
On 13 Dec 1997 23:30:00 PST, Nicholas Stavrakos
<stavrako@csrd.uiuc.edu> wrote:
>To my surprise it seems that the ^ (xor) operator works only for
>integral types (or more specifically not for bools).
The bitwise operators work on the bit representation of integral
types. Type bool is not defined in terms of bit representations, so
the bitwise operators do not directly apply.
In a non-boolean expression, bool values get promoted to the int
values 0 or 1, and the bitwise operators work on those. You can write
(b1^b2) for two boolean values and get the exclusive-or result as an
int value. You can assign an int value to a bool target, so in the end
you get the effect of an xor operation on bools.
---
Steve Clamage, stephen.clamage_nospam@eng.sun.com
( Note: remove "_nospam" when replying )
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Hyman Rosen <cardboard.mti.sgi.com!sgi.sgi.com!ncar.UCAR.EDU!uunet!jyacc!hymie>
Date: 1997/12/15 Raw View
Nicholas Stavrakos <stavrako@csrd.uiuc.edu> writes:
> To my surprise it seems that the ^ (xor) operator works only for
> integral types (or more specifically not for bools).
>
> Any reason for this?
For bools, use '!='. It's exactly equivalent to '^'.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Barry Margolin <barmar@bbnplanet.com>
Date: 1997/12/16 Raw View
In article <349232EA.9A89CE7D@csrd.uiuc.edu>,
Nicholas Stavrakos <stavrako@csrd.uiuc.edu> wrote:
>To my surprise it seems that the ^ (xor) operator works only for
>integral types (or more specifically not for bools).
You don't need it. The != operator is equivalent to xor when applied to
bools. You also don't need bit-wise & and |, as you can use && and || (if
you don't want short-circuiting, you can assign to temporaries).
--
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: johnstrw@muss.CIS.McMaster.CA (R.W. Johnstone)
Date: 1997/12/16 Raw View
>It's also a surprise for me that &, | and ^ are misssing
>(from FDIS). I don't know if it's intentionnal or not.
>Suggested fix: add pseudo proto:
> bool operator^ (bool, bool)
> bool operator& (bool, bool)
> bool operator| (bool, bool)
Actually, this is unnecessary. Those functions, operator^, operator&, and
operator|, are known to be bitwise operators. For those who want boolean
operators we already have operator&& and operator||. Perhaps what we need
to complete the set is an operator^^. Thus, one could choose to do
bitwise or logical operations of all three kinds.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1997/12/17 Raw View
R.W. Johnstone wrote:
>
> >It's also a surprise for me that &, | and ^ are misssing
> >(from FDIS). I don't know if it's intentionnal or not.
>
> >Suggested fix: add pseudo proto:
> > bool operator^ (bool, bool)
> > bool operator& (bool, bool)
> > bool operator| (bool, bool)
>
> Actually, this is unnecessary. Those functions, operator^, operator&, and
> operator|, are known to be bitwise operators. For those who want boolean
> operators we already have operator&& and operator||. Perhaps what we need
> to complete the set is an operator^^. Thus, one could choose to do
> bitwise or logical operations of all three kinds.
Actually introducing ^^ is unecesarry and inconsistant. && and
|| have lazy evaluation, ^^ doesn't.
But the pseudo proto have already been changed in the direction
I am proposing. So I think it would be a good idea to ADD these
pseudo proto, without removing the other ones.
I do want to have bitwise and, or and xor, and I want this
statement to print 8108falsetrue :
cout << 10 & 2 << 10 | 2 << 10 ^ 2 << true & false << true ^ false;
So one could choose &, |, ^ for complete evaluation, and
&&, || for lazy evaluation.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://www.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: jbdp@cix.compulink.co.uk ("Julian Pardoe")
Date: 1997/12/17 Raw View
In article <676lkb$rkn@muss.CIS.McMaster.CA>,
johnstrw@muss.CIS.McMaster.CA (R.W. Johnstone) wrote:
> Perhaps what we need to complete the set is an operator^^.
If bools are guaranteed to be 0 or 1 (rather than the old 0 or
other-than-0) then we don't need "^^" -- or rather C++ already has it, but
spells it "!=". And at last C++ catches up with BCPL which has EQV
(bit-wise equality) to go along side NEQV, its spelling of XOR!!!
-- jP --
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1997/12/17 Raw View
ark@research.att.com (Andrew Koenig) writes:
|> In article <349232EA.9A89CE7D@csrd.uiuc.edu>,
|> Nicholas Stavrakos <stavrako@csrd.uiuc.edu> wrote:
|>
|> > To my surprise it seems that the ^ (xor) operator works only for
|> > integral types (or more specifically not for bools).
|>
|> > Any reason for this?
|>
|> Yes. People often use ^ as a way of doing bitwise computations on
|> integers, and it is important that C programs that do so not break.
|> (0==0)^2 is 3 in C, so it had better have the same value in C++.
But it would, even if xor were defined for bool. In mixed arithmetic,
it is the bool which converts to int, and not vice versa (I hope).
--
James Kanze +33 (0)1 39 23 84 71 mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient e objet --
-- Beratung in objektorientierter Datenverarbeitung
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1997/12/17 Raw View
R.W. Johnstone wrote:
>
> Actually, this is unnecessary. Those functions, operator^, operator&, and
> operator|, are known to be bitwise operators. For those who want boolean
> operators we already have operator&& and operator||.
Unless you want to prevent the compiler from short-circuit evaluation.
--
Ciao,
Paul
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1997/12/17 Raw View
Andrew Koenig wrote:
>
> Yes. People often use ^ as a way of doing bitwise computations on
> integers, and it is important that C programs that do so not break.
> (0==0)^2 is 3 in C, so it had better have the same value in C++.
But 2 isn't a bool, so (0==0) would be promoted to int before doing the ^
operation, wouldn't it?
When both parameters are bools, none of the bitwise operators would introduce
any C incompatibilities. One could argue that even & and | should be defined on
bool, since their semantics are different from && and || (they don't
short-circuit).
--
Ciao,
Paul
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "James Russell Kuyper Jr." <kuyper@wizard.net>
Date: 1997/12/18 Raw View
Valentin Bonnard wrote:
> It's also a surprise for me that &, | and ^ are misssing
> (from FDIS). I don't know if it's intentionnal or not.
>
> The consequence is that the following code will rely on
> bool convertions:
>
> bool a, b, c = a & b, d = a | b, e = a ^ b;
Try the following code instead:
bool a, b, c = a && b, d = a || b, e = a != b;
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Bill Wade" <bill.wade@stoner.com>
Date: 1997/12/18 Raw View
Valentin Bonnard <bonnardv@pratique.fr> wrote in article
<3497EDB7.167EB0E7@pratique.fr>...
> I do want to have bitwise and, or and xor, and I want this
> statement to print 8108falsetrue :
The intent was probably for the first character to be 2 == (10&2).
> cout << 10 & 2 << 10 | 2 << 10 ^ 2 << true & false << true ^ false;
Since this is parsed as
((cout << 10) & (2 << 10)) | (... some other stuff )
this will generate a compile time error, since the compiler refuses to
implicitly do the conversions:
ostream -> void* -> bool -> int
Currently the compiler will not even compile
cout << true & false;
for the same reason. If '&' worked with bool the compiler would compile
the line, but probably not give the expected output, since it would mean
bool(cout << true) & false;
which is currently legal.
Except for precedence, type of result, and some short circuiting
true^false is the same as true==false
true|false true||false
true&false true&&false
I'm undecided in the subject debate.
true==false
is an easy, but obscure solution. Is the type of (true^false) any more
surprising than the type of ('a'^'b')?
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1997/12/18 Raw View
johnstrw@muss.CIS.McMaster.CA (R.W. Johnstone) writes:
|> >It's also a surprise for me that &, | and ^ are misssing
|> >(from FDIS). I don't know if it's intentionnal or not.
|>
|> >Suggested fix: add pseudo proto:
|> > bool operator^ (bool, bool)
|> > bool operator& (bool, bool)
|> > bool operator| (bool, bool)
|>
|> Actually, this is unnecessary. Those functions, operator^, operator&, and
|> operator|, are known to be bitwise operators. For those who want boolean
|> operators we already have operator&& and operator||. Perhaps what we need
|> to complete the set is an operator^^. Thus, one could choose to do
|> bitwise or logical operations of all three kinds.
Except that the variants || and && are short circuiting, which you can't
have with xor.
--
James Kanze +33 (0)1 39 23 84 71 mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orientie objet --
-- Beratung in objektorientierter Datenverarbeitung
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: tony@online.tmx.com.au (Tony Cook)
Date: 1997/12/19 Raw View
Nicholas Stavrakos (stavrako@csrd.uiuc.edu) wrote:
: To my surprise it seems that the ^ (xor) operator works only for
: integral types (or more specifically not for bools).
Can anyone explain how this is?
Author: Olaf Weber <infovore@xs4all.nl>
Date: 1997/12/19 Raw View
J Kanze writes:
> johnstrw@muss.CIS.McMaster.CA (R.W. Johnstone) writes:
>> Actually, this is unnecessary. Those functions, operator^, operator&, and
>> operator|, are known to be bitwise operators. For those who want boolean
>> operators we already have operator&& and operator||. Perhaps what we need
>> to complete the set is an operator^^. Thus, one could choose to do
>> bitwise or logical operations of all three kinds.
> Except that the variants || and && are short circuiting, which you can't
> have with xor.
While short-circuiting doesn't make sense for an operator^^, it could
provide a sequence point between the evaluation of left and right
operand. Whether this is sufficiently useful is another issue.
--
Olaf Weber
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Marcelo Cantos <marcelo@io.mds.rmit.edu.au>
Date: 1997/12/22 Raw View
"Bill Wade" <bill.wade@stoner.com> writes:
> Except for precedence, type of result, and some short circuiting
> true^false is the same as true==false
Actually, != is the correct equivalent.
> true|false true||false
> true&false true&&false
>
> I'm undecided in the subject debate.
> true==false
> is an easy, but obscure solution.
Why is it obscure (!=, that is, not ==)? When you're dealing with a
single bit (which is what bool ostensibly is), x ^ y is the same as
saying "x is different to y".
Cheers,
Marcelo
--
______________________________________________________________________
Marcelo Cantos, Research Assistant __/_ marcelo@mds.rmit.edu.au
Multimedia Database Systems Group, RMIT / _ Tel 61-3-9282-2497
L2/723 Swanston St, Carlton VIC 3053, Aus/ralia ><_>Fax 61-3-9282-2490
Acknowledgements: errors - me; wisdom - God; sponsorship - RMIT
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: claus@faerber.muc.de (=?ISO-8859-1?Q?Claus_Andr=E9_F=E4rber?=)
Date: 1997/12/22 Raw View
Hyman Rosen <cardboard.mti.sgi.com!sgi.sgi.com!ncar.UCAR.EDU!uunet!jyacc!=
hymie> schrieb:
> Nicholas Stavrakos <stavrako@csrd.uiuc.edu> writes:
> > To my surprise it seems that the ^ (xor) operator works only for
> > integral types (or more specifically not for bools).
> >
> > Any reason for this?
>
> For bools, use '!=3D'. It's exactly equivalent to '^'.
No, not always. There are situations, where they are not equivalent, =20
especially if you do not have booleans but ints representing bool =20
values: 2 !=3D 1 gives false but 2 ^^ 1 would give true.
--=20
Claus Andr=E9 F=E4rber <http://www.muc.de/~cfaerber/> Fax: +49_8061_3361
PGP: ID=3D1024/527CADCD FP=3D12 20 49 F3 E1 04 9E 9E 25 56 69 A5 C6 A0 C=
9 DC
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Gerard Weatherby <gerardw@alum.mit.edu>
Date: 1997/12/22 Raw View
Paul D. DeRocco wrote:
>
> R.W. Johnstone wrote:
> For those who want boolean
> > operators we already have operator&& and operator||.
>
> Unless you want to prevent the compiler from short-circuit evaluation.
But wouldn't very explicit code be better, anyway? That is, given:
bool A( );
bool B( );
if (A( ) && B( ))
{...
where it's desired both A( ) and B( ) be called can be written:
const bool aResult = A( ); //ensure both functions called
const bool bResult = B( );
if (aResult && bResult)
{...
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Hyman Rosen <cardboard.mti.sgi.com!sgi.sgi.com!ncar.UCAR.EDU!uunet!jyacc!hymie>
Date: 1997/12/23 Raw View
claus@faerber.muc.de (Claus Andr F rber) writes:
> Hyman Rosen schrieb:
> > For bools, use '!='. It's exactly equivalent to '^'.
>
> No, not always. There are situations, where they are not equivalent,
> especially if you do not have booleans but ints representing bool
> values: 2 != 1 gives false but 2 ^^ 1 would give true.
What in the world are you talking about? Read my quote. For bools,
'!=' and '^' are exactly the same, so you don't need a boolean
exclusive-or operator. For integers you already have one.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Barry Margolin <barmar@bbnplanet.com>
Date: 1997/12/23 Raw View
In article <6k8fDV$ZcDB@faerber.muc.de>,
=?ISO-8859-1?Q?Claus_Andr=E9_F=E4rber?= <claus@faerber.muc.de> wrote:
>Hyman Rosen <cardboard.mti.sgi.com!sgi.sgi.com!ncar.UCAR.EDU!uunet!jyacc!=
>hymie> schrieb:
>> For bools, use '!=3D'. It's exactly equivalent to '^'.
>
>No, not always. There are situations, where they are not equivalent, =20
>especially if you do not have booleans but ints representing bool =20
>values: 2 !=3D 1 gives false but 2 ^^ 1 would give true.
If you want to treat ints representing bools as bools, use a conversion
operator, e.g.
bool(2) != bool(1)
--
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: "Bill Wade" <bill.wade@stoner.com>
Date: 1997/12/24 Raw View
Marcelo Cantos <marcelo@io.mds.rmit.edu.au> wrote in article
<bgsormcowu.fsf@io.mds.rmit.edu.au>...
> "Bill Wade" <bill.wade@stoner.com> writes:
>
> > Except for precedence, type of result, and some short circuiting
> > true^false is the same as true==false
>
> Actually, != is the correct equivalent.
Oops.
> [Deleted]
> Why is it obscure (!=, that is, not ==)? When you're dealing with a
> single bit (which is what bool ostensibly is), x ^ y is the same as
> saying "x is different to y".
That is correct.
Obscure is probably too strong a word. Here are some mild surprises when
using '!=' for logical-xor:
1) Precedence. ^ is between & and |, but && is between != and ||.
2) Argument types: (true && 2) treats arguments as bool, returning true.
(true!=2) treats
arguments as int, also returning true. I find
(a AND b) AND (a XOR b)
surprising (or perhaps obscure;-).
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Nicholas Stavrakos <stavrako@csrd.uiuc.edu>
Date: 1997/12/13 Raw View
To my surprise it seems that the ^ (xor) operator works only for
integral types (or more specifically not for bools).
Any reason for this?
Thanks in advance.
--
Nicholas Stavrakos
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]