Things that I missed in learning C/C++

Started by
2 comments, last by Daishim 22 years, 9 months ago
I''m going through Andre LaMothe''s Tricks Of The Windows Game Programming Guru and have come accross a few things that I missed in learning C/C++ or am just not making the logical connection. First, the use of ! in
 

if(!(hwnd = CreateWindowEx(... )...

I don''t remember using the ! in this kind of situation.  What is it''s function here? ... I''m assuming that it''s providing a kind of error check in tangent with the if, but yet again, am not sure.

Second, combining things with OR.  Now the most immediate thought is that it''s not possible.  Or is one and not the other, but I''m assuming that I''ve something here too.

Third, Andre LaMothe keeps using  in his book... what does that mean?

Thanks in advance.    

I know only that which I know, but I do not know what I know.
Advertisement
Having never read LaMothe's book, I'm a little surprized he's putting statements like that without explaining them, especially since the books are aimed at newbies.

Ok, here's a quick explanation, the '=' operator is not all that unlike the '+' and '-' operators, in that it resolves to something. What do I mean? Well, 1 + 2 "resolves" to 3. Just like that does a = 1 "resolve" to 1. That's how you can do things like:

      a = b = c = 1;  


and have them all equal 1. What happens in the compiler resolves c = 1 first, which set c to 1 and is turned into 1. Then b = 1, then a = 1.

So, hwnd = CreateWindowEx(...) will resolve to whatever CreateWindowEx() returns. If it returns NULL, then there was an error. Another way of writing that line would be:

  hwnd = CreateWindowEx(...);if( !hwnd ){    ....}  


Now, as for combining things with OR, there's a difference between logical OR (||) and arithmetic OR (|). Logical OR assumes both arguments are boolean, and returns true if either value is true, and false only if both values are false. Arthimetic OR assumes both values are integers, and then returns another integer which has all it's bits set such that:

output[j] = lhs[j] OR rhs[j]

(where the [j] denotes the jth bit), So (1 | 2) == 3 (in binary: 01 | 10 == 11)

Finally, I believe means "Big Grin"...

(EDIT: I doesn't like having "i" in square brackets

War Worlds - A 3D Real-Time Strategy game in development.

Edited by - Dean Harding on June 25, 2001 11:41:19 PM
! is the logical not operator. Here is a quick example:

strcmp() returns 0 (false) if both strings are equal, yet an if statement will only evaluate to use it''s statement if the condition is 1 (true).

So doing:

if (!strcmp("Hello", "Hello"))
{
cout << "Whoa, our strings are equal." << endl;
}

This will convert the strcmp()''s return value of 0 to 1. It converts it to a true or false expression, then it inverts it. So remember:

!true is false.
!false is true.
"The time has come", the Walrus said, "To speak of many things."
Indeed... The ! operator is NOT...

In your case as Dean mentioned, it is testing to see if your hwnd was actually created. If there was some error, CreateWindowEx will return a NULL value to hwnd. NULL is the same as 0, or false. So basically the expression is saying:

If CreateWindowEx does NOT return a valid pointer, we must assert. A valid pointer would be anything that is not NULL or zero.

Consider this:

  #include "lifestuff.h"int life;bool imAlive(){    if (life > 0)        return true;    return false;}int main( int argc, void *argv[] ){    if ( !imAlive() )    {        printf("I''m Dead!!!");    }    else    {        printf("I''m Alive!!!");    }    return 0;}  


Here, imAlive function will return ''true'' if life is greater than 0, or we are still alive.

the expression:
  if ( !imAlive() ){printf("I''m Dead!!!");}  


is saying in English:

If I am NOT alive, say that I am dead!

Alot of the C library functions will make this a little wierd, because although alot of them you might think their return values mean true or false that is not really the case. For instance, the strcmp example used earlier is a function which will compare 2 string. But the return value is not true or false, but for instance in this case the C standard says that strcmp:

Returns a value indicating the lexicographical relation between the strings.

if the return value is less than 0, then string1 is less than string2. If the return value is greater than 0, string 1 is greater than string2, and if the return value is equal to 0, the strings are equal. When they created this they had in mind this expression:
  if ( strcmp(str1,str2) == 0 ){    // the strings are equal}  

but for the sake of less typing and picking up chicks it turned into:
  if (!strcmp(str1,str2) ){    // the strings are equal}  


Which although it looks like it says ''If the strings are NOT equal'', it is really saying ''If the strings are NOT different'' because a non-zero value would mean they are different, and by default any non-zero value is converted to a 1 or true using boolean expressions or math.

------------------

When you use the bitwise OR (|) operation, here is what happens on a bit level. Let''s say you have 2 8-bit binary numbers which are:

10110100 (128 + 32 + 16 + 4 = 180)
and
00011100 (16 + 8 + 4 = 28)
--------

What an OR does basically is it goes through each bit of each binary digit. If the first binary digit of the first number is equal to 1 OR the first binary digit of the second number is equal to 1, then it will be 1. If both of them are 1, it will still be 1. If neither of them are 1, it will be a 0. So our operation would do this:

10110100 (180)
OR
00011100 (28)
--------
10111100 (188)

if we AND them (&), we only set the digit to 1 if both numbers have that digit set as 1... so:

10110100 (180)
AND
00011100 (28)
--------
00010100 (20)

if we XOR (^), or eXclusive OR them, if one of the digits is 1 and the other is 0 it is set at 1, if both digits are the same it sets it to 0. so:

10110100 (180)
XOR
00011100 (28)
--------
10101000 (168)


Hope I did not confuse the issue... lol

Seeya
Krippy

This topic is closed to new replies.

Advertisement