1
$\begingroup$

I'm having trouble with a fairly simply concept, signed binary arithmetic. I know how to do 1's and 2's complement, convert from binary, decimal, octal, and hex, multiply and add bytes and bits, but for some reason this is stumping me:

Take 77 - 99 + 44 for example.

    77 > 01001101
 -(99) > 01100011
         10011100
         +      1   2's complement
         10011101
    44 > 00101100

At this point, I thought I could just take the sum of 77 and the 2's complement of 99, but I feel like I'm going in the wrong direction after completing that step and then adding 44, like below:

   77 >   01001101
-(99) > + 10011101
        = 00000010
   44 > + 00101100 (cut off ending 1, past 8)
        = 00101111

What am I missing? Thanks

Update: I've followed this example, and it shows that when dealing with a 1 (negative) sign bit in a given result, to do another 2's complement with the result, but I am not getting that result.

$\endgroup$
  • 1
    $\begingroup$ In your example adding the $77$ and $-99$ the third column ($8$'s place) carries, so you have three $1$'s to add in the fourth column. Then the fourth column carries (with sum one) and the fifth column carries (with sum zero). There is no carry in the sixth, and the seventh and eighth are $0+1=0$. You add it like regular binary. $\endgroup$ – Ross Millikan Sep 10 '13 at 3:04
  • $\begingroup$ Yeah I realized I was completing messing up with binary addition @RossMillikan, thanks $\endgroup$ – Ben Sewards Sep 10 '13 at 3:15
3
$\begingroup$
  • $+77 = 0~1~0~0~1~1~0~1$
  • $-99 = 1~0~0~1~1~1~0~1$

Adding these produces $-22$, that is:

  • $-22 = 1~1~1~0~1~0~1~0$

Now, we want to add that to $+44$:

  • $+44 = 0~0~1~0~1~1~0~0$

Adding these produces:

  • $-22 + 44 = 1~0~0~0~1~0~1~1~0$

Discard the overflow (MSB) and we are left with the result $= 22$.

$\endgroup$
  • $\begingroup$ I guess I'm doing binary addition wrong because I'm just not getting -22.. When you have 1 + 1 = 2 = 10 binary, you place 0 and carry the 1. When you have 1 + 1 + 1 = 3 = 011, I thought you place 0 and carry each 1 two the next 2 places? Update: Confirmed, I've been doing it wrong. $\endgroup$ – Ben Sewards Sep 10 '13 at 2:59
  • $\begingroup$ I lined up the bits so you can easily follow along. Do one operations at a time. So, do $77-99$ and see if you get the bits I show. Recall, when you add $1+1 = 0$ with a carry bit. Clear? $\endgroup$ – Amzoti Sep 10 '13 at 3:00
  • $\begingroup$ Thanks for making me realize that -.- $\endgroup$ – Ben Sewards Sep 10 '13 at 3:03
  • $\begingroup$ You are very welcome and hopefully you worked it out! Regards $\endgroup$ – Amzoti Sep 10 '13 at 3:04
  • $\begingroup$ Good ole binary! + 1 $\endgroup$ – Namaste Sep 11 '13 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.