What is the best way to check for integer overflow if I am adding 2 positive numbers together in c# if overflow isn't a condition that should cause an exception?
Can i just check if the result is negative then it is an overflow?
What is the best way to check for integer overflow if I am adding 2 positive numbers together in c# if overflow isn't a condition that should cause an exception?
Can i just check if the result is negative then it is an overflow?
If overflow is OK, you could check for negative, but that wouldn't account for large enough overflows. If it overflows enough to become positive again, checking for negative obviously wouldn't work.
But for two positive integers, checking for negative will work fine.