Boolean Algebra

This is a discussion on Boolean Algebra within the C++ Programming forums, part of the General Programming Boards category; I am having real trouble understanding these NOT AND and OR Statement in lesson 2 of the C++ tutorial. Cprogramming.com ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    5

    Boolean Algebra

    I am having real trouble understanding these NOT AND and OR Statement in lesson 2 of the C++ tutorial. Cprogramming.com Tutorial: If Statements

    I wondering if the input is true why does it return false. does this mean if the input !1 is true then it returns 0? and vise versa? please someone help me with this because I'm about to start school to take my prerequisites in order for me to get into my major which is software engineering. I really want to get a big head start on the courses I'll be taking in about 1 1/2 years to 2 years.

    Also should I be starting with C++ having never done programming before or should I start with C?
    Last edited by Cyris; 06-07-2009 at 07:48 PM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Boolean algebra works on two values: true or false. For values that aren't true or false (like 3) C++ "converts" it to true or false using the basic idea that 0 is false and everything else is true.

    NOT says: take the boolean value and return the other boolean value. So true becomes false and false becomes true. Think about it in english: You are allowed to go see the movie. Vs: You are not allowed to go see the movie.

    AND asks if two values are both true. So TRUE/TRUE is TRUE but TRUE/FALSE, FALSE/TRUE, and FALSE/FALSE are all FALSE. Jack and Jill both start with y is false.

    OR asks if at least one of the two values are true. TRUE/TRUE, TRUE/FALSE, FALSE/TRUE are true but FALSE/FALSE is FALSE. This operator does lead to some confusion as English tends to imply that one of the values are true but the other is false (you can have the red pill or the blue pill). Logically this is actually an XOR (Exclusive OR) where only one of the values may be true. So TRUE/TRUE would be false.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,656
    In C++, logical operations yield a result of type bool, which has a value true or false. If that is then converted back to an integer, true gives a value 1 and false gives a value zero.

    In C++ (and C) a zero integer value corresponds to false and everything else is deemed true. That's the convention. So "if (23)" is functionally equivalent to the test "if (true)" or, more accurately, to the test "if (23 != 0)".

    The ! operator is the logical NOT (i.e. !x gives a true result if x is false, and a false value if x is true [non-zero]). So !1 will yield false (zero), as will !23, and !0 will yield true (1).

    The logical AND ("&&" or "and" in C++) takes two operands and returns a true result only if both operands are true. So (a && b) will yield false (zero) if either a or b are false, and true (1) otherwise.

    The logical OR (|| or "or" in C++) takes two operands and returns a true result if either of the operands are true. So (a || b) will only yield false if both a and b are false.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    5

    I think I understand

    let me got back and restudy that part of lesson 2 to see if I can figure out how they got the answer they got for the question in the quiz of that lesson.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    wait so the logical Not or "!" is looking for the input of zero from the if statement if the logical Not doesn't get zero from the if statement it returns a zero but if it gets zero it returns a 1. is this how that works? I understand OR and AND from what grumpy and Thantos posts but I'm still have trouble with NOT. but this is what is confusing me.

    A. !( 1 || 0 ) ANSWER: 0

    why is NOT 1 OR 0 = False when OR says its true if either of the operands are true or is the NOT 1 mean 0.

    so is the problem 0 || 0 = 0?

    also if anyone has this stuff master can you give me you instant messenger name or something? being able to ask someone who knows directly is much better. I wont be IMing you whenever I'm stuck because I like to figure things out but this one took me for a loop.
    Last edited by Cyris; 06-07-2009 at 08:42 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You've heard of order of operations, right? Parentheses first, then look at what's outside.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    man you just told me the same thing my sister said. so the reason

    C. !( ( 1 || 0 ) && 0 ) ANSWER: 1 (Parenthesis are useful)

    is because its 1 OR 0 evalutes to 1 then the outer parenthesis becomes !1 (which is really 0) !0 || 0 evaluates to 1 because both operands are NOT false so it gotta be true
    but would it be done like this?

    !( (1 OR 0 = 0) && 0 )
    !( 0 && 0 = 0) so the statement become !0 so its 1. Right?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1 or 0 is 1. But 1 && 0 is still 0, so from then on it's right.

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    5
    ok gotcha I typed that in wrong I was watching the Laker Magic Game lol Thank you to everyone who bothered to help me out.

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    1
    hey I'm new here, I just started learning C++ and just want to say how helpful this thread has been. I was completely lost on lesson two as well but reading these replies has really helped me, so thanks everyone!

  11. #11
    Registered User
    Join Date
    Dec 2010
    Posts
    16

    need help

    friends, I am new to C. here is problem, whose output I could not understand. could you please help me out?

    Code:
    main()
    {
       int i=4,j=-1,k=0,w,x,y,z;
    
       w=i||j||k;
       x=i&&j&&k;
       y=i||j&&k;
       z=i&&j||k;
    
    
       printf("\nw=%d x=%d y=%d z=%d\n",w,x,y,z);
    }
    the output is w=1 x=0 y=1 z=1

    I am confused why w=1 in the answer, when expression says w=4 or -1 or 0
    similarly others?

    thanks in advance

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    The logical operators OR and AND are short-circuiting. With OR that means stop when you find truth, and with AND it means stop when you find falsehood, because these results will end up being the results for the whole expression. If you evaluate any Boolean expression you will either find truth or falsehood.

    The result of a Boolean expression also belongs in a bool. That way you can do things like

    Code:
    #include <iomanip>
    #include <iostream>
    
    std::cout << std::boolalpha << whatdoesitmean << std::endl;
    and it makes a little more sense to the layperson.

  13. #13
    Registered User
    Join Date
    Dec 2010
    Posts
    25
    Truth table - Wikipedia, the free encyclopedia

    Read this and convert to C/C++ syntax...

    This part is most useful:
    http://en.wikipedia.org/wiki/Truth_t...ical_operators
    Last edited by tenchu; 12-03-2010 at 12:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boolean algebra....urgent help needed!
    By Saimadhav in forum C++ Programming
    Replies: 1
    Last Post: 07-28-2008, 08:22 AM
  2. Boolean Algebra
    By Epo in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2005, 05:10 PM
  3. Looking for a Boolean Algebra site
    By Majin_Flack38 in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-21-2002, 12:03 PM
  4. Replies: 4
    Last Post: 05-03-2002, 10:40 PM
  5. pop quiz on boolean algebra
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 09-27-2001, 08:11 AM

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