Two's ComplementThe two's complement system is the most common method of representing signed (+ or -) integers on a computer. A two's-complement system, or two's-complement arithmetic, is a system in which negative numbers are represented by the two's complement of the absolute value. The two's complement of the number behaves like the negative of the original number in most arithmetic, and it can coexist with positive numbers in a natural way. The two's complement of a binary number is obtained by considering the MSB as negative and the rest of the bits as positive. For an N-bit number the two's complement version of it would have the 2N bit as a negative value - all of the others would be positive; summing them would then give you a negative value for the binary number. For example: if we have to find the N's complement of 0110 (decimal 6) it would be 1010. Let me show you why.
How to change a binary number to two's complement
To find out how to work out a two's complement in an even quicker, easy way click here. Normal binary addition
Binary subtraction
a - b = a + (-b)Example sum: Let's do a really simple sum in binary. Add decimal 3 to decimal -5. 3 in binary is 0011 -5 in binary can be worked out using two's complement. Remember that the MSB is the sign bit! 0101 is +5 in binary. To change it to the equivalent of -5 we switch digits and add 1. We therefore get 1011 Okay, now, to do the subtraction sum '3 - 5' we actually perform the addition sum '3 + (-5)' In your head you know that the answer to our simple sum is -2... but the negative numbers look strange to us. Is 1110 really -2? To find out we need to find the positive equivalent of it.
Ignore end-around carry The MSB is the sign bit - therefore a carry into an even-more significant bit would not make sense! If that occurs you just ignore it! For example Let's add two 8-bit numbers using two's compliment: +106 and -2 Ignoring the end-around-carry we get an answer of 0110 1000 which is decimal 104
|
|