1’s Complements: Just replaces each one with ‘0’ and each zero with ‘1’. For example: 100101 it’s 1’s complement is 011010
2’s Complements: When we add an extra one with the 1’s complements of a number we will get the 2’s complements. For example: 100101 it’s 1’s complement is 011010 and 2’s complement is 011010+1 = 011011 (By adding one with 1's complement)
Why we need the complements of a number
One reason of it is to represent the negative number. In binary system we use ‘1’ for negative number and ‘0’ for positive number. We represent the sign of a number using an extra bit at the extreme left of the number. For example 0,101 is +5 and 1,011 is -3.
And the most important reason is that, if we can evolve only one convention for representing positive and negative numbers which would allow us to use one basic procedure for both addition and subtraction. If the procedure is same, we could use a single electronic circuit to implement both addition and subtraction. A convention for representing negative numbers which allows this is the ‘complement representation’ of numbers.
2’s Complement
With n bits we can represent 2n number. For example if we take 4 bits then 24 = 16, so with 4 bits we can represents 16 different numbers(0 to 15 here.)
From the above image we can see that we have divided the total range (0-15) into two parts. one is 0001(1) to 0111(7) and another is 1001(9) to 1111(15). Now we use the first seven combinations of 4 bits for representing positive numbers 1 to 7 (See the above image). And we reserve the other seven combinations for representing the negative number -1 to -7. This images actually as the following…
Let’s see with formula
The 2’s complement of a binary number x, which has n bits, is given by (2n-x).. Of the n bits of the complement representation (n-1) bits represent the magnitude. That is 4 bits for the whole system {0000 through 1111(15)} and (n-1) = 4-1 = 3 bits represents the magnitude {000 through 111(8)} Consider the number +3 whose binary representation is 0011. it has four bits. Hence the 2’s complement of +3 is (24-3) which is 13 (1101) (See on the image). Thus 0011’s 2’s complement is 1101
From the above two images we see that the code 1111, which is normally represents 15, is assigned to -1. So 24-1 = 15. Similarly the binary equivalent of 4(0100) is assigned to 12(1100). And 24-4 = 12
1’s Comppement
If we notice the circular image we can see that 0001(1) points to the 1110(-2), Similarly 0110(6) points to 1001(-7). The amazing thing is that 1110(-2) is the 1’s complements of 0001(1) and 1001(-7) is 1’s complement of 0110(6). So the normal thinking is that if we want to find the 1’s complements of 165 is -166, we can say without any doubt. Just increment it by one and make its sign negative.
Are you thinking what about 8!!
Yeah that’s right. Lets see what about the 8(1000) 2’s complements
1’s complements of 8(1000) is 0111, and 2’s complement is 0111+1 = 1000. Which is equivalent 8.