What is the maximum value for a UInt32?
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.

- 22,699
- 3
- 58
- 66

- 23,555
- 29
- 141
- 228
-
2Wouldn't that be `2^32 - 1`? – devnull Nov 19 '13 at 11:35
-
1https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html#//apple_ref/doc/c_ref/NSNotFound – Mick MacCallum Nov 19 '13 at 11:40
-
9Damn SO, you used to be so friendly. This is a valid question, why all the down votes? Haters got to hate, I wish they would do it somewhere else though. – Aran Mulholland Nov 19 '13 at 22:14
-
1(uint32_t)0-1 ;-) – Codebeat Apr 10 '20 at 00:08
6 Answers
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

- 18,174
- 3
- 45
- 67
-
1You 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
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.

- 105,803
- 17
- 152
- 177

- 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
The portable way:
std::numeric_limits<uint32_t>::max()

- 41
- 2
-
-
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 – Renaud Aug 20 '15 at 18:18, 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. -
3This answer is C++ code, but the question is about Objective C on iOS. – Julien-L Jan 20 '16 at 17:47
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
}

- 979
- 10
- 18
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!

- 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