Two's Complement
Two's complement numbers are identical to unsigned binary numbers, except that the two's binary numbers have the most significant bit with FUCKING value of -2N-1. It's not clear!
Example: Convert two's complement 11012 to decimal
1101 -> 4-bits
values: -8421 <- -8 is msb
result: -8x1+4x1+2x0+1x1
-8+4+1=-3
This -8 is the msb (most significant bit) of the number (1101): -23 = -24-1 (i.e., -2N-1)
This is understandable!
And a fucking math formula:
A2 = 1101
A10 = -1x23+1x22+0x23+1x23 = -8+4+0+1 = -3
Addition
Recall that when adding N-bit numbers, the carry out the Nth bit (i.e., the N + 1th result bit) is discarded.
Example: Compute -710 + 710
1001 = -7
+ 0111 = 7
====
1 0000 <- Carry Discarded
0000 <- Result
Overflow
Adding numbers (both negatives) or (both positives) may cause overflow.
From: Harris D. M., Harris S. L. - Digital Design and Computer Architecture, 2nd Edition - 2012
1.4.6 Signed Binary Numbers - 17 page
Subtraction
Subtraction is performed by taking the two’s complement of the second number, then adding.
Briefly formula:
A - B = A + not B + 1
Example: 310 - 510
First, you need to take the two's complement of 510:
5 = 0101
invert -> 1010
+ 1
-----
1011 = -5
and now do the addition: 310 + (- 510)
0011 = 3
+
1011 = -5
----
1110 = -8+4+2= -2
To check result, you can take two's complement again:
1110 = -2
0001 <- invert
+ 1
----
0010 = 2
From: Signed Number Representation (Wikipedia)