Two’s complement is a mathematical operation on binary numbers and is an example of radix complement (10’s complement). It is majorly used in computing. In this post, we will discuss 2’s complement definition, rules to evaluate 2s complement, and a lot of examples to perfectly understand the concept.
What is two’s complement?
Two’s complement is a method of placing integers so that common math problems become simple to implement.
Two’s complement is a technique that encodes positive and negative numbers in a binary number representation. In case of positive numbers 0 is in the most significant position while in case of negative 1 is in the most significant position.
To get two’s complement of binary number, first convert all the 0’s to 1 and all the 1’s into 0 in other words transpose that binary number or invert that binary number. After inverting the binary number add 1 to LSB (Least significant bit).
How to find two’s complement?
In this section, we will use several examples to depict the calculation process of 2’s complement. However, you can also find 2s complement using online 2’s complement calculator.
Example:
Convert 00110010 into two’s complement?
Solution:
First of all, keep the bits in mind each bit system has different rules.
In the given example we have 8-bits.
Firstly, invert the binary number
00110010 change into 11001101 after inverting
Now add 1 in the LSB.
The two’s complement of 00110010 is 11001110.
Example:
Convert 0111 into two’s complement?
Solution:
First of all, keep the bits in mind each bit system has different rules.
In the given example we have 4-bits.
Firstly, invert the binary number
0111 change into 1000 after inverting
Now add 1 in the LSB.
1000 + 1 = 1001
The two’s complement of 0111 is 1001
Example:
Convert 9 to Two’s complement
Solution:
Step 1:
First of all, convert the decimal number into binary number.
We have
(9)2 = (1001)2
Step 2:
Now invert the binary number
1001 becomes 0110
Step 3:
Now add 1 to LSB
0110 + 1 = 0111
The two’s complement of 9 is 0111
Example:
Convert 11 to Two’s complement
Solution:
Step 1:
First of all, convert the decimal number into binary number.
We have
(11)2 = (1011)2
Step 2:
Now invert the binary number
1011 becomes 0100
Step 3:
Now add 1 to LSB
0100 + 1 = 0101
The two’s complement of 11 is 0101.
Rules of two’s complement
We generally find the two’s complement of
- Binary
- Decimal
Binary to two’s complement:
To get two’s complement of binary number, first convert all the 0’s to 1 and all the 1’s into 0 in other words transpose that binary number or invert that binary number. After inverting the binary number add 1 to LSB (Least significant bit).
Example:
Convert 00110010 into two’s complement?
Solution:
First of all, keep the bits in mind each bit system has different rules.
In the given example we have 8-bits.
Firstly, invert the binary number
00110010 change into 11001101 after inverting
Now add 1 in the LSB.
The two’s complement of 00110010 is 11001110.
Example:
Convert 0111 into two’s complement?
Solution:
First of all, keep the bits in mind each bit system has different rules.
In the given example we have 4-bits.
Firstly, invert the binary number
0111 change into 1000 after inverting
Now add 1 in the LSB.
1000 + 1 = 1001
The two’s complement of 0111 is 1001
Decimal to two’s complement:
The best way to discover two’s complement conversion is to start with small numbers of bit e.g., let’s start with 4-bits which can represent 16 decimal number range -8 to 7.
-
Positive integer
Example:
Convert 5 to Two’s complement
Solution:
Step 1:
First of all, convert the decimal number into binary number.
We have
(5)2 = (0101)2
Step 2:
Now invert the binary number
0101 becomes 1010
Step 3:
Now add 1 to LSB
1010 + 1 = 1011
The two’s complement of 5 is 1011
Example:
Convert 7 to Two’s complement
Solution:
Step 1:
First of all, convert the decimal number into binary number.
We have
(7)2 = (0111)2
Step 2:
Now invert the binary number
0111 becomes 1000
Step 3:
Now add 1 to LSB
1000 + 1 = 1001
The two’s complement of 7 is 1001.
-
Negative integer
Negative number are formed by taking the positive number complementing it (bit-by-bit inversion) and adding 1 to the results
Example:
Convert -7 to Two’s complement
Solution:
Step 1:
First of all, convert the positive number 7 into binary number.
We have
(7)2 = (0111)2
Step 2:
Now invert the binary number
0111 becomes 1000
Step 3:
Now add 1 to LSB
1000 + 1 = 1001
We have the two’s complement of 7 is 1001
Now invert the number
1001 becomes 0110
Now add 1 to LSB
0110 + 1 = 0111
Hence the two’s complement of -7 is 0111
Table of 4-bit two’s complement values
Decimal | Two’s Complement |
-8 | 1000 |
-7 | 1001 |
-6 | 1010 |
-5 | 1011 |
-4 | 1100 |
-3 | 1101 |
-2 | 1110 |
-1 | 1111 |
0 | 0000 |
1 | 0001 |
2 | 0010 |
3 | 0011 |
4 | 0100 |
5 | 0101 |
6 | 0110 |
7 | 0111 |
In case of 8-bit we have the range -128 to 127.
In case of 12-bit we have the range -2048 to 2047
In case of 16-bit we have the range of -32,768 to 32,767
Table of 8-bit two’s complement some values
Decimal | Two’s Complement | Decimal | Two’s Complement |
0 | 0000 0000 | ||
1 | 1111 1111 | -1 | 0000 0001 |
2 | 1111 1110 | -2 | 0000 0010 |
3 | 1111 1101 | -3 | 0000 0011 |
4 | 1111 1100 | -4 | 0000 0100 |
5 | 1111 1011 | -5 | 0000 0101 |
6 | 1111 1010 | -6 | 0000 0110 |
7 | 1111 1001 | -7 | 0000 0111 |
8 | 1111 1000 | -8 | 0000 1000 |
9 | 1111 0111 | -9 | 0000 1001 |
10 | 1111 0110 | -10 | 0000 1010 |
11 | 1111 0101 | -11 | 0000 1011 |
Read Also