2
overflow = c,n āŠ• c,nāˆ’1

I tried it with all four possible cases

                            c,n   c,n-1
-7+2      1001+0010         0     0         
7+2       0111+0010         0     1
-7+(-2)   1001+1110         1     0
7+(-2)    0111+1110         1     1 

and it seems to work, but can someone explain or prove why?

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
user342231
  • 519
  • 1
  • 6
  • 6

2 Answers2

2

When adding two numbers with n bits, the result can have n+1 bits. This means that by using n bits for result we cannot represent all of them.

Now, what is an overflow? If we consider a signed number, it will mean that we can extend the number by adding sign bit (MSB) above MSB, and they will be equal. So, when this does not hold (next bit after MSB is not equal to MSB, something that can be detected before truncating the result to n bits), then we say it's overflow.

As is your example, we say that the overflow occurs when after adding two positive number we obtained a negative one (or two negative that gave positive result).

Also, check this answer by Thomas Pornin, I think he explained it wery well.

Community
  • 1
  • 1
ruslik
  • 14,714
  • 1
  • 39
  • 40
0

It's been so long, I have only a vague understanding of what the notation is saying. I'd assume that 'c' is a carry flag, and 'n' is a negative flag. But then what is 'n-1'?

Anyway, I'm guessing your answer pertains to overflow occurring in either direction: from a negative number wrapping over into a positive, and a positive wrapping over into a negative.

Brent Arias
  • 29,277
  • 40
  • 133
  • 234
  • c,n is one variable, (what I meant is c subscript n, which I can't really type out). In this case n = 4, c,n is the carry flag for the most significant bit, and c,n-1 is the carry flag for the 2nd most significant bit. They are set when there is a need for a carry over. – user342231 Jan 19 '11 at 06:42