1. ## bools...

can some1 please tell me what bools are, why they are used, and how do u use them...

2. 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;
}```

3. ## 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.

4. 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).

5. very helpful for a beginner like me. thanks you all.

6. 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; ?

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

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

9. While legal, 'bool b = 42', etc. is an abuse of the abstraction and best avoided.

10. > While legal, 'bool b = 42', etc. is an abuse of the abstraction and best avoided.
Agreed.

11. 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]

12. ^ 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.

13. 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;```

14. 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:
output:
BOOL: 1
INT : 1
Although, as Prelude once said, "my compiler does this" is not the same as "the standard says this".

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