What
Used for representing signed binary numbers. Invert bits then add 1. For example, 12 in binary representation:
1 | |
Invert the digits.
1 | |
And add one.
1 | |
Compared with One’s Complement
For One’s Complement, to get the negative counterpart, we only need to invert the digits. Compared to this method, two’s complement has the following pros:
- In One’s Complement 0 has two representations(+0 and -0). For example in two bits,
00->+0,01->+1,10->-1,11->-0. With n bits it can represent 2^n-1 numbers, with 0 being representated twice. In Two’s Complement,00->+0,01->+1,10->-2,11->-1. 0 is only representated once, and n bits can represent 2^n numbers. This is because for 0(taking 2 bits as an example):00invert digits->11add one->100ignore the overflow and just take 2 bits ->00. - When doing arithmetic operations, One’s Complement requires an “add 1” at the end when dealing with negative numbers. For example, 2-1=1 in One’s Complement:
10+10=100taking 2 bits->00, and we need to “add 1” so that00+01=01. But for Two’s Complement, since there is only one representation of 0, we can skip the “add 1” procedure. In this example,10+11=101taking 2 bits->01is exactly the answer we want.
Why Inversion and Adding One Works
Take number “75”(1001011) as an example.
To get the negative counterpart, we subtract 75 from 0 and get:
1 | |
Which is the same as taking the last 8 bits in:
1 | |
Since 100000000 = 11111111 + 1:
1 | |
Note that in binary, when we subtract a number A from a number of all 1 bits, what we’re doing is inverting(~) the bits of A. Thus this operation is equivalent to inverting the number and then add 1.