Two's complement: some mysteries solved (Part 1) ******* Note: by definition, in 2C, multiplying N by -1 is the same as 2^N_unsigned - N ******* Below, I use 4 bits specifically to make the examples easier. Replace with N and the conclusions are the same. ******* Why is flipping the bits and adding 1 the same as subtracting from 2N: observation: subtracting from all 1's complements the number. 1111 - 1010 ---- 0101 n^4 is 10000 n^4 - 1 is 1111 10000 - 1 = 1111 10000 = 1111 + 1 2C(-A) = 10000 - 2C(A) substituting in: 2C(-A) = (1111 + 1) - 2C(A) = 1111 - 2C(A) + 1 = FLIP the bits and add 1. ******** Why should you throw away the carry when you add two 2C numbers? This assumes NO OVERFLOW We need to consider 4 cases: both numbers positive (can't have a carry) both numbers negative one pos, one neg, the pos one has larger abs value one pos, one neg, the pos one has smaller abs value If both numbers are positive, you can't generate a carry. Suppose both are negative, suppose they are -X and -Y in sign magnitude. -X + - Y in 2C: 2^n - X + 2^n - Y = 2*2^(n) + -X + -Y example: -1 + -1 1111 +1111 ----- 11110 16 + 8 + 4 + 2 = 30 and 2 * 2^4 + -1 + -1 = 32 + -1 + -1 = 30 check. Ok, the 2C representation of -X + -Y (which is what we are trying to produce) is: 2^N - (X + Y) But we produced 2*2^(N) + -X + -Y We have an extra 2^N...which is the carry! So, throw it out. **If we have X + -Y and |X| >= |Y| (so the result is positive): our addition gives us: X + (2^N - Y) but what we want is X - Y. 2^N is the carry, so throw it away. E.g., 7 + -5: want the answer to be |7| - |5| = 2. *** If we have X + -Y and |X| < |Y| (so result is negative) You won't have a carry out: bit Xn-1 is 0 and Yn-1 is 1 the only way to have a carry out is to have a carry in from n-2 if you just look at the bit patterns of 2C, this isn't possible for the case we are considering - i'm not going to prove this. The idea is that as the absolute values of negative numbers gets larger, the 1's move to the right in 2C; for positive numbers, as they get larger, the 1's move to the left. E.g. 1 + -2 0001 1110 -------- No carry.