1
$\begingroup$

I'm sorry about this, but I'm at my wit's end. I'm trying do to an incredibly simple calculation. I want to calculate 4 - 7 using the two's complement method for signed binary numbers with a 4 bit register. I've been doing this calculation over and over again for an hour and I can't get the right answer no matter how hard I try and I can't figure out why.

4 = 0100 7 = 0111

2's complement of 7 = 1000 + 1 = 1001

0100 + 1001 = 1101 = -5

This is quite obviously wrong. The answer should be 1011 = -3

I found a handful of resources online that say that the two's complement of seven is 0111, which makes the math work but has nothing to do with the method as I learned it. I feel like I'm missing something painfully obvious but I don't have a clue what it is and it's about to drive me mad. Please help me, kind nerds of math.stackexchange!

  • 1
    1101 **is** -3. The rest of your computation is OK.2017-01-25
  • 0
    How is that possible? 0011 is 3. Do I just not know how signed integers binary works?2017-01-25
  • 0
    You computed -7 all right. Follow the same procedure, or the one I'm currently typing up in my answer.2017-01-25
  • 0
    The rule for negating in 2's complement is "flip and add one". Flipping 3 = 0011 gives 1100 and then adding 1 gives 1101. Flipping 7 = 0111 gives 1000 and adding 1 gives 1001.2017-01-25

1 Answers 1

1

Here's a handy way to take the two's complement of a binary number. Start from the right and copy all digits up to and including the rightmost $1$. Then flip all other bits. It's not difficult to see that this is equivalent to flip and add $1$, but this method is faster and less error-prone.

Applied to your examples, this method tells us that 1101 is the two's complement of 0011. We can also flip and add 1. We get 0010+1= 0011. Same result, as expected.

  • 0
    So you're saying the answer will be in two's complement form as well?2017-01-25
  • 0
    Yes, most definitely. That's how the vast majority of general-purpose computers work.2017-01-25
  • 0
    Aaah, that's what I was missing! I knew it was some obvious detail like that. And looking at it now, all the examples I dredged up on the internet were with equations that yielded positive answers. That makes sense. Thank you!2017-01-25
  • 0
    You're welcome. BTW, when they say that the two's complement of seven is 0111, they either mean that the *representation* of 7 in two's complement is 0111 or they really don't know what they are talking about.2017-01-25