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
    $\begingroup$ 1101 is -3. The rest of your computation is OK. $\endgroup$ – Fabio Somenzi Jan 25 '17 at 22:51
  • $\begingroup$ How is that possible? 0011 is 3. Do I just not know how signed integers binary works? $\endgroup$ – Runic Jan 25 '17 at 22:55
  • $\begingroup$ You computed -7 all right. Follow the same procedure, or the one I'm currently typing up in my answer. $\endgroup$ – Fabio Somenzi Jan 25 '17 at 22:56
  • $\begingroup$ 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. $\endgroup$ – Rob Arthan Jan 25 '17 at 22:56

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.

  • $\begingroup$ So you're saying the answer will be in two's complement form as well? $\endgroup$ – Runic Jan 25 '17 at 23:04
  • $\begingroup$ Yes, most definitely. That's how the vast majority of general-purpose computers work. $\endgroup$ – Fabio Somenzi Jan 25 '17 at 23:18
  • $\begingroup$ 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! $\endgroup$ – Runic Jan 25 '17 at 23:21
  • $\begingroup$ 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. $\endgroup$ – Fabio Somenzi Jan 25 '17 at 23:24

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.