# Two's complement

• 08-28-2010
matthayzon89
Two's complement
Sorry, I didn't want to start a new thread as this is not DIRECTLY tied to C programming..

I need to understand one’s and two's Complement System Arithmetic.

Here is an example problem:

______________________________________________
Step1: Represent -3 and +5 as 4-bit numbers

So, -3 = 1100 and +5 = 0101

1100 = -3
+ 0101 = +(+5)
-------------------
0001
+ 1
-------------------
0010 = +2

Carry into MSB is 1 and carry out of MSB is 1. No overflow.

__________________________________________________

The only thing I don't understand is how they got "Carry into MSB is 1 and carry out of MSB is 1"

I think I understand overflow but I am not sure. The way I see it, one way of detecting overflow is if you add two positive numbers and end up with a negative result or you are suppose to end up with a negative result and you end up with a positive result that that is considered overflow. Is that correct?

Thank you in advance for the help.
• 08-28-2010
bernt
Quote:

Carry into MSB is 1 and carry out of MSB is 1
MSB = Most significant bit.
In this case adding bit 2^2 (3rd from the right) results in (1+1 = 0 carry 1), and adding the MSB (2^3) results in (1+0 + [carry 1] = 0 carry 1).

Quote:

The way I see it, one way of detecting overflow is if you add two positive numbers and end up with a negative result or you are suppose to end up with a negative result and you end up with a positive result that that is considered overflow. Is that correct?
Yes. However not all numbers are two's complement (read: unsigned integer) so more technically an overflow is when the result of an operation is too big to fit in whatever the answer is supposed to go into.
That is, some bits have to be dropped (the most significant ones) in order to keep the answer to, say, 8 bits.
• 08-28-2010
matthayzon89
Thanks for the reply bernt. I now understand overflow a lot better, however, I am still completely lost when it comes to carry in/out of MSB:/
• 08-29-2010
matthayzon89
can anyone give me some more pointers? I really am lost when it comes to carry/ overflow:/
• 08-29-2010
Elysia
Your math is wrong. -3 is 1101 (because 3 is 0011, invert to 1100, add 1: 1101).

1101
0101
---------
1 0010

The carry bit is the bit next to the MSB. In this case, it's 1 (the one to the far left).
This is the carry out.

What they actually mean by carry in/out to the MSB I don't know.
• 08-29-2010
Mario F.
Carry in/out (or carry input and output) refers to how the carry flag is being used by an instruction. A carry input means the instruction will use the the carry flag. A carry output means the instruction will change the carry flag (whether or not the change actually occurs).

EDIT:

I feel I should emphasize that the above is indeed only for the carry flag (CF). Not for the carry bit. Do not confuse both. The CF is set according to the value of the carry bit, naturally. But some instructions may change the carry bit without that meaning they are carry out instructions. In this group are all instructions where the CF is classified as undefined.

Also, because the carry flag can be used as a borrow flag, one can be pedantic and insist carry out/in instructions do not apply to instructions that use CF as a borrow flag. In this case the terminology should be borrow in/out. Personally I don't think one can be pedantic enough in assembly. So these distinctions make all sense to me. But they can also be useful if one thinks for instance that some processors do make the distinction very clear. See, Carry flag vs. Borrow flag header in http://en.wikipedia.org/wiki/Carry_flag (the last paragraph in that header also lists the unfortunate exceptions and who should be more careful with their terminology)
• 08-29-2010
matthayzon89
Quote:

Originally Posted by Elysia