In WinSock2.h, the invalid socket and socket error are defined as these? Is there any significance to this?
#define INVALID_SOCKET (SOCKET)(~0)
#define SOCKET_ERROR (-1)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
On a two’s complement system (and Windows is always two’s complement),
~0is equal to-1, so there’s no significance to the compiler.There may be a significance to the reader:
~0emphasizes that it’s a value with all bits set, whereas-1emphasizes that it’s a value 1 less than 0.Aside:
On a system which is not two’s complement, and assuming that
SOCKETis an unsigned type, it is generally wrong to write(SOCKET)(~0). The reason is that on such systems,~0does not represent the value -1, it’s one ofINT_MIN, negative zero, or a trap representation. Hence it will not necessarily convert to typeSOCKETas the value with all bits zero, rather it will convert asINT_MAX+2,0, or goodness-knows-what (perhaps the value with all bits set).So generally you should initialize unsigned types with
-1to get the value with all bits set. You could useUINT_MAX, or~0UL, or similar, if you know which unsigned type you’re dealing with. But it’s not worth it, because-1works for all unsigned types.