Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 12
Assume for the moment you can only hold -16 (10000) to 15 (01111) in an integer (5 bits). If you add 1 (00001) to 15, your integer can not hold the 16. But, the computer does not know this so it does it anyway. The most significate bit of 16 is placed into the sign bit making the result -16 (i.e. negative zero as the sign bit is negative while the data bits are zero). Negative numbers count up so -1 has a negative sign bit and data bits of 15 (11111). The same overflow switches -1 to 0 as the sign bit is tossed. 11111 (-1) 00001 (1) 100000 leaving 00000 (0) 01111 (15) 00001 (1) 10000 (-16)
10th Dec 2020, 3:22 PM
John Wells
John Wells - avatar
+ 11
It's all because of the binary representation of the number. See this https://stackoverflow.com/questions/29235436/c-integer-overflow
10th Dec 2020, 2:30 PM
XXX
XXX - avatar
+ 6
https://code.sololearn.com/cf5yTwLWJxYA/?ref=app as in code above int takes a value of 4 bytes , sizeof(intvar) .. takes 32 bits . for long variable takes 8 bytes etc .any exeeding becomes the two's complement of the representation resulting in decimal negative.
10th Dec 2020, 2:44 PM
Ona Nixon 🇹🇿 👑
Ona Nixon  🇹🇿  👑 - avatar
+ 2
If u want to solve this overflow error, u can try these data types: long, long long And for decimal or very high value then u can try these data types: float, double, long double And to know, what is the maximum value of integer can hold then u can see it by printing value of: INT_MAX (this is a constant)
11th Dec 2020, 2:56 PM
MOHAN👨🏻‍💻
MOHAN👨🏻‍💻 - avatar