am i doing it wrong? !, ||, &&

This is a discussion on am i doing it wrong? !, ||, && within the C Programming forums, part of the General Programming Boards category; this is a q taken from the if statement quizz Code: 3. Evaluate !(1 && !(0 || 1)). A. True ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3

    Talking am i doing it wrong? !, ||, &&

    this is a q taken from the if statement quizz
    Code:
    3. Evaluate !(1 && !(0 || 1)).
    A. True
    B. False
    C. Unevaluatable
    http://www.cprogramming.com/tutorial...z/answer2.html
    it says true, well, i answered it and its "B" false, am i doing it wrong? somebody explain how did he solve it.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    You have to work from the inside out.
    !(1 && !(0 || 1))
    Start here. This is false because 0 OR 1 is true, and NOT inverts the answer.
    So now we have:
    !(1 && (0))
    1 AND 0 is false, but the whole expression is true because of NOT.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    then, i am right, and the quizz writer is wrong!?
    cprogramming.com needs to fix this.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    Read again. I screwed up at first. I hate having to check these things for people, when they should just learn operator precedence.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,046
    What does operator precedence have to do with this? It's all parentheses.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    > What does operator precedence have to do with this?
    Everything.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by rubso
    somebody explain how did he solve it.
    Rehash, in code.
    Code:
    #include <stdio.h>
    
    #define WYSIWYG(x) #x " = %d\n", x
    
    int main ()
    {
       printf ( WYSIWYG(          0 || 1  ) );
       printf ( WYSIWYG(        !(0 || 1) ) );
       printf ( WYSIWYG(   1 && !(0 || 1) ) );
       printf ( WYSIWYG( !(1 && !(0 || 1) ) ) );
       return 0;
    }
    
    /* my output
    0 || 1 = 1
    !(0 || 1) = 0
    1 && !(0 || 1) = 0
    !(1 && !(0 || 1) ) = 1
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    2,046
    Quote Originally Posted by citizen
    > What does operator precedence have to do with this?
    Everything.
    Look at it again: !(1 && !(0 || 1)).

    There is no need to know opperater precedence to figure this out. All you need to know is that you do what's in parentheses first.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    You can say that because you have the benefit of knowing what to evaluate first and how. As I said, precedence has everything to do with it. The predicate of the AND does take some work for the new or confuzzled.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by King Mir
    There is no need to know opperater precedence to figure this out. All you need to know is that you do what's in parentheses first.
    How do you know that parentheses go first? Don't just snap back "because", actually think why you know that. Now you'll see Citizen's point.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    3
    Quote Originally Posted by Dave_Sinkula
    Rehash, in code.
    Code:
    #include <stdio.h>
    
    #define WYSIWYG(x) #x " = %d\n", x
    
    int main ()
    {
       printf ( WYSIWYG(          0 || 1  ) );
       printf ( WYSIWYG(        !(0 || 1) ) );
       printf ( WYSIWYG(   1 && !(0 || 1) ) );
       printf ( WYSIWYG( !(1 && !(0 || 1) ) ) );
       return 0;
    }
    
    /* my output
    0 || 1 = 1
    !(0 || 1) = 0
    1 && !(0 || 1) = 0
    !(1 && !(0 || 1) ) = 1
    */
    Thank you, i got it now, 1 || 0 = 1 , 0 || 1 = 1

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    2,046
    Quote Originally Posted by citizen
    You can say that because you have the benefit of knowing what to evaluate first and how. As I said, precedence has everything to do with it. The predicate of the AND does take some work for the new or confuzzled.
    Quote Originally Posted by quzah
    How do you know that parentheses go first? Don't just snap back "because", actually think why you know that. Now you'll see Citizen's point.


    Quzah.
    Which parenthasis can you do first?
    !(1 && !(0 || 1)).

    You can't fo the outer ones, because you haven't done the inside parenthasis, so you don't know there value. Similarly it is true that && has higher precidence than ||, but it wouldn't make a difference given this code.

    What other way is there to solve this?

    No offence ment to rubso. Boolien logic may take a little thought, and you need to watch out for careless mistakes. You probably missed a step somewhere along the way.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  13. #13
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Parentheses and precedence are not the only things you have to worry about.
    Boolean operators use short-circuit evaluation.

    If you had
    !(0 && !(0 || 1))

    Then the right-hand side of the && expression would NOT be evaluated at all, regardless of how many ( ) you have there.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by King Mir
    Which parenthasis can you do first?
    You have again missed the point. The reason you know which one to do first is because of operator precedence. Just like in math in school. They teach you what to do first. That is operator precedence! That's the whole point. That's why it has "Everything" to do with it.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,046
    Quote Originally Posted by Salem
    Parentheses and precedence are not the only things you have to worry about.
    Boolean operators use short-circuit evaluation.

    If you had
    !(0 && !(0 || 1))

    Then the right-hand side of the && expression would NOT be evaluated at all, regardless of how many ( ) you have there.
    That's true, but it would have no impact on the reslut anyway. If there were a function call there you would be on to something, but there isn't.

    @Quzah just show me an alternate logical way to interpret that code, that doesn't totally ignore the parenthesis.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

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

Similar Threads

  1. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 09:44 PM
  2. !(1 && !(0 || 1)) Help
    By scope in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 10:19 PM
  3. Evaluate !(1 && !(0 || 1)).
    By Grumpy_Old_Man in forum C++ Programming
    Replies: 7
    Last Post: 08-18-2003, 01:28 PM
  4. Shorter notation?
    By GaPe in forum C Programming
    Replies: 2
    Last Post: 06-14-2003, 11:35 AM
  5. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM

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