# bools...

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-04-2004
Finchie_88
bools...
can some1 please tell me what bools are, why they are used, and how do u use them...
:)
• 10-04-2004
master5001
Its just a boolean value. It can have a value of "true" or "false." They are nice since they make code a little more understandable to everyone.

Example:

Code:

```bool done = false; while(!done) {   if(doSomething() == true)     done = true; }```
• 10-04-2004
DougDbug
boolean = binary = 2-state logic.
The if-statement tutorial has some good information about the use of true & false. (But I didn't see any bool type variables used there.)

bools are used to store two-state variables...

bool x = true; // OK
bool x = 5; // ERROR!

bool SwitchOn = true; // true if on, false if off
bool Male = true; // true if male, false if female
bool Cat = false; // true if cat, false if not a cat

if(Cat && !Male) // If it's a female cat...

Boolean logic is binary (2-state) logic. It uses AND, OR, EXCLUSIVE OR, and NOT (invert).

false is stored in memory as a zero, and true is stored in memory as a one.

When you use integers with boolean logic, zero is always false, and non-zero is always true. In an if-statement, (2 + 2) is true, and (2 - 2) is false.
• 10-04-2004
prog-bman
What they are has been explained enough. I personally use them to control while loops. I also use them on select functions, well currently for a collision detection function(return true on a hit false on not hitting).
• 10-04-2004
cpp!n
very helpful for a beginner like me. thanks you all.
• 10-04-2004
misplaced
i'm not trying to argue....but are you sure bool x = 5 is illegal? because the value 5 returns true...i know there's very few times you would even think about doing that, but it may be reasonable in some cases...
i have never tried though...

bool x = (bool)5; ?
• 10-04-2004
Perspective
Quote:

Originally Posted by misplaced
i'm not trying to argue....but are you sure bool x = 5 is illegal?

its not illegal, but in general a bool should be used as a bool, using an int to flag zero vs non-zero boolean logic is more intuitive IMO.

ex (g++, Linux 2.6 Kernel, x86)
Code:

```bool b = 2345; cout << "BOOL: " << b << '\n' << "INT : " << (int)b << endl; output: BOOL: 1 INT : 1```
• 10-04-2004
master5001
Actually, bools are usually a byte in size. Thus bool x = 5 is legal. bool b = 2345 wouldn't be legal as it is too large. However, the extra bits will be truncated thus its not illegal to say 2345, it just would only output 41 as its value.

P.S. cout will treat a bool as a bool...thus it will only have a set and unset value. Try doing the same but typecast b as an int.
• 10-04-2004
Zach L.
While legal, 'bool b = 42', etc. is an abuse of the abstraction and best avoided.
• 10-05-2004
master5001
> While legal, 'bool b = 42', etc. is an abuse of the abstraction and best avoided.
Agreed.
• 10-05-2004
misplaced
i'm not saying i want to do that....but i think in some program somewhere you might want to test a certain values state without directly using the variable...for instance, repeated referencing of arrays. say you had an array of int's and you were going to reference element[x] a repeated number of times within a loop. for optimization you would do something like int current = element[x]. well, if you only want to know the elements state, you might as well do bool currentElementsState = element[x]
• 10-05-2004
master5001
^ random code.

As far as giving a bool a value other than true or false, I would also point out that you would need to test a bool for a value rather than comparing it to true.

Example:
Code:

``` bool x = someFunction(); // for the sake of argument, lets say some function returned 5 if(x) {   // do stuff }```

Code:

``` bool x = someFunction(); // for the sake of argument, lets say some function returned 5 if(x == true) {   // do stuff }```
Which how I handle bools anyway, but others may prefer a more direct comparison. So again, one should just play by the rules even though they don't have to.
• 10-05-2004
Perspective
Quote:

Originally Posted by master5001
As far as giving a bool a value other than true or false, I would also point out that you would need to test a bool for a value rather than comparing it to true.

this is inncorrect. If you assign a bool an integer value, you can compare it to "true" successfully. just try it :)

Code:

```bool b = 2345; if( b == true )   cout << "b is true!!!" << endl;```
• 10-05-2004
Hunter2
I believe, although bool's are physically a whole byte (hard not to be), they only ever hold 1 or zero - and if you attempt to store another value in it, it will automatically be converted to either true or false according to 'if' logic. Perspective's test output backs this up:
Quote:

output:
BOOL: 1
INT : 1
Although, as Prelude once said, "my compiler does this" is not the same as "the standard says this".
• 10-05-2004
quzah
Theoreticly, "true" could be defined as "!0". Thus, if it was simply treated as an integer, the truth test could equal "true". I don't know if the C++ standard defines "true" as "must be one" or not. However in a "boolean test", anything which isn't false is considered true.

Quzah.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last