52
  1. What is the maximum value for a UInt32?

  2. Is there a way I can use the sizeof operator to get the maximum value (as it is unsigned)? So I don't end up with #defines or magic numbers in my code.

NSGod
  • 22,699
  • 3
  • 58
  • 66
Aran Mulholland
  • 23,555
  • 29
  • 141
  • 228

6 Answers6

80

There's a macro UINT32_MAX defined in stdint.h which you can use

#include <stdint.h>

uint32_t max = UINT32_MAX;

More about the relevant header <stdint.h>:

http://pubs.opengroup.org/onlinepubs/009695299/basedefs/stdint.h.html

CouchDeveloper
  • 18,174
  • 3
  • 45
  • 67
  • 1
    You have my up vote already, but it may be helpful for future users if you list a few more of these macros like MAXFLOAT and similar. – Mick MacCallum Nov 19 '13 at 11:45
  • @0x7fffffff You should take a look into . There are virtually a few dozens of macros which define min, max for all signed and unsigned integer types and a few other macros. These are defined in the [ISO/IEC 988:1999 specification](http://www.iso.org/iso/home/store/catalogue_ics/catalogue_detail_ics.htm?csnumber=57853). – CouchDeveloper Nov 19 '13 at 11:59
19

The maximum value for UInt32 is 0xFFFFFFFF (or 4294967295 in decimal).

sizeof(UInt32) would not return the maximum value; it would return 4, the size in bytes of a 32 bit unsigned integer.

peak
  • 105,803
  • 17
  • 152
  • 177
liamnichols
  • 12,419
  • 2
  • 43
  • 62
  • Thanks for that, I was thinking maybe you could do some simple arithmetic on it (sizeof(UInt32) * 8) ^ 2. Be a bit of overkill, I was a bit sleepy when I asked. – Aran Mulholland Nov 19 '13 at 22:16
4

The portable way:

std::numeric_limits<uint32_t>::max()
Renaud
  • 41
  • 2
  • why is this more portable? – Aran Mulholland Aug 17 '15 at 00:57
  • More portable in comparison with some hardcoded values. Maybe uint32_t is not a great example. If you take std::numeric_limits for example, you do not have to make assumption on how signed numbers are coded on the machine. If you take std::numeric_limits, you are not making any assumption about the size of an int which may not alway be 32 bit depending on the hardware that is targeted. It is also a bit more flexible than the macros. If you need to change the type, through a typedef for instance, you do not have to update all the UINT32_MAX like macros in your code. – Renaud Aug 20 '15 at 18:18
  • 3
    This answer is C++ code, but the question is about Objective C on iOS. – Julien-L Jan 20 '16 at 17:47
4

Just set the max using standard hexadecimal notation and then check it against whatever you need. 32-bits is 8 hexadecimals bytes, so it'd be like this:

let myMax: UInt32 = 0xFFFFFFFF

if myOtherNumber > myMax {
    // resolve problem
}
Ryan Dines
  • 979
  • 10
  • 18
2

4_294_967_295 is the maximal value or in hexadecimal 0xFFFFFFFF.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Modass
  • 197
  • 8
0

An alternative for any unsigned in C or C++ is:

anUnsigned = -1;

This is useful since it works for them all, so if you change from unsigned int to unsigned long you don't need to go through your code. You will also see this used in a lot of bit fiddling code:

anUnsigned |= -(aBoolOrConditionThatWhenTrueCausesAnUnsignedToBeSetToAll1s)
anUnsigned |= -(!aValueThatWhenZeroCausesAnUnsignedToBeSetToAll1s)
anUnsigned |= -(!!aValueThatWhenNonZeroCausesAnUnsignedToBeSetToAll1s)

The downside is that it looks odd, assigning a negative number to an unsigned!

Howard Lovatt
  • 968
  • 1
  • 8
  • 15
  • sometimes doing it this way causes compiler warnings or static analysis warnings, pity because it works fine! :) – ericcurtin Nov 19 '19 at 15:47
  • To avoid a compiler warning and make it clear to other readers that you know what you're doing, cast the value to the correct type, e.g. `anUnsigned = (uint32_t)-1;` . – Jac Goudsmit Nov 28 '22 at 18:12