Integer overflow
suggest changeThere is a maximum capacity an integer can store. And when you go over that limit, it will loop back to the negative side. For int
, it is 2147483647
int x = int.MaxValue; //MaxValue is 2147483647
x = unchecked(x + 1); //make operation explicitly unchecked so that the example also works when the check for arithmetic overflow/underflow is enabled in the project settings
Console.WriteLine(x); //Will print -2147483648
Console.WriteLine(int.MinValue); //Same as Min value
For any integers out of this range use namespace System.Numerics which has datatype BigInteger. Check below link for more information https://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents