0
votes

Ladies and gentlemen, I have successfully been able to understand adding, etc. for unsigned binary numbers. However, this Two's complement has me beat.. Let me explain with some examples.

Example practice problem, perform each arithmetic using 8-bit signed integer storage system, under two's complement:

  1111111
  01100001
+ 00111111
----------
  10100000    <== My Answer (idk if right / wrong), but i think right.

So it doesn't exceed 8 bits, but we have changed (positive + positive) = negative. That has to be an overflow, because the sign is changing, right? (I never understood what the carry in MSB and carry out MSB's were).

The reallllly tricky part for me are the following equations: (negative + negative) which in reality is equal to (negative - positive).

       111111
      10111111
    + 10010101
    ----------
  1 | 01010100

So I think this should be wrong because when we discard the overflow bit (the 1 that is way out in left field), it turns the 8-bit representation into a positive number, when it should be a negative. SO this would entail an overflow, no?

The following equation is similar:

      1111
     10001110
   + 10110101
   ----------
 1 | 01000011

Understandably, if we were working with 16-bit, etc. Then these wouldn't be overflows, because the signs aren't changing, the math is correct. But since when we store the 8-bit representation of these numbers, we lose the MSB, that would flip the signs.

But one thing that I noticed about my theory is, whenever adding two negative numbers, the MSB's will obviously always be 1, therefore you will always have a carry, which means you would always have an overflow.

** I think the more logical conclusion is that I am forgetting to convert the second negative to a positive or something prior to adding them, or something along those lines. But I've tried youtube and various research online. And TBH, my professor is terrible with the whole "communication" thing.. I would appreciate any help the community can give, so I can push past these problems and onto harder material XD.

2

2 Answers

2
votes

Yes, if you discard a 1 carry bit then that signals overflow. Don't worry about overflow. It is correct to discard the carry bit.

But since when we store the 8-bit representation of these numbers, we lose the MSB, that would flip the signs.

It's important not to think of discarding a 1 bit as flipping the sign. First, discarding a carry bit is not the same as flipping the sign bit. You can discard the carry bit from a negative result and still end up with a negative answer. For instance:

    1111111  
    11111111   (-1)
  + 11111111   (-1)
    --------
1 | 11111110   (-2)

The final 1 carry bit is discarded, but the answer's sign doesn't flip.

Second, even if you're thinking of flipping (as opposed to discarding) the sign bit, it's not good to think of that as flipping the sign. In sign magnitude representation flipping the sign bit will flip the sign of the number. But in one's and two's complement, negation is more than just flipping the leftmost bit. If you just flip the bit you get a very different number. Yes, it has the opposite sign, but it's not the same number.

           Sign Mag.  |  One's Compl.  |  Two's Compl.
01111111 =    127     |      127       |       127
11111111 =   -127     |      -0        |       -1
1
votes

Yes, your math is correct.

The elegance of two's compliment is that addition "just works" without any special considerations for the sign bit. The reason both of those subtractions underflow is because the magnitudes of the numbers are already quite large.

Let's do the last two questions in decimal:

   (-65)
+ (-107)
--------
  (-172) which underflows to 84.

  (-114)
+  (-75)
--------
  (-189) which underflows to 67.

The lowest signed 8-bit value is -128, so both of them underflow.