Newbie question !( 1 || 0 ) - ?

This is a discussion on Newbie question !( 1 || 0 ) - ? within the C++ Programming forums, part of the General Programming Boards category; I was going through Lesson 2 in the tutorials. At the end, it has some statements ~ Code: A. !( ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    Question Newbie question !( 1 || 0 ) - ?

    I was going through Lesson 2 in the tutorials. At the end, it has some statements ~
    Code:
    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)
    What does the ! do here?
    How does 1 && 0 evaluate to 0? (previous paragraph in tutorial)

    Thanks

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    ! means not

    for example:
    Code:
    a = 1;
    if( a != 3 )
    {
      cout << "blah Blah" << endl;
    }
    
    
    //OUTPUT:
    
    blah blah
    
    //because a  is not 3..

  3. #3
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    > How does 1 && 0 evaluate to 0?
    How can something be true and false?

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    Quote Originally Posted by SirCrono6
    > How does 1 && 0 evaluate to 0?
    How can something be true and false?

    - SirCrono6
    Maybe I'm reading into it too much..

    All I get from !(1 && 0) is "Not(True AND False)" I don't see how an answer can come from that

    I understand If statments quite well

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    AND requires both terms to be true for the result to be true, where OR only requires one term to be true for the result to be true. So, starting with the parentheses:

    (True AND False)

    you get:

    (false)

    because both terms aren't true. Then, NOT reverses the result, so

    !(false)

    gives you true.

    If the problem had OR instead:

    !(true OR false)

    since OR only requires one term to be true for the result to be true, you would get:

    !(true)

    which is false.
    Last edited by 7stud; 03-30-2005 at 05:25 PM.

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Don't read too much into the names given to the logical operators ('and', 'or' ... especially 'or'). The meanings of these words in English does not completely correspond to their logical meanings, just take them as definitions.

    p && q is true if and only if p is true and q is true
    p || q is false if and only if p is false and q is false
    !p is true if and only if p is false

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    Ok, that makes sense now.. Thanks for clarifying

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i'm sure you probably know, but i haven't seen anyone point this out, so i will. anything that evaluates to zero evaluates to false;
    anything that evaluates to anything that's NOT zero, evaluates to true.

    if you REALLY want to learn about logic operators, i suggest reading some ASM tutorials concerning bitwise operations.

    just a little example of logic operators in it's binary form
    Code:
    1010
    1000 &&
    ----------
    1000   = true (not zero)  //  !1000 = false
    
    1010
    0100 ||
    ---------
    1110  = true (not zero)  //  !1110 = false;
    
    
    0101
    0101 XOR //i forget the XOR operator in c/c++ (^ maybe?)
    ------------
    0000  = false (zero)    //  !0000 = true;
    
    for you specific question -
    
    0001
    0000 ||
    ------------
    0001 true  // !0001 = 0000 = false
    Last edited by misplaced; 03-30-2005 at 09:49 PM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    I dont think there is a XOR operator availible in c++..some people say its a flaw..hu knows..a quick and easy work around is:

    bool p, q;

    (p || q) && !(p && q)

    producing an xclusive OR..

    cheers

    Alex

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > I dont think there is a XOR operator availible in c++
    Of course there is, and it's ^
    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.

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    is that quite a recent addition to c++?

    becuase the book im reading says there isnt one..

    man..i feel dumb!

    thanx for the heads up!

    cheers
    Alex

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    > is that quite a recent addition to c++?
    No, it's there from original C, which means it's been there forever basically.
    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.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Of course what Salem is failing to state is that it's bitwise XOR

  14. #14
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    bitwise?

    Please explain further?

    cheers
    Alex

  15. #15
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by macman
    bitwise?

    Please explain further?

    cheers
    Alex
    Bitwise:

    00010001 XOR 00100001

    00010001
    00100001
    -------------
    00110000

    Bitwise means that the XOR operator takes a look at the individual bits. XOR, in this case 'Exclusive OR', means that only one can be true to evaluate to true.

    Does that help?

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

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. Newbie with Very Newbie Question
    By Jedi_Mediator in forum C++ Programming
    Replies: 18
    Last Post: 07-01-2008, 08:00 AM
  3. Quick Ques on String Manipulation
    By ckuttruff in forum C Programming
    Replies: 8
    Last Post: 06-22-2008, 09:32 PM
  4. newbie: array question :(
    By cstudent in forum C Programming
    Replies: 2
    Last Post: 04-09-2008, 06:46 AM
  5. Optimization of code
    By dpro in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2002, 03:01 PM

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