Thread: The 1 and 0.. True/false - CONFUSED!

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    4

    The 1 and 0.. True/false - CONFUSED!

    Hi all,

    So I've been learning for only a couple days, so please, be gentle.

    I'm up to chap 2 (tutorial on this site) and the true/false (0-1) and the NOT, AND, OR stuff.

    I'm going ok until I hit this spot and am quite confused as the tutorial didn't go into any detail about it (the code below). In english, can u please break down this so I can understand it.
    Code:
    !(1 || 0) ANSWER 0.
    So the above, I'm guessing here, but.. the ! is a NOT, so I am assuming that it is saying the 2 inside numbers are not equal? Or False? So would:
    Code:
    !(1 || 1) be 1?
    The question I have is what is the exact meaning of this code? Why wouldn't we just put 1 > 0.

    I'm sure is is a really stupid question, but I can't understand why this code is the way it is. In the tutorial (on this site by the way) it doesn't go into any specifics of why they are like this.

    And, just for kicks, can someone break down what this is?
    Code:
    !( ( 1 || 0 ) && 0 ) Answer 1
    I mean what the ????. The tutorial just jumped right into this. It didn't say where this would come in handy or in what context I would ever use this? Is it just an example or?

    Any help is muchly appreciated.

    Cheers,

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    When an integer is converted to bool, zero is converted to false and non-zero is converted to true. When a bool value is converted to int, false is converted to 0 and true is converted to 1.

    Now, the operands of the built-in operator|| are of type bool. Therefore, you can interpret (1 || 0) as (true || false), which evaluates to true. !true == false, so the expression !(1 || 0) evaluates to false, hence the 0.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    TRUE  and TRUE  = TRUE 
    TRUE  and FALSE = FALSE
    FALSE and TRUE  = FALSE
    FALSE and FALSE = FALSE
    
    ---
    
    TRUE  or TRUE  = TRUE 
    TRUE  or FALSE = TRUE
    FALSE or TRUE  = TRUE
    FALSE or FALSE = FALSE
    
    ---
    
    not TRUE  = FALSE
    not FALSE = TRUE
    Therefore:

    Code:
    !(1 || 0) ANSWER 0.
    NOT (1 OR 0) = 0
    NOT (1) = 0
    
    !(1 || 1)
    NOT (1 OR 1) = NOT (1) = 0
    
    !( ( 1 || 0 ) && 0 ) Answer 1
    NOT ( (1 OR 0) AND 0) = 1
    NOT ( (1) AND 0) = 1
    NOT (0) = 1
    Last edited by Matticus; 07-06-2011 at 10:57 PM. Reason: Corrected minor format issue

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    Thanks for that guys. I see what you are saying Matticus and thanks so much for the easy breakdown and I now see what you mean. (thanks to you to laser as I learned something from what you said too ).

    Can u guys tell me though, do u have an example of where this would run in a code (simple please). My mind works better when I see something in action rather than just meaningless code. Sorry if not, as I said, just learning this stuff.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Crazycanook
    Can u guys tell me though, do u have an example of where this would run in a code (simple please).
    Sure, e.g.,
    Code:
    #include <iostream>
    
    int main()
    {
        using namespace std;
        cout << !( ( 1 || 0 ) && 0 ) << endl;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    Thanks .

    So can u tell me why you use that exactly. Not the program, but the numbers and brackets and all of that.

    For what reason would u need to type that just to have it spit back a False. Isn't there an easier way to have it spit out a False/True

    I think once I can figure out why I can move on.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is just an academic exercise. In practice most of those integer/boolean literals would be variables or the return values of functions, and the expression could be the condition of an if statement or a loop, thus determining the flow of control.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    Thanks for all of your help!

  9. #9
    Registered User Timon's Avatar
    Join Date
    Jul 2011
    Location
    Bangalore
    Posts
    4
    !(1 || 0) ANSWER 0.

    Logical operators are usually used to apply on the result of the two or more conditional statements that returns either true or false.
    like if((a>b) ||(a>c)).

    Any non zero value for these operators will be considered as "true" (includes negative numbers also) and zero value will be considered as "false".

    value for the true is "1"
    value for the false is "0"

    1 || 1 = 1
    1 || 0 = 1
    0 || 1 = 1
    0 || 0 = 1

    !(1) = 0
    !(0) = 1

    first (1||0) will be computed the output will be 1 based on the above table

    !(1) will be computed the result of which will be 0

    Remeber any non zero value will be "true" and zero value will be false for the logical operators.

    Hope it has cleared your doubt.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Think of a true statement and a false statement.

    Eg.
    1. Cats are mammals (true)
    2. The moon is made out of cheese (false)

    Code:
    !(1 || 0)
    Means -
    NOT (cats are mammals OR the moon is made out of cheese)

    "cats are mammals OR the moon is made out of cheese" is true, because cats are mammals.
    NOT of the statement makes it false (by definition).

    Code:
    !( ( 1 || 0 ) && 0 )
    That's the same as

    NOT ( (cats are mammals OR the moon is made out of cheese) AND the moon is made out of cheese)
    (cats are mammals OR the moon is made out of cheese) is true, but that AND the moon is made out of cheese isn't. Because the moon isn't made out of cheese.
    NOT of that gives you true.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    This is probably a little too advanced for now (or maybe not), but you'll notice that in the second example, (1 || 0) && 0, it doesn't matter what the first part is, the answer has to be false.

    "xxx AND the moon is made out of cheese" is false no matter what xxx is.

    This leads to something called short circuit evaluation.

    Similarly

    "xxx OR cats are mammals" is true no matter what xxx is.

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by cyberfish View Post
    This is probably a little too advanced for now (or maybe not), but you'll notice that in the second example, (1 || 0) && 0, it doesn't matter what the first part is, the answer has to be false.

    "xxx AND the moon is made out of cheese" is false no matter what xxx is.

    This leads to something called short circuit evaluation.
    No, it doesn't. Conditions are tested in sequence, from left to right.
    So "The moon is made out of cheese AND xxx" will never look at xxx, because the first part is always false. But if the xxx is on the left side, it will be looked at, and it's "the moon is made out of cheese" that won't be looked at if xxx turns out to be false.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Ah yes I got the order wrong for C/C++. Thanks.

    I was thinking more in terms of logic minimization.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help.. true/false
    By salmansalman in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2008, 10:10 AM
  2. True or False
    By noob programmer in forum C Programming
    Replies: 9
    Last Post: 10-29-2008, 10:15 AM
  3. True / False
    By goran00 in forum C Programming
    Replies: 13
    Last Post: 03-14-2008, 03:26 PM
  4. Help with True, False, NOT, AND, OR...
    By cgsarebeast in forum C++ Programming
    Replies: 22
    Last Post: 05-09-2006, 07:59 PM
  5. 1 or 0 == true or false?
    By Discolemonade in forum C++ Programming
    Replies: 4
    Last Post: 08-14-2005, 04:08 PM