Why is the value of NSUInteger 2^32 - 1 instead of 2^32? Is there a relationship between this fact and the need of a nan value? This is so confusing.
-
Also, when dealing with programming shift your thought into thinking of counting from 0 to n-1 (for n states) rather than 1 to n. This will help you in general (eg: dealing with arrays, this question, etc...) – RonaldBarzell Dec 03 '12 at 19:52
-
3Nit: There is no `NaN` integer value. NSUInteger != NSFloat. – Dec 03 '12 at 19:58
-
1Because also the zero is needed, you could go from 1 to 2^32 included.They choosed from 0 to 2^32-1, but the number of possibile values is the same. – Ramy Al Zuhouri Dec 03 '12 at 20:44
-
1Steven Frank has written an entire book on how computers think about numbers (including this very topic), titled “How to Count”: http://amzn.com/B005DPIKPE – Peter Hosey Dec 04 '12 at 07:26
5 Answers
Count to 10 on your fingers. Really :)
The standard way to count to 10 is 1,2,3,..10
(the ordinality of each finger is counted). However, what about "0 fingers"?
Normally that might represent that by putting your hands behind our back, but that adds another piece of information to the system: are your hands in front (present) or behind (missing)?
In this case, putting hands behind your back would equivalent to assigning nil
to an NSNumber
variable. However, NSUInteger
represents a native integer type which does not have this extra state and must still encode 0 to be useful.
The key to encode the value 0 on your fingers is to simply count 0,1,2..9
instead. The same number of fingers (or bits of information) are available, but now the useful 0
can be accounted for .. at the expense of not having a 10
value (there are still 10 fingers, but the 10th finger only represents the value 9
). This is the same reason why unsigned integers have a maximum value of 2^n-1
and not 2^n
: it allows 0
to be encoded with maximum efficiency.
Now, NaN
is not a typical integer value, but rather comes from floating point encodings - think of float
or CGFloat
. One such common encoding is IEEE 754:
In computing, NaN, standing for not a number, is a numeric data type value representing an undefined or unrepresentable value, especially in floating-point calculations ..
-
"yet NSUInteger is only a wrapper object for the native integer types" - euh. Nope. It's a typedef for `int` or `long`, it's not an object. Also you cannot assign `nil` to it without casting, since `nil` is a pointer. – Dec 03 '12 at 20:41
-
@H2CO3 Yes, I was very wrong :( I was confusing it with NSNumber. Thanks for pointing it out. – Dec 03 '12 at 20:44
-
2^32-1 because counting starts from 0 for bits. If it's easier think of it as 2^32 - 2^0.

- 33,938
- 5
- 80
- 91
It is the largest value a 32-bit unsigned integer variable can hold. Add one to that, and it will wrap around to zero.
The reason for that is that the smallest unsigned number is zero, not one. Think of it: the largest number you can fit into four decimal places is 9999, not 10000. That's 10^4-1.

- 59,826
- 25
- 160
- 281
-
To be more specific, “it will wrap around to zero” if you try it in C, because the next larger value cannot fit. Mathematically, the next larger value from 2**32 - 1 is 2**32. If you write that out in binary, it's 1 followed by 32 zeroes (just as 10**3, in base 10, is 1 followed three zeroes—or 1000). 1 followed by 32 zeroes is 33 digits, which obviously cannot fit into a 32-binary-digit (32-*bit*) space. The largest value that can fit is one less: 2**32 - 1. – Peter Hosey Dec 04 '12 at 07:21
You cannot store 2^32 in 4 bytes, but if you subtract one then it fits (result is 0xffffffff)

- 4,384
- 1
- 20
- 26
Exactly the same reason why the odometer in your car shows a maximum of 999999 mi/km (assuming 6 digits) - while there are 10^6 possible values it can't show 10^6 itself but 0 through 10^6-1.

- 52,522
- 5
- 70
- 86