Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2's complement example, why not carry?

I'm watching some great lectures from David Malan (here) that is going over binary. He talked about signed/unsigned, 1's compliment, and 2's complement representations. There was an addition done of 4 + (-3) which lined up like this:

0100
1101 (flip 0011 to 1100, then add "1" to the end)
----
0001

But he waved his magical hands and threw away the last carry. I did some wikipedia research bit didn't quite get it, can someone explain to me why that particular carry (in the 8's ->16's columns) was dropped, but he kept the one just prior to it?

Thanks!

like image 709
Alex Mcp Avatar asked Nov 02 '09 20:11

Alex Mcp


2 Answers

The last carry was dropped because it does not fit in the target space. It would be the fifth bit.

If he had carried out the same addition, but with for example 8 bit storage, it would have looked like this:

00000100
11111101
--------
00000001

In this situation we would also be stuck with an "unused" carry.

We have to treat carries this way to make addition with two's compliment work properly, but that's all good, because this is the easiest way of treating carries when you have limited storage. Anyway, we get the correct result, right :)


x86-processors store such an additional carry in the carry flag (CF), which is possible to test with certain instructions.

like image 171
Magnus Hoff Avatar answered Oct 15 '22 03:10

Magnus Hoff


A carry is not the same as an overflow

In the example you do have a carry out of the MSB. By definition, this carry ends up on the floor. (If there was someplace for it to go, then it would not have been out of the MSB.)

But adding two numbers with different signs cannot overflow. An overflow can only happen when two numbers with the same sign produce a result with a different sign.

like image 21
DigitalRoss Avatar answered Oct 15 '22 04:10

DigitalRoss