0
0xfb-0xfa

I have first turn to binary

0xfb=11111011

0xfa=11111010

-0xfa=00000101+1=00000110

Now 11111011+00000110=100000001 which is underflow, but it surely incorrect, where did I get it wrong?

gbox
  • 809
  • 1
  • 8
  • 22

1 Answers1

1

It is not clear whether the initial numbers are signed or unsigned. The results will be identical, but the reasoning is slightly different.

Assuming your initial numbers are unsigned, the initial conversion is incorrect. Your data are 8 bits when coded in binary as unsigned. If you want to convert them to 2s complement without truncation, you need an extra bit. So

+0xfb = 0 11111011
+0xfa = 0 11111010
-0xfa = 1 00000110

Now we can compute the addition

        (1)
  0xfb     0 11111011
 -0xfa   + 1 00000110
           ----------
           0 00000001   

We can notice that there is no overflow. There is a carry out, but we can just ignore it in 2s complement. And the sum of a positive and negative number cannot overflow.

Now, as the two MSB are equal, result can be shortened to 8 bits giving the obvious result of 1.

If initial numbers are signed, most your computation is correct. All the operations can be done on 8 bits. And the result will be on 8 bits.

        (1)
  0xfb     11111011
 -0xfa   + 00000110
           ----------
           00000001   

There is a carry out, but no over or underflow. The result is correct.

Alain Merigot
  • 10,667
  • 3
  • 18
  • 31
  • So in 2s complement there will never be overflow? we ignore the the carry out? – gbox Feb 04 '19 at 23:20
  • 1
    Overflow is complex to determine for 2s complement. There is never overflow if operands are of != sign and there may or not be a carry out (that can be ignored). If operands are both >=0, so must be the result and no carry out should happen. If operands are both <0, result must be <0 and there must be a carry out. A global rule is that carry-out==carry-in-at-MSB, otherwise there is an overflow. See for instance https://stackoverflow.com/questions/32805087/how-is-overflow-detected-in-twos-complement – Alain Merigot Feb 05 '19 at 06:16