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

This is a discussion on ((uint32_t)(~0UL)) ?? within the C Programming forums, part of the General Programming Boards category; Can anyone explain this definition? Code: #include <stdint.h> #include <stdbool.h> #define NULL_BLOCK ((uint32_t)(~0UL))...

  1. #1
    frs
    frs is offline
    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
    22,269
    Recall that ~ is the bitwise complement operator.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    265
    ~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.

  4. #4
    frs
    frs is offline
    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
    22,269
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    frs
    frs is offline
    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
    22,269
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    frs
    frs is offline
    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21