开发者

Binary addition/subtraction

开发者 https://www.devze.com 2023-04-07 03:21 出处:网络
I am having a bit of trouble understanding Carry Flag (CF) and Overflow Flag (OF). Here are some sample problems I am working on:

I am having a bit of trouble understanding Carry Flag (CF) and Overflow Flag (OF).

Here are some sample problems I am working on:

1.  1011 1111    2.  1111 0111    3.  0111 1110  -->   0111 1110
  + 1011 0111      + 1101 1101      - 1011 0000  --> + 0100 1111
  ___________      ___________      ___________      +         1
    0111 0110 开发者_StackOverflow社区       1101 0100                       ___________
                                                       1100 1110
  1. The carryout of the sign position is 1 and the carry in to the sign position is 0, so OF = 1?
  2. The carryout of the sign position is 1 and the carry in to the sign position is 1, so OF = 0?
  3. The carryout of the sign position is 0 and the carry in to the sign position is 1, so OF = 1?

I guess I am having trouble understanding an unsigned overflow and the appropriate CF value.


Disclaimer: I'm not an expert (or even a user of this level of code :) ).

I believe the carry-flag makes sense for unsigned data, and the overflow-flag makes sense for signed data.

Both will always be generated, but it is up to you to determine if you consider the values unsigned, or two's complement, so it is up to you which flag you pay attention to.

From: http://en.wikipedia.org/wiki/Overflow_flag

Internally, the overflow flag is usually generated by an exclusive or of the internal carry into and out of the sign bit. As the sign bit is the same as the most significant bit of a number considered unsigned, the overflow flag is "meaningless" and normally ignored when such numbers are added or subtracted.

The sign bit is the most significant bit (the one farthest left).

Exclusive or (XOR) is:

  • If neither: 0
  • If either: 1
  • If both: 0

Carry-in to the sign bit is when the 2nd most significant bits, when added, produce a value to be carried over to the next column.

Carry-out is whether carry must be done when adding the most significant bits (the sign bits, if the numbers are two's complement) together.

XOR those two values, and you should end up with the value for your overflow flag after a given addition.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号