FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Is true bool always converted to 1?





Peterssidan
Hello! Smile I want to know if it's guaranteed that a bool that is true always converts into 1 when cast to an int, in C++.
Example:
Code:
   int i = 20;
   bool b = i;
   cout << i + b << endl;

So can I be sure that the result always will be 21 in this case regardless which compiler?

I'm a bit worried that b might actually get the value 20. Should I stop worrying?
Fire Boar
Boolean is always stored as a single bit. 0 is off, 1 is on. When typecasted to any integer... let's take char as an example. It just adds zeros until it's the right length. So bool "true" to char is 1, plus 0000000 as padding making 0000001. In decimal, that's 1.

So... yes. Bool true does cast to 1, always.
AftershockVibe
I'm afraid fireboar is not entirely correct. Boolean values on any language are stored as a word (32 bits on your average 32bit machine) because that is the smallest unit the processor can work with.

While in C++ the standard is to convert true to 1 (although I'm not sure if it's de facto or specified), this is not the only value which represents true. Any value other than 0 can represent true.

In C (from which C++ is obviously derived), true was often represented as -1 as well (NOT operator on integer 0 gives -1 in twos complement notation).

In short, you're probably safe using it unless you have some bizarre or ancient compiler. You could always cast it yourself to make sure. Doesn't that code give you a warning anyway? If not, you're almost certainly safe.
Cool
ThePolemistis
Peterssidan wrote:
Hello! Smile I want to know if it's guaranteed that a bool that is true always converts into 1 when cast to an int, in C++.
Example:
Code:
   int i = 20;
   bool b = i;
   cout << i + b << endl;

So can I be sure that the result always will be 21 in this case regardless which compiler?

I'm a bit worried that b might actually get the value 20. Should I stop worrying?


Basically 0 will always be false.
Anything else is true.
jeremyp
Peterssidan wrote:
Hello! Smile I want to know if it's guaranteed that a bool that is true always converts into 1 when cast to an int, in C++.
Example:
Code:
   int i = 20;
   bool b = i;
   cout << i + b << endl;

So can I be sure that the result always will be 21 in this case regardless which compiler?

I'm a bit worried that b might actually get the value 20. Should I stop worrying?


You should worry because that's a really bad piece of code. Booleans are not integers and vice versa. You should not be trying to add the one to the other directly. The fact that you can is an unfortunate historical problem with the C-like languages (excepting modern ones like Java and C#).

Your whole question goes away if you use type correct operations e.g:

Code:
   int i = 20;
   bool b = i != 0;
   cout << i + (b ? 1 : 0) << endl;


The internal implementation of type bool is not important in the above code, it'll always work.
Related topics
Windows vs Mac
New Tool album on the way!
Do we need Religions?
How do you deal with a boyfriend's soon to be ex-wife?
DOES SEX CONSTITUTE LOVE?
The hardest thing
Best PhpBB 2.0.x template
World of War Craft, or Guildwars?
Is this site a scam?
Does God exist?
President Bush talks w/ Matt Lauer on Torture
some usefull PHP functions
What do you think about the violent games' influences on kid
WHy did Human being create GOD?
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.