Thread: When to use uint32?

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132

    When to use uint32?

    I just red this stackoverflow post and still don't get it. Aside from serializing to file, when would an int not work and a uint32 would be better? Same question for uint16 and uint8. I know uint32 means the size reserved for the variable is gauranteed to be 32 bits but 1) why would that matter? 2)since the range is defined of an int wouldn't that imply the size is always the same anyways, i.e. log_2(2^32)=2

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by c_weed
    since the range is defined of an int wouldn't that imply the size is always the same anyways, i.e. log_2(2^32)=2
    The minimum range of int is defined, but implementations are free to exceed it. In fact, mathematically, we only need 16 bits to cover the minimum range of an int, not 32.
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    AFAIK, the standard only mentions it has to be larger than or same as a short and smaller than or same as a long.

    When you need exactly 32 bits...say for storing 32 boolean flags, it should be a good idea to use the exact type.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132
    Ok. No ones answered when they should be used. For example if it doesn't matter, like your getting user input, should int or uint16 be used?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    the standard only mentions it has to be larger than or same as a short and smaller than or same as a long.
    It is true that the C++ standard does not define the minimum limits but rather the relative sizes. However, it makes references to the C standard on this point, which does define the minimum limits.

    Quote Originally Posted by c_weed
    No ones answered when they should be used.
    manasij7479 gave the example of boolean flags, though in C++ there's std::bitset for that. There are certain algorithms that might have such a requirement of number of bits, e.g., the kind that deals with cryptography. In the example of serialization that was cited to you earlier, portability might be the concern, i.e., a number serialized on one platform should be unserialized without loss of data on another platform.

    Quote Originally Posted by c_weed
    For example if it doesn't matter, like your getting user input, should int or uint16 be used?
    I would probably choose int because uint16_t is not guaranteed to be available, but it really depends on your requirements. Since you say "it doesn't matter", then you have no requirements, so generate a random number to pick your integer type (or use std::string instead).
    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 Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    You could use it to store rgba values for 32 bit color depth, or bit flags, or store any unsigned value that wouldn't fit in a 16 bit unsigned integer type. If you're getting user input, you just pick a type that will hold the input (also keeping in mind all the operations you will be doing with that variable).
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. two uint16 types to uint32
    By Lauri1991 in forum C Programming
    Replies: 3
    Last Post: 07-18-2013, 05:35 AM
  2. Uint32 math
    By Akkernight in forum C++ Programming
    Replies: 12
    Last Post: 03-22-2009, 04:46 PM
  3. Accessing HW registers using bit fields and UINT32
    By samdomville in forum C Programming
    Replies: 4
    Last Post: 12-10-2008, 01:00 PM
  4. UINT32 x : 1 ?
    By mottoshiritai in forum C Programming
    Replies: 3
    Last Post: 06-25-2008, 03:53 AM
  5. uint32_t or uint32
    By RoshanX in forum C Programming
    Replies: 7
    Last Post: 03-31-2007, 06:41 AM