Does true ^ true return false?

S

Siemel Naran

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
 
J

John Harrison

Siemel Naran said:
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
 
A

AngleWyrm

Siemel Naran said:
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.
 
S

Sharad Kala

John Harrison said:
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
 
S

Siemel Naran

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

AngleWyrm

Siemel Naran said:
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");
}
 
R

Rolf Magnus

Siemel said:
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.
 
A

AngleWyrm

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

David Harmon

On Thu, 17 Jun 2004 08:16:02 GMT in comp.lang.c++, "Siemel Naran"
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.
 
O

Old Wolf

AngleWyrm said:
cout << "does t1 == 3? " << (t1 == 3) << " or not? " << (t1!=3) <<
endl;

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".
 
C

Chris Theis

Siemel Naran said:
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.

This is rather a question of conversion but really bit ops.

Regards
Chris
 
C

Chris Theis

David Harmon said:
On Thu, 17 Jun 2004 08:16:02 GMT in comp.lang.c++, "Siemel Naran"


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

Why do you consider using ^ bitwise exclusive or with bool operads a
mistake? IMHO this is perfectly okay and I can't see why one should refrain
(however I might be overlooking something).

Regards
Chris
 
D

David Harmon

Why do you consider using ^ bitwise exclusive or with bool operads a
mistake? IMHO this is perfectly okay and I can't see why one should refrain
(however I might be overlooking something).

Convert from bool to int, then bitwise xor. How is that helpful?

Assuming you wanted a bool result, != is very clear as to what is intended and could have avoided this whole thread.
 
E

Eric Schmidt

Siemel Naran said:
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

The above program is ill-formed because it uses an undeclared
identifier cout. Therefore, if it even compiles, it may print
anything.
 
M

Mats Weber

Chris Theis said:
Why do you consider using ^ bitwise exclusive or with bool operads a
mistake? IMHO this is perfectly okay and I can't see why one should refrain
(however I might be overlooking something).

But then, why do we need && and ||, we could simply use & and |
 
D

David Harmon

On Fri, 18 Jun 2004 02:51:51 +0200 in comp.lang.c++, Mats Weber
But then, why do we need && and ||, we could simply use & and |

&& does not evaluate the RHS if the LHS is false.
|| does not evaluate the RHS if the LHS is true.
^ has no similar equivalent.
 
S

Siemel Naran

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

I have an objects a1 and a2 of type A. There is a functionA::something()
that returns a bool. If one of a1 and a2 have something() == true, do I do
some extra processing. Thus:

if (a1->something() ^ a2->something()) {
process(a1, a2);
}

I guess one could say

if ((a1->something() && !a2->something()) || (!a1->something() &&
a2->something())) {
process(a1, a2);
}

But the intent of the first version looks clearer. Or there may be other
ways that I've overlooked :).
 
J

John Harrison

Siemel Naran said:
I have an objects a1 and a2 of type A. There is a functionA::something()
that returns a bool. If one of a1 and a2 have something() == true, do I do
some extra processing. Thus:

if (a1->something() ^ a2->something()) {
process(a1, a2);
}

I guess one could say

if ((a1->something() && !a2->something()) || (!a1->something() &&
a2->something())) {
process(a1, a2);
}

But the intent of the first version looks clearer. Or there may be other
ways that I've overlooked :).

As David said

if (a1->something() != a2->something()) {
process(a1, a2);
}

john
 
C

Chris Theis

David Harmon said:
Convert from bool to int, then bitwise xor. How is that helpful?

Well, I never claimed it to be helpful and absolutely share your opinion but
I wouldn't consider it a "mistake" (however this might be nit-picking) ;-)
Assuming you wanted a bool result, != is very clear as to what is intended
and could have avoided this whole thread.

Point taken and I absolutely agree!

Cheers
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top