What is this' ! '

Discussion in 'Mac Programming' started by larswik, Oct 8, 2006.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    The book tossed this one out at me in code and didn't explain it.

    Code:
     while (!validInput); 
    I don't know why he put the ! in front of validInput

    Does it do anything?

    -Lars
     
  2. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #2
    It means "not"

    It's often used as shorthand for "equal to zero" in C.
     
  3. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #3
    hey thanks. That is the one thing about this book, the author just tosses in something with out explaining it. He might explain it later in the book like he has with other things but people who are learning, Java in this case, like to analyze everything and when something like that pops up I wonder what it does. Now I NOT know what it is, haha ! = NOT. Anyways it's late and that was my first programmers joke ever and a bad one I am sure : )

    -Lars
     
  4. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #4
    The world is filled with books like that! I do wonder how many textbooks are ever shown to people who don't know the subject yet, to see if they make any sense before they are printed.

    I am afraid that this is about as good as programming jokes get :D
     
  5. Sayer macrumors 6502a

    Sayer

    Joined:
    Jan 4, 2002
    Location:
    Austin, TX
    #5
    For future reference:

    ! - NOT, this must be false to continue
    != - NOT equal to N, for comparing non-boolean (numerical) values
    == - EQUAL to N, for comparing non-boolean (numerical) values
    && - AND, this AND that = both must be true to continue
    || - OR, this OR that = one or both must be true to continue

    % - modulus or mod, aka division returning only the remainder e.g. 17 mod 5 = 2 (2 being the remainder from 17 divided by 5).

    For bitwise operations you use only one & and | for "AND" and "OR."

    And then if you are doing C++ you use a lot of reference passing which has a & precede an argument in a function call. This just means "here is the address to the beginning of the object or data" which is typ. only 32 bits (4 bytes) in size instead of sending the entire chunk of data as an argument, which can be many, many bytes large.

    Code:
    	OSErr			theErr = noErr;
    	CInfoPBRec 		pBlock; // this is a very large data struct
    
    	memset(&pBlock, 0, sizeof(pBlock));
    
    	...
    
    	theErr = PBSetCatInfoSync(&pBlock);
    
    	if (theErr != noErr) return theErr;
     
  6. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #6
    So ! this must be false? the (!validInput) was of type boolean which could only result in a True or False answer.

    Here is another example from the book,
    Code:
     if (num ! = 12) 
    So in this example he is saying if it is NOT equal to 12, is my understanding of that right?

    So in the !validInput example it would only execute if it is NOT TRUE or would that convert a TRUE to a FALSE boolean type?

    thank,

    -Lars
     
  7. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #7
    Yes, when ! precedes an expression it changes TRUE to FALSE and FALSE to TRUE.

    The "!=" operator goes between two values you want to compare. In particular, if (num ! = 12) ... means to do the ... part when num is not equal to 12.

    If you think about it, you'll see that these two produce the same result:
    Code:
    if ( num != 12 ) ...
    
    if ( ! ( num == 12 ) ) ...
    The first uses the "not equal" comparison operator. The second uses the logical "not" operator on the result of the "equal" comparison. Said in English, the first is "num is not 12" and the second is "it is not the case that num is 12". So in English they mean the same thing too.
     
  8. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #8
    That is correct.

    Right.

    The first. It is only a test in this case. An assignment statement like:

    validInput = !validInput;

    …would be needed for ! to change the value.
     
  9. dukebound85 macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #9

    lol or there are 10 types of people in this world, those who understand binary and those who dont hahahaha now im a geek too
     
  10. Krevnik macrumors 68030

    Krevnik

    Joined:
    Sep 8, 2003
    #10
    If things make sense, great. Although a lot of this might be a little easier to understand/use if you read up a little on Boolean Algebra. It isn't required reading, and I am pretty sure that a few of the senior programmers in my product area never took classes, but I found that a lot of the logical operators made more sense and were more useful pieces of my programming knowledge once I read up a bit on it. Plus, it helps formalize a bit of the boolean logic used so heavily in expressions. The real only difference is the syntax in C for boolean operators are different then when you write it on paper.
     
  11. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #11
    ! is an operator, just like +, -, *, and /. The ! operator inverts a boolean, so true becomes false, but false also becomes true. An expression like (num == 12) evaluates to a boolean result, so that !(num == 12) is the same as (12 != num).

    The if test only looks at the final result after evaluating all expressions inside the parentheses. If this result is true, then the following commands are executed. If it evaluates to false then those commands are skipped.
     
  12. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006

Share This Page