Negative values.. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Negative values..

Why do you think that long long numbers come out negative sometimes when they should be positive? Check out my little code sample to see what i mean...Its fixable by making it unsigned,but why negatives sometimes?... https://code.sololearn.com/c8C36YAPmTBf/?ref=app

21st Jan 2019, 2:06 PM
Mensch
Mensch - avatar
3 Answers
+ 4
Use unsigned long long instead. If it comes up negative, that's because it's overflowing. Unsigned numbers double the range for positive numbers compared to signed, but you're still reaching the limit in the latter values, which explains the erroneous results you are still getting with unsigned. Range for unsigned long long int is 0 to 18,446,744,073,709,551,615. https://en.wikipedia.org/wiki/Integer_overflow
21st Jan 2019, 2:38 PM
Zen
Zen - avatar
+ 2
Thanks Bennett Post,i hadn't thought of that๐Ÿ‘๐Ÿ‘
22nd Jan 2019, 5:29 AM
Mensch
Mensch - avatar
0
I see most reply tell you to use an unsigned type But be aware that unsigned type will accept affectation with negative value (which overflow) If you have an overflow with a type, use a bigger one Or try to store 2 values Like 2 32bits word used to create a 64bits word
21st Jan 2019, 10:52 PM
Abel Louis
Abel Louis - avatar