Since the 256 possible bit patterns for 8 bits (28 = 256) are to represent both positive and negative values, half of them must be negative.
So the range is
-128 to +127
So overflow occurs if mathematically a sum is > 127 or < -128.
For the 8 bit signed type char, binary addition works the same way as for unsigned char.
That is, if there is a carry out of the 7th bit, it is discarded.
This carry in to the 8th bit position has value 256 (= 28). So discarding it is the same mathematically as subtracting 256.
Examples
char x | char y | char z = x + y (mathematically) |
(z <= 127)? z : z - 256 |
---|---|---|---|
126 | 1 | 127 | 127 |
126 | 2 | 128 | -128 (= 128 - 256) |
126 | 3 | 129 | -127 (= 129 - 256) |
(If z is negative mathematically, then as signed char, the value is (z >= -128) ? z : z + 256.)