Reputation: 197
I'm working with multiplications in my objective c application. The problem is, when i do some multiplications, sometimes it gives me a false result :
Exemple :
int intern = 77986;
int secondNumber = 70654;
long long int multiply = intern * secondNumber;
NSLog(@"Intern : %i two : %i mult : %lli", intern, secondNumber, multiply);
The result is :
Intern : 77986 two : 70654 mult : 1215055548
The result would be 5510022844
I don't understand... Thank you for your help !
Upvotes: 0
Views: 114
Reputation: 318804
It's a simple overflow issue. An operation with two int
variables is done as an int
. But the result is too big for int
so it overflows. Then the overflowed result is assigned to the long long int
.
Making either (or both) of the first two int
variables into long long int
will result in the operation being done as a long long int
resulting in the expected and correct result.
To clarify, you can either do:
long long int intern = 77986;
// and the rest
or do:
long long int multiply = (long long int)intern * secondNumber;
Upvotes: 3
Reputation: 360662
125055548 -> 0x 486C46BC
5510022844 -> 0x1486C46BC
Since you're multiplying ints, the result is an int, and gets truncated down to 32bits.
Upvotes: 0