Thread: Evaluating Operators

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    2

    Evaluating Operators

    Hi, I am new to the message board here, and brand new to C++. I'm trying to learn how to do this on my own, to better my life. Anyhow, here is my question:

    the statement !(1 && !(0 || 1)) is a true statement.

    Why?

    Could someone explain this statement to me and how its evaluated? Thanks in advance!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    What do you know about these operators? How would you go about solving it?

    Do you know that in this case 1 is true and 0 is false?

    Do you know what order of operations means?

    Do you know what 1 || 0 evaluates to? How about 1 && 0? How about !1?

    Do you know how parentheses affect the order of evaluation?

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Start with what's in the innermost set of parenthesis (the OR):
    !(1 && !(0 || 1)) = !(1 && !(1))

    Next, evaluate the NOT(!):
    !(1 && !(1)) = !(1 && 0)

    Next evaluate the AND:
    !(1 && 0) = !(0)

    Last, evaluate the NOT(!):
    !(0) = 1
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Remember that && and || use short-circuit evaluation. The RHS might not even be evaluated.

    If you had !(0 && !(0 || 1)) for example, the red part of the expression would not even be evaluated ( despite the extra () ). The LHS side of the && is false, therefore the whole thing is false regardless of what the RHS is.

    This is to allow you to test and dereference a pointer for example, with complete safety.
    char *p;
    if ( p && *p ) doStuff(*p);

    First checks that the pointer is not NULL, then checks that the char being pointed at isn't zero.

    Boolean expressions are read L->R, not starting with the most deeply nested ( ) sub-expression.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    2
    Quote Originally Posted by hk_mp5kpdw View Post
    Start with what's in the innermost set of parenthesis (the OR):
    !(1 && !(0 || 1)) = !(1 && !(1))

    Next, evaluate the NOT(!):
    !(1 && !(1)) = !(1 && 0)

    Next evaluate the AND:
    !(1 && 0) = !(0)

    Last, evaluate the NOT(!):
    !(0) = 1





    Thank you for your help hk. Very useful explanation, it helped A LOT.

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Well, Salem, that allows me to simplify a great deal of my code.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. evaluating statements with bitwise operators
    By tbabula in forum C Programming
    Replies: 1
    Last Post: 06-25-2008, 07:49 AM
  3. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  4. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 04:37 AM
  5. Operators
    By George in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2003, 07:35 PM