Thread: Boolean operators

  1. #1
    Registered User Trogdor27's Avatar
    Join Date
    Sep 2005
    Location
    Toronto, ON, CA
    Posts
    5

    Boolean operators

    Hi, I'm trying to go through the C++ tutorials, but I keep getting stumped when i hit the Boolean stuff. Could somebody please explain it in an easier way? Thanks

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    If you narrow down your question, I'm sure your problems will be addressed more promptly and you will understand much better than if some kind person came along and wrote up a big page about boolean logic. Perhaps, you could also use the resources the internet provides to help you narrow down your question:

    http://computer.howstuffworks.com/boolean.htm
    http://www.intap.net/~drw/cpp/cpp03_04.htm
    http://www.cprogramming.com/tutorial/lesson2.html

    etc. etc. etc. google. google.

  3. #3
    Registered User Trogdor27's Avatar
    Join Date
    Sep 2005
    Location
    Toronto, ON, CA
    Posts
    5
    Ok, I'm working on the tutorial 2, and I sort of understand the NOT operator, but the AND and OR are just beyond me. It gives this example:

    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)

    For A, i think this means NOT 1. 0 is not 1 so this would be true but the answer is 0 meaning false.

    For starters, what am I not understanding in A?

  4. #4
    Registered Usurer
    Join Date
    Apr 2005
    Location
    upstate NY
    Posts
    79
    Holy deja vu, Batman! This was covered just a few posts ago:

    http://cboard.cprogramming.com/showthread.php?t=69525

    I guess you have !(been reading the posts);

    -JM

  5. #5
    Registered User Trogdor27's Avatar
    Join Date
    Sep 2005
    Location
    Toronto, ON, CA
    Posts
    5
    ok, i read that and it helps, but one question still:

    How does ( 1 || 0 ) equal to 1?
    It means 1 or 0. Does the OR operator only look for either of the values to be one and then it is true? Can the Or operator ever be looking for a zero? How about a seven?

    Edit: I think I understand now. I had been thinking of 0 and 1 as numbers in the questions. Instead I should be thinking of them as true and false. This makes a lot more sense and gets me the correct answers for the examples. Now I can finally get on to tutorial 3 and continue my path to world domination.
    Last edited by Trogdor27; 09-11-2005 at 01:15 PM.

  6. #6
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Trogdor27
    ok, i read that and it helps, but one question still:

    How does ( 1 || 0 ) equal to 1?
    It means 1 or 0. Does the OR operator only look for either of the values to be one and then it is true? Can the Or operator ever be looking for a zero? How about a seven?
    Yes, OR returns true (1) if either one are true (ie. if either values is true it will do the next section of code). No, OR wont return true if both are false (0), I think you might be confusing what exactly OR does. You use OR in combination to things like ==, !=, !, and those are what return 1 or 0: ie. if (someVariable == 0 || somevariable == 100) - lets say someVariable is equal to 100, so that statement becomes: (0 || 1), the right side is true, and therefor the if statement procedes. Of course there are some cases where what you said (if its 7) will occur, but its not extremely common: ie. if ( someFunction() || anotherFunction() ) - what this does is takes the value returned from each function, someFunction could return 7 and itd be considered true - (7 || 0) lets say, which is 1. True in C++ is clasified as non-zero, so 1, 7, 14, 1000 are all 'true' (anything not 0).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #7
    Registered User Trogdor27's Avatar
    Join Date
    Sep 2005
    Location
    Toronto, ON, CA
    Posts
    5
    Excellent, thank you for clarifying. Its all coming together now.

  8. #8
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    You might be thinking of these as logical statements. Also think of the logical operators as mathematical functions. The ! operator is simply defined by f(x) = 0 if x != 0, 1 if x = 0.

    The || operator is simply defined by f(x,y) = 0 if (x,y) = (0,0), 1 otherwise.

    The && operator is simply defined by f(x,y) = 0 if x = 0, 0 if y = 0, 1 otherwise.

    (Assume that true and false are stand-ins for 1 and 0.)

    The || and && operators will stop after evaluating their left hand side if they can already deduce the answer, so sometimes, the right side does not get evaluated. It's good to know this.

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Trogdor27, I noticed you use your text gun as a sig, and yet it's not. If you made it a sig you wouldn't have to add it to each post.

  10. #10
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Cool-August
    Trogdor27, I noticed you use your text gun as a sig, and yet it's not. If you made it a sig you wouldn't have to add it to each post.
    Hmm?

    Quoting him doesnt seem to catch his gun sig.. doesnt seem like hes adding it manually. (nor is it included in the quotes below this text box)
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #11
    Registered User Trogdor27's Avatar
    Join Date
    Sep 2005
    Location
    Toronto, ON, CA
    Posts
    5
    its a sig

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boolean operators
    By forkpie hat in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2008, 03:35 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 07:42 PM
  3. C++ and Boolean Operators
    By Rockskin in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2006, 03:45 PM
  4. boolean operators
    By bj31t in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2004, 08:34 PM
  5. Boolean Operators?
    By civilwarsearch in forum C++ Programming
    Replies: 11
    Last Post: 12-15-2003, 09:50 PM