Peter Hermann
Peter Hermann

Reputation: 23

8 bit unsigned number addition and subtraction overflow

I have trouble understanding when overflow occurs in unsinged addition and subtraction. For example,

   1 1 1 1 0 0 0 0 
+  0 0 1 1 1 0 0 0 
  __________________
   0 0 1 0 1 0 0 0 

Because of the 1 in the MSB, it results as overflow. I understand this problem, but when it gets to subtraction, I have trouble determining when it is overflow

For example,

   0 0 0 0 0 0 0 1               
-  0 0 0 0 0 0 1 1   

(After applying 2's complement)

   0 0 0 0 0 0 0 1 
+  1 1 1 1 1 1 0 1

   1 1 1 1 1 1 1 0 

Therefore the result should be "no overflow" because there is no 1 carry out in the end. However, the answer says "overflow". Could you please tell me why?

Upvotes: 1

Views: 853

Answers (1)

MvG
MvG

Reputation: 60908

I'd probably call this underflow not overflow.

Think about it like this. You are trying to compute xy. Using two's complement you are implementing this as x + (2Ny) = 2N + (xy). So the result will only correctly represent (xy) if there is an overflow carry bit 2N that fell off the left. Otherwise the result you get is the two-s complement representation of a negative subtraction result.

Upvotes: 1

Related Questions