Boolean characters

This is a discussion on Boolean characters within the C++ Programming forums, part of the General Programming Boards category; hi I'm andrew. I'm new here, I've been reading the tutorials but I haven't gone past the second because the ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

    Boolean characters

    hi I'm andrew.
    I'm new here, I've been reading the tutorials but I haven't gone past the second because the answers to the questions arent obvious to me. I was wondering if somebody could explain to me the reason for the answers please.

    A. !( 1 || 0 ) ANSWER: 0
    B. !( 1 || 1 && 0 ) ANSWER: 0 (AND is evaluated before OR)
    C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    I'll start with the first one. Always start from the inside of parentheses. The || operator returns true (which is 1 in this case) if either side of the expression is true. So 1 || 0 evaluates to 1 because true OR false evaluates to true. That leaves !(1). The parentheses are not necessary there since there is a single value inside, so that leaves !1. The ! operator evaluates to the opposite of the expression. So !true is false and !false is true. In this case, that means that !1 is 0.

    Now how would you get the answer to the other two based on that and what you learned from the tutorial?

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)

    1 || 0 = 1 =0

    0 || 0 =1


    is this a correct disection of the problem? is 0 || 0 = 1?

    I really apreciate your help

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    0 || 0 = 0. English: false or false. Does false or false sound true to you? (Two wrongs . . . .)

    [edit] To solve it, follow Daved's advise.
    Always start from the inside of parentheses.
    !( ( 1 || 0 ) && 0 )

    Solve 1||0, and call that A.

    !( A && 0)

    Solve A&&0, and call that B.

    !B

    Then solve that . . .
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> 1 || 0 = 1 =0
    Ok, explain in words what you are thinking here.

    >> 0 || 0 =1
    Remember, operator || evaluates to 1 only if one side or the other side is 1. Also, where did the 0 || 0 come from in the problem? There is a big and important difference between || and &&.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    From the tutorial:
    NOT: The NOT operator accepts one input. If that input is TRUE, it returns FALSE, and if that input is FALSE, it returns TRUE. For example, NOT (1) evalutes to 0, and NOT (0) evalutes to 1. NOT (any number but zero) evaluates to 0. In C NOT is written as !. NOT is evaluated prior to both AND and OR.

    AND: This is another important command. AND returns TRUE if both inputs are TRUE (if 'this' AND 'that' are true). (1) AND (0) would evaluate to zero because one of the inputs is false (both must be TRUE for it to evaluate to TRUE). (1) AND (1) evaluates to 1. (any number but 0) AND (0) evaluates to 0. The AND operator is written && in C. Do not be confused by thinking it checks equality between numbers: it does not. Keep in mind that the AND operator is evaluated before the OR operator.

    OR: Very useful is the OR statement! If either (or both) of the two values it checks are TRUE then it returns TRUE. For example, (1) OR (0) evaluates to 1. (0) OR (0) evaluates to 0. The OR is written as || in C. Those are the pipe characters. On your keyboard, they may look like a stretched colon. On my computer the pipe shares its key with \. Keep in mind that OR will be evaluated after AND.
    That explains it pretty well. If you don't get it, try looking at some truth tables:
    Code:
      AND
    0 && 0 = 0
    0 && 1 = 0
    1 && 0 = 0
    1 && 1 = 1
    
      OR
    0 || 0 = 0
    0 || 1 = 1
    1 || 0 = 1
    1 || 1 = 1
    
      NOT
    !0 = 1
    !1 = 0
    If you still don't get it, say why.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    Quote Originally Posted by Daved
    >> 1 || 0 = 1 =0
    one or zero equals one and with the not (!) I musta change the answer to the oposite, that being 0
    Ok, explain in words what you are thinking here.

    >> 0 || 0 =1 correction 0 && 0 = 0 =1 because of !(first zero representing the first equation and the second representing the original && 0 and then the answer being changed by !)
    Remember, operator || evaluates to 1 only if one side or the other side is 1. Also, where did the 0 || 0 come from in the problem? There is a big and important difference between || and &&.



    Questiong: does the ! affect the answer of !( ( 1 || 0 ) && 0 ) ?

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    No. ( 1 || 0 ) is evaluated by itself first, an expression unto itself. The result of that is &&ed with 0. The result of that is inverted.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    4
    Quote Originally Posted by dwks
    No. ( 1 || 0 ) is evaluated by itself first, an expression unto itself. The result of that is &&ed with 0. The result of that is inverted.

    perfect!!!! I got it.

    Thank you guys and thank you for you patience.

    Hope I can return the favor!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Novice needs help
    By ghaasemi in forum C++ Programming
    Replies: 9
    Last Post: 05-30-2009, 08:20 AM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. How do you check how many characters a user has entered?
    By engstudent363 in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 06:05 AM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM

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