Topic: Result of "bool v = true; v ^= 2;
Author: Manuel Menezes de Sequeira <Manuel.Sequeira@iscte.pt>
Date: 2000/02/01 Raw View
I was experimenting with assignement operators and boolean operators and
wrote the following code:
#include <iostream>
using namespace std;
int main() {
bool v = true;
v ^= 2;
cout << (v ? "true" : "false") << endl;
bool w = true;
w = w ^ bool(2);
cout << (w ? "true" : "false") << endl;
}
I compiled it both with egcs-1.1.2-12 (egcs-2.91.66) and VC++ 5. In
both cases the result of execution was:
true
false
I thought it should have been:
false
false
According to 5.17 clause 3, the expression "2" in "v ^= 2;" is
"implicitly converted (clause 4) to the cv-unqualified type of the left
operand". According to 4.7 clause 4, "if the destination type is bool,
see 4.12", which says, "an rvalue of [several types] can be converted to
an rvalue of type bool. A zero value [...] is converted to false; any
other value is converted to true." Hence, the expression "2" should be
converted to true. According to 5.17 clause 7, "the behaviour of an
expression of the form E1 op= E2 is equivalent to E1 = E1 op E2 except
that E1 is evaluated only once." The result of "v ^= 2;" should then be
the same as obtained by the expression "v = v ^ bool(2);", i.e., "v = v
^ true", if I understood the standard right. Acording to 5.12, operands
E1 and E2 should then undergo the usual arithmetic conversions in 5
clause 9, which states "[...] - otherwise, the integral promotions (4.5)
shall be performed on both operands." According to 4.5 clause 4, "an
rvalue of type bool can be converted to an rvalue of type int, with
false becoming zero and true becoming one." Hence, the result of "v ^
true" should be the same result of "1 ^ 1", i.e., zero. Converted back
to bool, the result in "v" should be false.
I am assuming that the E2 in 5.17 clause 7 refers to the second operand
but after the implicit conversion mentioned in clause 3, though this is
not very clear in the text of the standard.
Manuel Menezes de Sequeira
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Manuel Menezes de Sequeira <Manuel.Sequeira@iscte.pt>
Date: 2000/02/01 Raw View
I was experimenting with assignement operators and boolean operators and
wrote the following code:
#include <iostream>
using namespace std;
int main() {
bool v = true;
v ^= 2;
cout << (v ? "true" : "false") << endl;
bool w = true;
w = w ^ bool(2);
cout << (w ? "true" : "false") << endl;
}
I compiled it both with egcs-1.1.2-12 (egcs-2.91.66) and VC++ 5. In
both cases the result of execution was:
true
false
I thought it should have been:
false
false
According to 5.17 clause 3, the expression "2" in "v ^= 2;" is
"implicitly converted (clause 4) to the cv-unqualified type of the left
operand". According to 4.7 clause 4, "if the destination type is bool,
see 4.12", which says, "an rvalue of [several types] can be converted to
an rvalue of type bool. A zero value [...] is converted to false; any
other value is converted to true." Hence, the expression "2" should be
converted to true. According to 5.17 clause 7, "the behaviour of an
expression of the form E1 op= E2 is equivalent to E1 = E1 op E2 except
that E1 is evaluated only once." The result of "v ^= 2;" should then be
the same as obtained by the expression "v = v ^ bool(2);", i.e., "v = v
^ true", if I understood the standard right. Acording to 5.12, operands
E1 and E2 should then undergo the usual arithmetic conversions in 5
clause 9, which states "[...] - otherwise, the integral promotions (4.5)
shall be performed on both operands." According to 4.5 clause 4, "an
rvalue of type bool can be converted to an rvalue of type int, with
false becoming zero and true becoming one." Hence, the result of "v ^
true" should be the same result of "1 ^ 1", i.e., zero. Converted back
to bool, the result in "v" should be false.
I am assuming that the E2 in 5.17 clause 7 refers to the second operand
but after the implicit conversion mentioned in clause 3, though this is
not very clear in the text of the standard.
Manuel Menezes de Sequeira
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Steve Clamage <stephen.clamage@sun.com>
Date: 2000/02/01 Raw View
Manuel Menezes de Sequeira wrote:
>
> I was experimenting with assignement operators and boolean operators and
> wrote the following code:
>
> #include <iostream>
> using namespace std;
>
> int main() {
> bool v = true;
> v ^= 2;
> cout << (v ? "true" : "false") << endl;
> bool w = true;
> w = w ^ bool(2);
> cout << (w ? "true" : "false") << endl;
> }
>
> I compiled it both with egcs-1.1.2-12 (egcs-2.91.66) and VC++ 5. In
> both cases the result of execution was:
> true
> false
>
> I thought it should have been:
> false
> false
The expression v^=2 is equivalent to v=v^2, except that v is evaluated
only once (which is irrelevant here). Since 2 has type int, v is
promoted to an int, and "true" is converted to the value 1. The result
of v^2 is therefore 1^2, having the value 3, which is converted to
"true" for assignment to v.
Refer to clause 5 paragraph 9, section 4.5, and section 5.17.
Possibly you were thinking that in v^2 the 2 should have been
converted to bool. That is not the case, because ^ is not a
boolean operator, but an integer bitwise operator.
You can get that effect by writing an explicit conversion:
v ^= bool(2); // yields "false"
--
Steve Clamage, stephen.clamage@sun.com
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Manuel Menezes de Sequeira <Manuel.Sequeira@iscte.pt>
Date: 2000/02/02 Raw View
I was experimenting with assignement operators and boolean operators and
wrote the following code:
#include <iostream>
using namespace std;
int main() {
bool v = true;
v ^= 2;
cout << (v ? "true" : "false") << endl;
bool w = true;
w = w ^ bool(2);
cout << (w ? "true" : "false") << endl;
}
I compiled it both with egcs-1.1.2-12 (egcs-2.91.66) and VC++ 5. In
both cases the result of execution was:
true
false
I thought it should have been:
false
false
According to 5.17 clause 3, the expression "2" in "v ^= 2;" is
"implicitly converted (clause 4) to the cv-unqualified type of the left
operand". According to 4.7 clause 4, "if the destination type is bool,
see 4.12", which says, "an rvalue of [several types] can be converted to
an rvalue of type bool. A zero value [...] is converted to false; any
other value is converted to true." Hence, the expression "2" should be
converted to true. According to 5.17 clause 7, "the behaviour of an
expression of the form E1 op= E2 is equivalent to E1 = E1 op E2 except
that E1 is evaluated only once." The result of "v ^= 2;" should then be
the same as obtained by the expression "v = v ^ bool(2);", i.e., "v = v
^ true", if I understood the standard right. Acording to 5.12, operands
E1 and E2 should then undergo the usual arithmetic conversions in 5
clause 9, which states "[...] - otherwise, the integral promotions (4.5)
shall be performed on both operands." According to 4.5 clause 4, "an
rvalue of type bool can be converted to an rvalue of type int, with
false becoming zero and true becoming one." Hence, the result of "v ^
true" should be the same result of "1 ^ 1", i.e., zero. Converted back
to bool, the result in "v" should be false.
I am assuming that the E2 in 5.17 clause 7 refers to the second operand
but after the implicit conversion mentioned in clause 3, though this is
not very clear in the text of the standard.
Manuel Menezes de Sequeira
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 2000/02/02 Raw View
Manuel Menezes de Sequeira wrote:
> v ^= 2;
Will always be true.
> w = w ^ bool(2);
>
Ah...here is your misconception. THat is *NOT* equivelent to the
statement above. bool(2) yelds true and the XOR works. However
The first statement is equiv to
v = v ^ 2;
Since the operands of ^ are not the same type, the bool gets promoted
to int yielding:
v = 1 ^ 2
which evaluates to
v = 3
which is converted back to bool as true.
---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]