Can anyone explain this definition?
Code:#include <stdint.h> #include <stdbool.h> #define NULL_BLOCK ((uint32_t)(~0UL))
Can anyone explain this definition?
Code:#include <stdint.h> #include <stdbool.h> #define NULL_BLOCK ((uint32_t)(~0UL))
Recall that ~ is the bitwise complement operator.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
~0UL means that complement of 0 is to be interpreted as an unsigned long explicitly casted as an unsigned int.
~0UL = 4294967295 or 0xffffffff
Thats the largest value that can be in a 32 bit integer.
Last edited by Syscal; 09-18-2010 at 12:40 PM.
If you are talking about the values, then you can #include <limits.h> and check the constants, though in this case it would have been simpler to just print the result out to verify the value for yourself.Originally Posted by frs
If you are talking about the meaning of the expression, then as I noted it is about knowing what ~ means (and of course the cast).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I wonder though if it's actually valid. I don't think so: "0UL" is not guaranteed to be 32 bits is it? Or is it guaranteed to be at least 32 bits? If not, say a long may be 16 bits, then 0UL may be 16 bits, and ~0UL would have 16 bits set to 1, and casting it to uint32_t won't change this...
So my question to those who know a bit more about the strict specification: is this actually legal?
As a consequence of the minimum range of an unsigned long, an unsigned long object must be at least 32 bits in size.Originally Posted by EVOEx
Ah. In terms of value and type, yes. It is not a cast; it is a way of specifying that the integer constant is of type unsigned long.Originally Posted by frs
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)