Thread: ((uint32_t)(~0UL)) ??

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    29

    ((uint32_t)(~0UL)) ??

    Can anyone explain this definition?

    Code:
    #include <stdint.h>
    #include <stdbool.h>
    
    #define NULL_BLOCK ((uint32_t)(~0UL))

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall that ~ is the bitwise complement operator.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    ~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 11:40 AM.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    29
    Quote Originally Posted by Syscal View Post
    ~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.
    Thank you.
    Where can I find information about these conventions?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by frs
    Where can I find information about these conventions?
    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.

    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    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?

  7. #7
    Registered User
    Join Date
    Jun 2010
    Posts
    29
    so 14UL is the same as (unsigned long)14?? I'm talking about the syntax, 0UL, never seen a cast made that way.

    Quote Originally Posted by laserlight View Post
    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.

    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).

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by EVOEx
    II 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...
    As a consequence of the minimum range of an unsigned long, an unsigned long object must be at least 32 bits in size.

    Quote Originally Posted by frs
    so 14UL is the same as (unsigned long)14?? I'm talking about the syntax, 0UL, never seen a cast made that way.
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2010
    Posts
    29
    Quote Originally Posted by laserlight View Post
    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.
    Hmm, thank you once again.

Popular pages Recent additions subscribe to a feed