Skip to main content
gamedev.net
🔒 Locked

C Syntax Question

Started by Sean Doherty Dec 11, 2005 at 8:12 AM 4 replies 1.2k views
Original Post
Sean Doherty
Sean Doherty
1) What does this mean? I understand that it is being cast to a double but I don't know what the ~Ou if for? (double)~0u 2) What is the ! doing in this example? int ptx[NPT], pty[NPT], ptz[NPT]; //Assume the array has been populated. !pty[n] Thanks [Edited by - Sean Doherty on December 11, 2005 8:42:09 AM]
_______________________________________Understanding is a three edged sword...Freelance Games - Home of XBLIG Starchonwww.FreelanceGames.com
Illco
Illco
They are both operators, unary ones. See this link.

In short, the tilde does a bitwise inverse of its operands (one's complement or simply inverse) and the exclamation mark stands for logical not (i.e. if the argument is non-zero the result is false, and vice versa).

The not-operator is used in particular to test if a pointer is valid; invalid pointers (if looked after carefully) should have the value zero. Therefore the expression !p yields true if the pointer is zero and false otherwise.

An example would be where one wants to test the validity of the pointer:
void Test( int* p ){  if ( !p )  {    // Dont use the pointer; it is invalid  }  else  {    // We can safely dereference the pointer    printf( "The value is %d.\n", *p );  }}


Illco
Trap
Trap
0u <=> (unsigned int)0
~0u <=> ~((unsigned int)0) <=> 0xFFFFFFFF (if int is 32-bit)

It creates a double with 2^32-1 as it's value.
ToohrVyk
ToohrVyk
~0u is parsed as ~(0u), which is simply the ~ unary operator being applied to the integer literal 0u.

!pty[n] applies the unary operator ! to the object pty[n] (the default semantics for booleans represented as booleans or integers is a logical not). pty[n] generally denotes the n-th element of pty, although it might be user-defined to mean something else.
Enigma
Enigma
I consider both to be bad form since they are unnecessarily obscure. In fact the second one doesn't even make sense. Assuming that they are both the same array (they actually use have different names in your example) !pty[n] will always evaluate to false (except for the cases where n is out of bounds, in which case the behaviour is undefined). (double)UINT_MAX is equivalent to (double)~0u and far more readable.

Enigma
Sean Doherty
Sean Doherty
So the logical not will return zero unless the value being checked is zero and then it returns a value of one.

I totally missed that 0u! When I looked at it I thought it was Ou not zero u:)

Thanks for the explanation
_______________________________________Understanding is a three edged sword...Freelance Games - Home of XBLIG Starchonwww.FreelanceGames.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.