I need to assign 2,554,416,000 to a variable. What would be the primitive to use, and what would be the object representation class to use? Thanks.
2,554,416,000 = 0x9841,4B80 ≤ 0xFFFF,FFFF (UINT_MAX
), so uint32_t
(unsigned int
) or int64_t
(long long
).
A signed int32_t
(int
) cannot represent this because 0x9841,4B80 > 0x7FFF,FFFF (INT_MAX
). Storing it in an int
will make it negative.
This can be represented by a 32-bit unsigned integer (UINT_MAX is about 4 billion). That's actually what NSUInteger is on the iPhone, but if you want to be very specific about the bit width, you could specify a uint32_t
.
Chuck is right, but in answer to the "object representation", you want NSNumber
used with the unsignedInt
methods.
NSNumber *myNum = [NSNumber numberWithUnsignedInt:2554416000];
NSUInteger myInt = [myNum unsignedIntValue];
You could store it in a regular int scaled down by 1000 if you wanted, if this represented a score that could never have the bottom 3 digits hold any info or something similiar. This would be a way to save a few bits and possibly an entire extra int of space, if that matters.