bool specifics

J

JKop

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 8-Bit 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) Implementation-defined ?


Are the following statments equivalent at all times:

k = true;
++k;

k = false;
--k;


Even if k had initially been set to 5?


-JKop
 
J

JKop

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
 
R

Rolf Magnus

JKop said:
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 8-Bit 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) Implementation-defined ?
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.
 
R

Ron Natalie

JKop said:
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 8-Bit 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.
 
R

Ron Natalie

JKop said:

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

JKop

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
 
R

Ron Natalie

Rolf Magnus said:
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.
 
J

JKop

Ron Natalie posted:
This sets k to "true"


The value of k is promoted to int here. It's value will be 1
if k had been true before.


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
 
R

Rolf Magnus

JKop said:
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.

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;
 
R

Rolf Magnus

JKop said:
Rolf Magnus posted:


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

Rolf Magnus

JKop said:
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.
 
J

JKop

Ron Natalie posted:
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
 
R

Rolf Magnus

Ron said:
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
 
S

Siemel Naran

JKop said:
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
 
T

Thomas Matthews

JKop said:
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 8-Bit 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) Implementation-defined ?

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++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

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,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top