2

How much is NSNotFound exactly?

According to this post, it's NSIntegerMax.(Although I can't find on iOS 9.3 on XCode.)

So in 64 bits system, it should be 2^64-1 or 2^63-1? I think it's 2^64-1 but a post said it's 2^63-1, which one correct?

Community
  • 1
  • 1
allenlinli
  • 2,066
  • 3
  • 27
  • 49
  • 1
    Is the actual value important? It's not something you save, it's something that you compare against. – Abizern Jul 31 '16 at 14:43

2 Answers2

6

It is 2^63-1. In a 64 bit number the least significant bit is 2^0 and the most significant is 2^63 making a total of 64 bits

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • I just understood now, it's the max number in signed integer, not unsigned integer. So that 2^0 doesn't mean 0 or 1, it actually means -2^63. – allenlinli Jul 31 '16 at 13:49
  • 1
    No, there are 64 bits. 2^0 is the first bit and 2^63 is the 64th bit. Think of an array with 64 elements; array[0] is the first element and array[63] is the 64th element. In a signed integer the maximum value that can be represented is 2^63-1 and the smallest is -2^63+1 In an unsigned the largest value is 2^64-1 – Paulw11 Jul 31 '16 at 20:59
3

This is how it's defined in Foundation/NSObjCRunTime.h:

static const NSInteger NSNotFound = NSIntegerMax;

So on 32-bit devices it's 2^31 - 1, on 64-bit devices it is 2^63 - 1. In Swift, you can find the constant as Int.max

Code Different
  • 90,614
  • 16
  • 144
  • 163