Originally posted by Seraph
and the 1's complement ?? another way?
So when you represent these numbers
for instance -2 represented as
1101 in 2's complement , you cant actually do any binary calculations to work out the number is 2..
uhh then how are you supposed to know if they just gave you 1101 and said what negative 2's complement number is this representing (in decimal) .. ?
and one more thing is it possible to do say 10 - 12 ?
or -20 - 10 ? using this system
There isn't actually any relationship between 1s and 2s complement. It just so happens that adding 1 to a number in 1s complement will give you 2s complement. There are many different types of complements...
Take -9 for example, in:
signed magnitude = 10001001
1s complement = 11110110
2s complement = 11110111
A even quicker way of getting the 2s complement of a binary number is to leave all the least significant 0s and the least significant 1 unchanged, and then replacing 1s with 0s and 0s with 1s in all the other higher bits.
Eg, 6 is 0110. Ignore the least significant zeros, and the least significant 1, then complement all the other bits => 1010.
-2 in 2s complement is not 1101.
Lets do this one step by step.
2 in binary is 0010
Take the 1s complement (complement all bits), this becomes 1101.
Add 1 to 1s complement to get 2s complement: 1101+1 = 1110
Now, for example, if you wanted to do 3-2, you do not actually need to do any subtraction; you add 3, and the two's complement of 2 togther.
ie, you
add 0011 and 1110
0011 + 1110 = 10001
Ignore the carry and you get 0001. Positive 1, which is 3-2.
Now lets do your example of 10-12.
10 in binary is 01010
12 in binary is 01100
Take the 2s complement of 12
01100=> 10011 => 10011+1 => 10100
Now add binary 10, and 12 in 2s complement
01010
10100 +
11110 is the result.
The most signifcant bit is a 1, indicating the subtrahend was bigger than the minuend and resulted in a negative result. This means, the current result is really the result in 2s complement.
To correct this (called 2s complement correction), take the 2s complement of this number:
11110 => 00001 => 00001 + 1 => 00010
This is +2, however, since the original problem was that the result was -ve two, you have to keep in mind that this is really -ve 2.
You can rewrite this in signed magnitude: 10010 to indicate it is -2.
Notice in the first example, 3-2, the initial result was 0001, which does not have a 1 in the MSB, indicating the result is positive and does not need to go through 2s complement correction.
Hope that helps.