Thread: Question about logical operators

  1. #1
    Registered User
    Join Date
    Jul 2002

    Question about logical operators

    the code is:
    i = 2; j = 1
    printf("%d", !!i + !j);
    (the answer comes out to be 1... i don't understand why [and i don't know what the "!!" means (only the "!")...

  2. #2
    Registered User
    Join Date
    Jul 2002
    What your code is saying is not(not)i + not j, which means i is a true statement, regardless of the integer you assigned and j is a false statement. So, in effect, your code is saying true + false. That will equal 1. If you change it to !i + !j, it will equal 0.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    !! means not not.

    x = 1;

    if( x ) //true
    if( !x ) //false
    if( !!x ) //true

    Recall that any non-zero value equates to true in a true/false test. Thus, not one means zero (or false), so not zero means true.

    Hope is the first step on the road to disappointment.

  4. #4
    Registered User CAP's Avatar
    Join Date
    May 2002
    Hmmm...two negatives equal a positive thing right...algebra strikes back. Then again it could mean other things but I dont' know how big Simpsons fans you people are so nevermind
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Newbie with C++ NOT, OR, AND operators question...
    By JiWiz in forum C++ Programming
    Replies: 4
    Last Post: 07-27-2008, 11:05 AM
  3. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Question about operators.
    By Liger86 in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2002, 04:00 PM