the maximum value for an 32-Bit integer is: 2^31-1 = 2147483647. but just with negative and positive number. because the half of the number is negative. so the real maximum value is 2^32-1 = 4294967295. but in this case we just use positive numbers.
ok, a normal int is both negative and positive number. i want to use just positive number because i want the maximum value to be: 4294967295. i'm going to use "unsigned int" instead of "int"
but this will not work! the maximum value is still 2147483647.
here is the code for a simple random number generator:
-(Action for my button) {
unsigned int minNumber;
unsigned int maxNumber;
unsigned int ranNumber;
minNumber=[self.textFieldFrom.text intValue]; //getting numbers from my textfields
maxNumber=[self.textFieldTo.text intValue]; //Should i use unsigned intValue?
ranNumber=rand()%(maxNumber-minNumber+1)+minNumber;
NSString *str = [NSString stringWithFormat:@"%d", ranNumber];
self.label.text = str;
}
and this will view : 2147483647 as a maximum value.
what's wrong? should i use unsigned intValue when i getting numbers from my textFields?
Jonathan
here you can read about this number. : http://en.wikipedia.org/wiki/2147483647
The number 4,294,967,295, equivalent to the hexadecimal value FFFF,FFFF16, is the maximum value for a 32-bit unsigned integer in computing.
A 32-bit unsigned integer. It has a minimum value of 0 and a maximum value of 4,294,967,295 (inclusive).
It takes a size of 32 bits. A maximum integer value that can be stored in an unsigned int data type is typically 4, 294, 967, 295, around 232 – 1(but is compiler dependent). The maximum value that can be stored in unsigned int is stored as a constant in the <climits> header file.
To find the max value for the unsigned integer data type, we take 2 to the power of 16 and substract by 1, which would is 65,535 . We get the number 16 from taking the number of bytes that assigned to the unsigned short int data type (2) and multiple it by the number of bits assigned to each byte (8) and get 16.
The problem is intValue
which returns a int
(not an unsigned int
) and is maxed out at 2,147,483,647. If you need larger than INT32_MAX
, then use long
variables and NSString
method longLongValue
instead of intValue
.
Yes, %d
is signed, but if you use %u
that will not solve the problem with intValue
. Using long long
variables and longlongValue
method will solve this. For example:
NSString *string = @"2147483650";
unsigned int i = [string intValue];
NSLog(@"i = %u", i); // returns 2147483647
NSUInteger j = [string integerValue];
NSLog(@"j = %u", j); // returns 2147483647
long long k = [string longLongValue];
NSLog(@"k = %lld", k); // returns 2147483650
So, looking at your original code, it might be:
long long minNumber;
long long maxNumber;
long long ranNumber;
minNumber = [self.textFieldFrom.text longLongValue];
maxNumber = [self.textFieldTo.text longLongValue];
ranNumber = arc4random_uniform(maxNumber-minNumber+1) + minNumber;
NSString *str = [NSString stringWithFormat:@"%lld", ranNumber];
self.label.text = str;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With