0
$\begingroup$

I understand the rule says that overflow cannot happen for two's complement addition of numbers with different signs, but do not understand why this specific case does not cause overflow:

 1001010
+0010101
--------
 1011111

Am I simply misunderstanding the actual rule or missing a step somewhere? Thanks in advance!

EDIT: removed an extra 1 in the result

$\endgroup$
  • $\begingroup$ 1/ carry out does not imply 2's complement overflow. 2/ the two MSB are 0, I don't think there is a carry out in your example $\endgroup$ – AProgrammer May 1 '17 at 11:50
  • $\begingroup$ Sorry, the carry was a mistake, fixed now! $\endgroup$ – CluelessButCurious May 1 '17 at 12:30
0
$\begingroup$

Okay, let's take a look at your example:

 1001010
+0010101
--------
 1011111

Your numbers here only have 7 bits, so we have a smaller range than we typically deal with. Nevertheless:

$1001010$ is

$-64 + 8 + 2$, which is $-54$.

$0010101$ is

$16 + 4 + 1$, which is $21$.

$21-54$ is $-33$. Let's see whether our answer matches up:

$1011111$ is

$-64+16+8+4+2+1$, which is $-33$.

An intuitive way to think about this is that the number space is divided entirely in half (with $0$ acting as a positive number). So, with the standard 8 bits, your numbers range from $-128$ to $127$. There is no positive number from within that set that you could add to a negative number from that set and get a number either above $127$ or below $-128$.

$\endgroup$
  • $\begingroup$ This clears up a lot! however, how can the last two bits be different and there not be overflow? would the length of the sum need to be 1 longer than the largest number added for there to be a overflow? $\endgroup$ – CluelessButCurious May 1 '17 at 13:12
  • $\begingroup$ I'm not sure you're correctly defining overflow. Overflow is when you switch sign inappropriately. A positive plus a negative can be positive or negative, but a negative plus a negative (hypothetically) can only be negative. But with limited bits, we can run into trouble. If we add a negative plus a negative and end up with a positive, obviously something has gone wrong. That's where the word overflow comes in. $\endgroup$ – Ben I. May 1 '17 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.