P: n/a

Will the following print "NOT equal" on all implementations:
#include <iostream>
signed main()
{
bool k;
k = 5;
std::cout << (k == 5 ? "equal" : "NOT equal");
}
Let's say that a bool is 8Bit on a particular implementation. When you
assign 5 to a bool, does the bool's bit pattern represent:
A) The integer 1
B) The integer 5
C) Implementationdefined ?
Are the following statments equivalent at all times:
k = true;
++k;
k = false;
k;
Even if k had initially been set to 5?
JKop  
Share this Question
P: n/a

Will the following print
NOT equal
true
true
on all implementations?
#include <iostream>
signed main()
{
bool k;
k = 5;
std::cout << (k == 5 ? "equal" : "NOT equal") << '\n';
k = 2;
std::cout << ( k ? "true" : "false") << '\n';
k = 3;
std::cout << ( k ? "true" : "false");
}
What is the effect of k =2 ?
JKop  
P: n/a

JKop wrote: Will the following print "NOT equal" on all implementations:
#include <iostream>
signed main() { bool k;
k = 5;
std::cout << (k == 5 ? "equal" : "NOT equal"); }
Let's say that a bool is 8Bit on a particular implementation. When you assign 5 to a bool, does the bool's bit pattern represent:
A) The integer 1
Maybe.
B) The integer 5
Maybe.
C) Implementationdefined ?
Yes.
Are the following statments equivalent at all times:
k = true; ++k;
k = false; k;
They are never equivalent. The first one sets k to false, the second one
sets it to true.
Even if k had initially been set to 5?
A bool only knows two values, true and false. If you assign an integer
to it, that integer gets converted to bool first (with 0 resulting in
true, any other value in false), and then k gets set to that value.  
P: n/a

"JKop" <NU**@NULL.NULL> wrote in message news:Ps******************@news.indigo.ie... bool k;
k = 5;
This sets k to "true"
std::cout << (k == 5 ? "equal" : "NOT equal");
The value of k is promoted to int here. It's value will be 1
if k had been true before. Let's say that a bool is 8Bit on a particular implementation. When you assign 5 to a bool, does the bool's bit pattern represent:\
It represents a value that would be true. It's up to the implementation to figure
out how to encode it. However. All bools with true values compare equal.
All bool true values convert to 1 when converted to an integer.
So, while it is perfectly acceptable for the implementation to store a representation
of 5 in the bool, it will be hard for you to tell, as
static_cast<int>(k)
will always be 1.  
P: n/a

"JKop" <NU**@NULL.NULL> wrote in message news:hy******************@news.indigo.ie... k = 2;
The semantics of this is:
k = k  2;
k  2 is (1  2) if k was previously true or (0  2) if k was previously false.
In either case, the result is non zero, and the store back to k results in a
true value.  
P: n/a

Rolf Magnus posted: A bool only knows two values, true and false. If you
assign an integer to it, that integer gets converted to bool first (with 0
resulting in true, any other value in false), and then k gets set to
that value.
I'm assuming the above is a typo, and that you meant:
(with 0 resulting in false, any other value in true)
JKop  
P: n/a

"Rolf Magnus" <ra******@tonline.de> wrote in message news:cf*************@news.tonline.com... Are the following statments equivalent at all times:
k = true; ++k;
k = false; k;
They are never equivalent. The first one sets k to false, the second one sets it to true.
The first one is a deprecated unconditional set to true.
The second one is illegal.  
P: n/a

Ron Natalie posted: "JKop" <NU**@NULL.NULL> wrote in message news:Ps******************@news.indigo.ie...
bool k;
k = 5; This sets k to "true"
std::cout << (k == 5 ? "equal" : "NOT equal");
The value of k is promoted to int here. It's value
will be 1 if k had been true before. Let's say that a bool is 8Bit on a particular
implementation. When you assign 5 to a bool, does the bool's bit pattern
represent:\ It represents a value that would be true. It's up to
the implementation to figure out how to encode it. However.
All bools with true values compare equal. All bool true values
convert to 1 when converted to an integer.
So, while it is perfectly acceptable for the
implementation to store a representation of 5 in the bool, it will be hard for you
to tell, as static_cast<int>(k) will always be 1.
So the following will print "EQUAL" on all implementations:
#include <iostream>
signed main()
{
bool a;
bool b;
a = 5;
b = 7;
if (a == b) std::cout << "EQUAL";
}
Right?
JKop  
P: n/a

JKop wrote: Will the following print
NOT equal true true
on all implementations?
#include <iostream>
signed main() { bool k;
k = 5;
Here, 5 is converted to bool with the value true, so k is set to true. std::cout << (k == 5 ? "equal" : "NOT equal") << '\n';
Here, k is converted to an int with the value 1 (false results in 0,
true in 1). That integer is then compared to the value 5. The result of
that comparison is of course false.
k = 2;
Here, k is again converted to 1, then 2 are subtracted from it,
resulting in 1. That is then converted back to bool and written to k.
The result is true, so k is still set to true.
std::cout << ( k ? "true" : "false") << '\n';
k = 3;
Same here as above, just the intermediate integer value is 2 instead of
1.
std::cout << ( k ? "true" : "false"); }
What is the effect of k =2 ?
It's the same as the effect of:
k = true;  
P: n/a

JKop wrote: Rolf Magnus posted:
A bool only knows two values, true and false. If you assign an integer to it, that integer gets converted to bool first (with 0 resulting in true, any other value in false), and then k gets set to that value.
I'm assuming the above is a typo, and that you meant:
(with 0 resulting in false, any other value in true)
Yes, of course. Sorry.  
P: n/a

JKop wrote: So the following will print "EQUAL" on all implementations:
#include <iostream>
signed main() { bool a; bool b;
a = 5; b = 7;
if (a == b) std::cout << "EQUAL"; }
Right?
Right.  
P: n/a

Ron Natalie posted: "JKop" <NU**@NULL.NULL> wrote in message news:hy******************@news.indigo.ie... k = 2;
The semantics of this is: k = k  2;
k  2 is (1  2) if k was previously true or (0  2) if k was previously false. In either case, the result is non zero, and the store back to k results in a true value.
How about :
#include <iostream>
signed main()
{
bool a = true;
a = 1;
std::cout << (a ? "true" : "false");
}
On my system it prints "false". It will print "false" on every
implementation, right? Here's the rationale:
a = 1;
a = a  1;
a = 1  1;
a = 0;
a = false;
JKop  
P: n/a

Ron Natalie wrote: "Rolf Magnus" <ra******@tonline.de> wrote in message news:cf*************@news.tonline.com... > Are the following statments equivalent at all times: > > k = true; > ++k; > > k = false; > k;
They are never equivalent. The first one sets k to false, the second one sets it to true.
The first one is a deprecated unconditional set to true. The second one is illegal.
Hmm. I was assuming that the first one would convert k to the integer
value 1 and then increment that. Don't ask me how I got the idea that
it will set k to false then... should actually be true. Anyway, in this
case it would indeed be equivalent to unconditionally setting it to
true. Why is that deprecated?
And why is the second one illegal? I would have assumed that k would
just invert the value:
false > 0 > 1 > true
true > 1 > 0 > false  
P: n/a

"JKop" <NU**@NULL.NULL> wrote in message news:PspRc.24584 Will the following print "NOT equal" on all implementations:
#include <iostream>
signed main() { bool k;
k = 5;
std::cout << (k == 5 ? "equal" : "NOT equal"); }
Yes. It is similar to the question I asked recently on this newsgroup.
Search for subject "Does true ^ true return false"  though in that thread
there was an alternate solution, which I actually not ended up using in my
actual code as it didn't make the intent look clear to me.
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  
P: n/a

JKop wrote: Will the following print "NOT equal" on all implementations:
#include <iostream>
signed main() { bool k;
k = 5;
std::cout << (k == 5 ? "equal" : "NOT equal"); }
In the above, you are converting integral type into a boolean type.
Focus on staying in the same domains, and don't cross them unless
necessary.
int main(void)
{
bool result = false;
int k = 5;
result = k == 8;
result = result && k == 5;
std::cout << "Result is " << result << endl;
return EXIT_SUCCESS;
}
In my experience, converting between bool and numerics is only
needed for storing objects and maybe accessing hardware. When
optimizing for space, boolean variables may be converted to
bits. Interchanging bools and integers is a dangerous
practice.
Let's say that a bool is 8Bit on a particular implementation. When you assign 5 to a bool, does the bool's bit pattern represent:
A) The integer 1
B) The integer 5
C) Implementationdefined ?
Why do we care?
If we use a bool variable for boolean results, we are not
concerned about its conversion to an integer and its integral
value.
We can easily convert boolean variables to bits. Heck, if
your going to convert from bool to integer, why not just
pack it and convert bool to bits? Why waste the extra
bits?
Are the following statments equivalent at all times:
k = true; ++k;
k = false; k;
Even if k had initially been set to 5?
JKop
I wouldn't mix bools with integers, floats, or doubles.
Often times, mixing makes no sense and just makes the
program needlessly complex and difficult to maintain.
The Dogma:
1. Keep it simple, easy to read, understand and maintain.
2. Don't optimize until the project is finished or
out of resources.
3. Just because you are able to do something in a language
doesn't mean you should do it.
4. Focus on how to get the task completed, not on
how many different ways the language can be used.
5. Test early, test often.

Thomas Matthews
C++ newsgroup welcome message: http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++faqlite
C Faq: http://www.eskimo.com/~scs/cfaq/top.html
alt.comp.lang.learn.cc++ faq: http://www.comeaucomputing.com/learn/faq/
Other sites: http://www.josuttis.com  C++ STL Library book   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 1579
 replies: 14
 date asked: Jul 22 '05
