Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Does true ^ true return false?

Reply
Thread Tools

Does true ^ true return false?

 
 
Siemel Naran
Guest
Posts: n/a
 
      06-17-2004
Does true ^ true return false?

int silly() { return 3; }

int main() {
bool t1 = silly();
bool t2 = true;
cout << (t1 ^ t2) << '\n';
}

Does the above program print "0" because true^true is false.

Or does it print "1" because 00000011^00000001 = 00000010


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      06-17-2004

"Siemel Naran" <(E-Mail Removed)> wrote in message
news:8%aAc.82249$(E-Mail Removed)...
> Does true ^ true return false?
>
> int silly() { return 3; }
>
> int main() {
> bool t1 = silly();
> bool t2 = true;
> cout << (t1 ^ t2) << '\n';
> }
>
> Does the above program print "0" because true^true is false.
>
> Or does it print "1" because 00000011^00000001 = 00000010
>


Surely it would take you five minutes to find out yourself? The answer is
"0". When 3 has been converted to a bool, it forgets it was ever 3 and just
becomes true.

john



 
Reply With Quote
 
 
 
 
AngleWyrm
Guest
Posts: n/a
 
      06-17-2004
"Siemel Naran" <(E-Mail Removed)> wrote in message
news:8%aAc.82249$(E-Mail Removed)...
> Does true ^ true return false?
>
> int silly() { return 3; }
>
> int main() {
> bool t1 = silly();
> bool t2 = true;
> cout << (t1 ^ t2) << '\n';

cout << "does t1 == 3? " << (t1 == 3) << " or not? " << (t1!=3) <<
endl;
> }
>
> Does the above program print "0" because true^true is false.
>
> Or does it print "1" because 00000011^00000001 = 00000010


It prints 0 because (true XOR true) is false.
t1 != 00000011.


 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      06-17-2004

"John Harrison" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
>
> Surely it would take you five minutes to find out yourself? The answer is
> "0". When 3 has been converted to a bool, it forgets it was ever 3 and just
> becomes true.


Yes, and i think it's clear from the standard.
4.12 An rvalue of arithmetic, enumeration, pointer, or pointer to member type
can be converted to an rvalue of type bool. A zero value, null pointer value, or
null member pointer value is converted to false; any other value is converted to
true.

-Sharad


 
Reply With Quote
 
Siemel Naran
Guest
Posts: n/a
 
      06-17-2004
"John Harrison" <(E-Mail Removed)> wrote in message
news:2jct25Fuvoj4U1@uni-

> Surely it would take you five minutes to find out yourself? The answer is
> "0". When 3 has been converted to a bool, it forgets it was ever 3 and

just
> becomes true.


I had a hunch what the right answer was, but just wanted to be sure about
the matter, because my code will have to run correctly on many different
platforms from UNIX to Windows. As for the standard and its quotes, I find
it very hard to understand in these matters of bit operations and such.


 
Reply With Quote
 
AngleWyrm
Guest
Posts: n/a
 
      06-17-2004
"Siemel Naran" <(E-Mail Removed)> wrote in message
news:6rcAc.82674$(E-Mail Removed)...
>
> I had a hunch what the right answer was, but just wanted to be sure about
> the matter, because my code will have to run correctly on many different
> platforms from UNIX to Windows. As for the standard and its quotes, I

find
> it very hard to understand in these matters of bit operations and such.
>


#include <cstdlib>
#include <iostream>
using namespace std;

int silly() { return 3; }

int main() {
bool t1 = silly(); // converts an int to a bool

// it's the same as saying t1 = (bool)( silly() )
cout << "is bool(silly()) same? "
<< ( t1 == (bool)(silly()) ) << endl;

// but just like converting a float to an int, resolution is lost
// converting bool back to int results in 1 or 0
cout << "is t1 == 1 true? " << ( t1==1 )
<< " or is it false? " << (t1 != 1)
<< endl;

// exclusive or is only one or the other, but not both:
cout << "\n1^0=" << (true^false) << "\n0^1=" << (false^true)
<< "\n1^1=" << (true^true) << "\n0^0=" << (false^false)
<< endl;

system("pause");
}





 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      06-17-2004
Siemel Naran wrote:

> "John Harrison" <(E-Mail Removed)> wrote in message
> news:2jct25Fuvoj4U1@uni-
>
>> Surely it would take you five minutes to find out yourself? The
>> answer is "0". When 3 has been converted to a bool, it forgets it was
>> ever 3 and

> just
>> becomes true.

>
> I had a hunch what the right answer was, but just wanted to be sure
> about the matter, because my code will have to run correctly on many
> different platforms from UNIX to Windows. As for the standard and its
> quotes, I find it very hard to understand in these matters of bit
> operations and such.


What is hard about that? A bool is a bool is a bool. There are only two
possible values for bool, which are true and false. It doesn't matter
what you used to initialize your variable. Bit operations don't have
anything to do with that.

 
Reply With Quote
 
AngleWyrm
Guest
Posts: n/a
 
      06-17-2004
An interesting use of xor is for switching. If we have a bool variable, and
we xor it with 0, what happens?
0 ^ 0 = 0
1 ^ 0 = 1
The result is the same as the original before we xor'd it. But if we xor it
with a 1:
0 ^ 1 = 1
1 ^ 1 = 0
The result is exactly opposite of the original. So we can use this to
either leave a bit alone, or switch it over to the other state. It becomes
a way to flip a switch, or change a flag or state.

The other functions, & and | do things like this as well. With AND, if we
use a 1:
0 & 1 = 0
1 & 1 = 1
The original state is preserved, but if we use a 0:
0 & 0 = 0
1 & 0 = 0
The result is always 0. It can be used to turn something off, or set it to
zero, or even be thought of as allowing/disallowing the state of the bit to
show through.

With OR, if we use a 0:
0 | 0 = 0
1 | 0 = 1
The original is once again showing through normally. But if we use a 1:
0 | 1 = 1
1 | 1 = 1
The result is always on, or set to 1.






 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      06-17-2004
On Thu, 17 Jun 2004 08:16:02 GMT in comp.lang.c++, "Siemel Naran"
<(E-Mail Removed)> wrote,
>platforms from UNIX to Windows. As for the standard and its quotes, I find
>it very hard to understand in these matters of bit operations and such.


It is probably a mistake to use ^ bitwise exclusive or with bool
operands. Use != instead.

 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      06-17-2004
"AngleWyrm" <(E-Mail Removed)> wrote:
> "Siemel Naran" <(E-Mail Removed)> wrote:
>
> > int silly() { return 3; }
> >
> > int main() {
> > bool t1 = silly();
> > bool t2 = true;
> > cout << (t1 ^ t2) << '\n';

> cout << "does t1 == 3? " << (t1 == 3) << " or not? " << (t1!=3) <<
> endl;
> > }
> >
> > Does the above program print "0" because true^true is false.
> >
> > Or does it print "1" because 00000011^00000001 = 00000010

>
> It prints 0 because (true XOR true) is false.


Actually, (true ^ true) is 0 because the operands of '^' undergo
integer promotions. If 0 is assigned to a bool then it would
be converted to 'false', but the OP's code didn't do that.

If he changed his first cout line to:
cout << boolalpha << (t1 ^ t2) << ' ' << t1 << '\n'
it would print "0 true", not "false true".
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 4.54 Why does defined() return true on empty arrays and hashes? PerlFAQ Server Perl Misc 0 04-21-2011 04:00 PM
[False,True] and [True,True] --> [True, True]????? bdb112 Python 45 04-29-2009 02:35 AM
does the logical expression return 1 ( if it is true ) absolutely? moosdau C Programming 6 12-29-2005 05:56 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
difference between return true; and return false; w i l l Javascript 4 07-04-2003 10:05 PM



Advertisments