How to break large numbers into more manageable parts in iOS?
I want to create an app for iOS 开发者_JAVA技巧that utilizes a random 80 bit number but I am virtually certain that the current hardware can't handle numbers that large. So what's a good way to break the number into smaller pieces? Thus far the best I can think of is to break it into 4 20 bit blocks, but I'm not happy with how much processor capacity doing it that way takes up. Thank you for any help you can give me.
How about two ints and a short? That'll get you 32 + 32 + 16 = 80 bits. I can't tell what you're trying to do, but I probably wouldn't worry about processor capacity at this point.
If you want to do calculations with that number, you're probably better off using an existing library, such as GMP instead of developing your own. If it's just used as a hash, cryptographic key, or something like that, you should use NSData
or a raw byte array.
Wouldn't it be plenty efficient to make a class that malloc's 80 booleans? the BOOL type is already "true/false", and the extra memory overhead is a minimal concern, equivalent to two or three hard-coded strings' worth of memory. I'll try for an example below:
UN-TESTED CODE :
@interface KenoBoard : NSObject{
BOOL * boardSelections;
}
-(BOOL)selectionForPosition:(int)number;
-(void)setSelection:(BOOL)selection forPosition:(int)number;
@end
@implementation KenoBoard
-(id)init{
if(self = [super init]){
boardSelections = calloc(80*sizeof(BOOL));
}
return self;
}
-(void)dealloc{
free(boardSelections);
[super dealloc];
}
-(BOOL)selectionForPosition:(int)number{
if (number >= 80){
...make a NSException here
@throw exception
}
return boardSelections[number];
}
-(void)setSelection:(BOOL)selection forPosition:(int)number{
if (number >= 80){
...make a NSException here
@throw exception
}
boardSelections[number] = selection;
}
@end
精彩评论