Thread: The boolean operator NOT(!)

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    5

    The boolean operator NOT(!)

    Hi, I'm going through the C-tutorial(beginner) and I want to know if I have understand it right.

    Code:
    A. !( 1 || 0 )         ANSWER: 0
    Isnt really hard. (1 OR 0 is true) = false in this case.

    Code:
    B. !( 1 || 1 && 0 )    ANSWER: 0 (AND is evaluated before OR)
    C. !( ( 1 || 0 ) && 0 )  ANSWER: 1 (Parenthesis are useful)
    These two I dont understand so well.

    Code:
    B. !( 1 || 1 && 0 )
    && are operated before || so 1 && 0 is false because both numbers must be true. But what happens after that? Now we have

    1 || FALSE and FALSE replaces 1 && 0. Could someone explain this? I was thinking if it would look like this after the &&-operation is done:

    1 || 0 - the zero replaces the 1 && 0 wich was false. Am I thinking right?

    And the last one:
    C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)

    the 1 || 0 is operated and is true(1) Then 1 && 0 is operated wich is false. The NOT-operator inverts this to true.
    Last edited by Aphex; 08-10-2010 at 05:25 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    It's easier to do step by step. These are all equal, see if you can understand these steps:
    Code:
    !( 1 || 1 && 0 )
    !( 1 || (1 && 0) )
    !( 1 || 0 )
    !1
    0
    If you can, try it yourself with the other example.

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Don't forget that they are short-circuit operators.
    So

    !( 1 || 1 && 0 )
    !( 1 || (1 && 0) )
    ! (1) // no need to evaluate (1 && 0) since 1 || anything is 1 !

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    || if either is, or both are, !0, the result is 1, otherwise 0
    && iff both are !0, the result is 1, otherwise 0

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    5
    Thanks people, Ima write some programs and combine them in loops and stuff so I don't forget them...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with boolean algebra!!!
    By blakjakd in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2010, 10:03 AM
  2. Novice needs help
    By ghaasemi in forum C++ Programming
    Replies: 9
    Last Post: 05-30-2009, 08:20 AM
  3. Replies: 4
    Last Post: 04-02-2004, 07:30 PM
  4. Working with boolean...
    By CompiledMonkey in forum C Programming
    Replies: 4
    Last Post: 11-03-2003, 10:39 AM
  5. Use struct to create a boolean type
    By skyglin in forum C Programming
    Replies: 6
    Last Post: 06-18-2003, 08:21 PM