Sign of integer overflow behavior

When adding two positive values Int32with a theoretical result greater than Int32.MaxValue, can I expect that the overflow value is always negative?

I want to do this as a way to check for overflow without using the checked context and handling exceptions (for example, it is suggested here: http://sandbox.mc.edu/~bennet/cs110/tc/orules.html ), but is this behavior guaranteed?

From what I have read so far, an integer overflow has been signed in C # specific behavior ( Is the behavior of an overflow of an integer type C # /. NET defined (unlike C / C ++) and Int32is two-component, so I would grateful for someone who would better understand this topic than me to test it.

Update
Quote from link 1:

The rules for overflow detection in the sum of the two additions are simple:

  • If the sum of two positive numbers gives a negative result, the amount is full.
  • If the sum of two negative numbers gives a positive result, the amount is full.
  • Otherwise, the amount is not overflowed.
+4
source share
2 answers

Rule number 2 of

http://sandbox.mc.edu/~bennet/cs110/tc/orules.html

  1. , .

:

  int a = int.MinValue;
  int b = int.MinValue;

  unchecked {
    // 0
    Console.Write(a + b);
  }

  1. , .

№ 1

  • , .

+4

, .

checked, , . , , , checked , unchecked. . , checked, unchecked, , , , .

, , , unchecked.

+1

Source: https://habr.com/ru/post/1648660/


All Articles