Thread: booleans

  1. #1
    Unregistered
    Guest

    booleans

    Im having trouble understanding AND OR and NOT
    can someone clarify these.

  2. #2
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Those aren't bools.. they are logical operators.

    one and a one is a one.

    one and a zero is a zero.

    zero and a one is a zero.

    zero and a zero is a zero.


    one or a one is a one.

    one or a zero is a one.

    zero or a one is a one.

    zero or a zero is a zero.


    Not one is zero.

    Not zero is one.


    1 AND 1 = 1
    1 AND 0 = 0
    0 AND 1 = 0
    0 AND 0 = 0

    1 OR 1 = 1
    1 OR 0 = 1
    0 OR 1 = 1
    0 OR 0 = 0

    NOT 1 = 0
    NOT 0 = 1


    There really is nothing to understand. It is one of those 'I believe' things. Just remember any zero in an AND equation makes the equation zero. Any one in an OR equation makes the equation one. A NOT of a one or zero is it's opposite.

    Bool...

    TRUE

    FALSE

    where

    TRUE = 1

    FALSE = 0

    so

    TRUE AND FALSE = FALSE

    TRUE or FALSE = TRUE

    NOT(TRUE) = FALSE

    etc
    Blue

  3. #3
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    cant put it any other way...
    i suggest tho, when you are using your logical operators, make sure you understand your condition.

    note:

    if (!(mybool))
    /....

    and

    if(mybool == false)
    /...

    the second one is easier to read. if you are beginning you should stick to readablility.

  4. #4
    Seņor Member
    Join Date
    Jan 2002
    Posts
    560
    Just look at it like this.

    If I declare bool poop=true;
    then if(poop) means if(poop==true)
    if (!poop) means if (poop==false)
    and also, false is a 0 and true is a 1.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    TRUE is not always 1 and FALSE is not always 0!

    In electronics it is sometimes defined that TRUE is 0 and FALSE is 1. A zero-signal means that everything is OK. But when a sensor notices a failure it will send a pulse, which is a one-signal. So in that case FALSE is 1 and TRUE is 0. So to handle the failure one would do:

    if (error)
    {
    // handle error
    }

    But also in software TRUE is not always 1 and FALSE not always 0.

  6. #6
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    >>TRUE is not always 1 and FALSE is not always 0!

    That's true...
    -

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Originally posted by Shiro
    TRUE is not always 1 and FALSE is not always 0!
    Isn't FALSE always 0? I thought FALSE = 0 and TRUE = non-zero
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Isn't FALSE always 0? I thought FALSE = 0 and TRUE = non-zero
    Generally, but the actual values of TRUE and FALSE are up to the implementation. If you want TRUE to be 20 and FALSE to be -4756 then you can do so and anyone who uses your code will have to accept that.

    -Prelude
    My best code is written with the delete key.

  9. #9
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    Well when you are using an anal compiler you may have a backwards value list.

    but when you are in a program environment, the compiler takes a non zero as true and a 0 as false, but on the other hand, any returns to the OS', the operating systems process index, will look at the return value as 0 successful and 1 not successful. so when returning from a function and returning from main , there is a difference... else your compiler is almost guarenteed to be true non zero and false 0. because almost all hardware (yes almost all) define, 0 as off and 1 as on. if its the other way around, then its outta sync with the standard.

    and besides, if you using your booleans, then a true is a true no matter what... so test for a true and not a number value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob getting confused by booleans
    By Nathan the noob in forum C++ Programming
    Replies: 7
    Last Post: 06-27-2008, 03:50 PM
  2. 2d array of booleans
    By eklavya8 in forum C++ Programming
    Replies: 9
    Last Post: 06-27-2008, 02:36 PM
  3. Array of Booleans Represented with 1 Bit
    By Brad0407 in forum C++ Programming
    Replies: 43
    Last Post: 07-08-2007, 01:23 PM
  4. Help plz, Booleans
    By tagman in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2005, 08:30 AM
  5. Booleans in C
    By KneeLess in forum C Programming
    Replies: 6
    Last Post: 09-09-2004, 09:47 AM