Reputation: 3412
Im trying to create an app that will use very large numbers. I was wondering if the storage power of [NSNumber numberWithUnsignedLongLong] is the max number I can get? And if NSUInteger has the same storage power as a [NSNumber numberWithUnsignedLongLong]?
Upvotes: 0
Views: 558
Reputation: 53010
You are correct the largest unsigned integer type you[*] can store in an NSNumber
is unsigned long long
- and on current systems expect this to be 64 bits.
Is this type equivalent to NSUInteger
? No, that is platform dependent and is either an int
or long
, but not a long long
. Just use unsigned long long
or typedef
it, e.g.:
typedef unsigned long long MYULongLong
You could use a sized type, such as uint64_t
, but there are no matching sized methods on NSNumber
. You can address that by adding such methods to NSNumber
using a category and conditional code based on sizeof
- with a little care you can write that so the conditionals all disappear during compilation, that is left as an exercise ;-)
HTH
Upvotes: 0
Reputation: 194
NSUInteger is a typedef of a basic c-type. The exact type depends on your platform:
#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
So sizes can vary by implementation of C, but unsigned long is at least 32 bits and unsigned long long is at least 64 bits.
Using types where you know the size is probably better when you're worried about overflowing. They can always be wrapped in objective-C types if needed.
uint64_t which holds a number up to UINT64_MAX might be useful.
#define UINT64_MAX (18446744073709551615ULL)
Upvotes: 1
Reputation: 122391
Just use uint64_t
(64-bit unsigned integer, which is the same as unsigned long long
).
You don't want to use NSNumber
unless you are storing the vales within an Objective-C collection class (NSArray
, for example), as they are immutable making them very difficult and expensive to manipulate.
Upvotes: 0