bools...

This is a discussion on bools... within the C++ Programming forums, part of the General Programming Boards category; can some1 please tell me what bools are, why they are used, and how do u use them......

  1. #1
    Registered User Finchie_88's Avatar
    Join Date
    Aug 2004
    Posts
    154

    Question bools...

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

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb 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.
    Last edited by DougDbug; 10-04-2004 at 05:12 PM.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,805
    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).
    Woop?

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    13
    very helpful for a beginner like me. thanks you all.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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...

    what about
    bool x = (bool)5; ?

  7. #7
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    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

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    While legal, 'bool b = 42', etc. is an abuse of the abstraction and best avoided.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    > While legal, 'bool b = 42', etc. is an abuse of the abstraction and best avoided.
    Agreed.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    ^ 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
    }
    instead of

    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. #13
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    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;

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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".
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How get bool's from std::vector<bool>
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2008, 05:24 AM
  2. counting true's and false's from bools
    By quiet_forever in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2007, 02:57 PM
  3. WM_CREATE and BOOLs
    By kidburla in forum Windows Programming
    Replies: 3
    Last Post: 09-18-2006, 11:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21