简体   繁体   English

8位无符号数加减溢出

[英]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. 由于MSB中的1,它导致溢出。 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) (应用2的补码后)

   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. 因此,结果应为“无溢出”,因为最后没有执行1。 However, the answer says "overflow". 但是,答案为“溢出”。 Could you please tell me why? 你能告诉我为什么吗?

I'd probably call this underflow not overflow . 我可能称这种下溢不会溢出

Think about it like this. 这样想吧。 You are trying to compute xy . 您正在尝试计算x - y Using two's complement you are implementing this as x + (2 Ny ) = 2 N + ( xy ). 使用二进制补码,您将其实现为x +(2 N - y )= 2 N +( x - y )。 So the result will only correctly represent ( xy ) if there is an overflow carry bit 2 N that fell off the left. 因此,如果溢出的进位2 N落在左边,则结果只能正确表示( x - y )。 Otherwise the result you get is the two-s complement representation of a negative subtraction result. 否则,您得到的结果是负减法结果的2补码表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM