views:

48

answers:

4

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.

+1  A: 

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.

KennyTM
+1  A: 

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
@Squeegy, your code is using a signed integer, not an unsigned integer. Try an unsigned integer.
Alex Reynolds
Yeah, I figured that out and deleted the comment, hoping you wouldn't see it and I wouldn't look like an idiot.
Squeegy
@Squeegy, mission accomplished! Thanks guys.
marty
+1  A: 

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];
Squeegy
A: 

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.

Michael Dorgan