[Math] Signed binary subtraction using two’s complement method

binary

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!

Best Answer

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.