In mathematics, two’s complement is used frequently and used integers 0 and 1 only like the binary numbers. Two’s complement is a general method to represent the positive and negative numbers also to perform arithmetic operations like addition and subtraction.
In this post, we will learn about two’s complement, how to convert decimal and binary into two’s complement, and perform arithmetic operations on two’s complement with a lot of examples.
What is two’s complement?
According to Wikipedia:
“Two’s complement is a mathematical operation on binary numbers and is an example of a radix complement. It is used in computing as a method of signed number representation.”
Two’s complement is usually used to encode the positive and negative numbers easily in computer language. We can convert binary and decimal numbers into two’scomplements easily. In simple words, adding one into the one’s complement gives two’s complement.
Where one’s complement is the transpose of the given binary number.
How to convert binary and decimal into two’s complements?
To convert the binary number and decimal numbers into two’s complement, you have to follow the rules of this method. For the conversion of binary and decimal numbers into two’s complement, you can use a 2’s complement calculator.
Using this calculator.
Step 1: You can select the option like binary or decimal to convert them into two’s complements.
Step 2: You can select the bit.
Step 3: Put your problem and click the “calculate” button.
Step 4: A step-by-step result will show below the calculate button.
Binary to two’s complement
To convert a binary number into two’s complement, invert all the numbers of the given binary number and then add one in the least significant bit. By following this process any binary number can be converted into 2’s complement.
Let’s understand this concept by taking some examples.
Example 1
Convert 100111001 into 2’s complement.
Solution
Step 1:Write the given binary number.
100111001
Step 2:Now invert the numbers like zero into one and one into zero.
011000110
Step 3:now add one to the least significant bit of the inverted binary number.
011000110 + 1
011000111
Hence 11000111 is the 2’s complement of the given binary number.
Example 2
Convert 1011010 into 2’s complement.
Solution
Step 1:Write the given binary number.
1011010
Step 2:Now invert the numbers like zero into one and one into zero.
0100101
Step 3:now add one to the least significant bit of the inverted binary number.
0100101 + 1
0100110
Hence 0100110 is the 2’s complement of the given binary number.
Here is a list of some binary numbers that are converted into two’s complement for 8-bit.
Binary numbers | Two’s complement |
11110100 | 00001100 |
11000100 | 00111100 |
11111111 | 00000001 |
11001100 | 00110100 |
00110011 | 11001101 |
10000000 | 10000000 |
111000 | 11001000 |
11000001 | 00111111 |
10000001 | 01111111 |
10010011 | 01101101 |
00000000 | 0 |
Decimal to two’s complement
To convert a binary number into 2’s complement, first, convert the given decimal number into binary number and the convert into two’s complement. The decimal number can be positive or negative.
Two’s complement of the positive integer is taken by converting the decimal number into binary and then inverting the number and adding 1 to the least significant bit.While for negative integer, first calculate the two’s complement according to the positive integer and then repeat the process of 2’s complement.
Let’s understand this concept by taking some examples.
Example 1: For a positive integer
Convert 27 into two’s complement.
Solution
Step 1:Take the given decimal integer.
27
Step 2:Now convert the given decimal number into binary number.
2 | 27 |
2 | 13 – 1 |
2 | 6 – 1 |
2 | 3 – 0 |
2 | 1 – 1 |
Hence, the binary number of 27 is 11011.
Step 3:Now invert the calculated binary number.
11011 becomes 00100 after inverting.
Step 4:Now add 1 in the least significant bit of the inverted binary number.
00100 + 1 = 00101
Hence 00101 is the 2’s complement of the given decimal number.
Example 2: For a negative integer
Convert -39 into two’s complement for 8-bit.
Solution
Step 1:Take the given decimal integer.
-39
Step 2:Now convert the given decimal number into binary number without taking the negative sign.
2 | 39 |
2 | 19 – 1 |
2 | 9 – 1 |
2 | 4 – 1 |
2 | 2 – 0 |
2 | 1 – 0 |
Hence, the binary number of 36 is 100111.
For 8-bit binary number becomes 00100111
Step 3:Now invert the calculated binary number.
00100111 becomes 11011000 after inverting.
Step 4:Now add 1 in the least significant bit of the inverted binary number.
11011000 + 1 = 11011001
Hence 11011001 is the 2’s complement of 39.
Step 5:Now repeat the process by inverting the result of 39 and adding one to get the result of -39.
11011001 becomes 00100110 after inverting.
00100110 + 1
00100111
Hence 00100111 is the 2’s complement of -39.
Example 3
Convert -58 into two’s complement for 8-bit.
Solution
Step 1:Take the given decimal integer.
-58
Step 2:Now convert the given decimal number into binary number without taking the negative sign.
2 | 58 |
2 | 29 – 0 |
2 | 14 – 1 |
2 | 7 – 0 |
2 | 3 – 1 |
2 | 1 – 1 |
Hence, the binary number of 58 is 111010.
For 8-bit binary number becomes 00111010
Step 3:Now invert the calculated binary number.
00111010 becomes 11000101 after inverting.
Step 4:Now add 1 in the least significant bit of the inverted binary number.
11000101 + 1 = 11000110
Hence 11000110 is the 2’s complement of 58.
Step 5:Now repeat the process by inverting the result of 59 and adding one to get the result of -58.
11000110 becomes 00111001 after inverting.
00111001 + 1
00111010
Hence 00111010 is the 2’s complement of -58.
Here is a list of some positive and negative integers that are converted into two’s complements.
Positive integer | 2’s complement | Negative integer | 2’s complement |
6 | 1111 1010 | -6 | 0000 0110 |
12 | 1111 0100 | -12 | 0000 1100 |
18 | 1110 1110 | -18 | 0001 0010 |
21 | 1110 1011 | -21 | 0001 0101 |
29 | 1110 0011 | -29 | 0001 1101 |
35 | 1101 1101 | -35 | 0010 0011 |
40 | 1101 1000 | -40 | 0010 1000 |
55 | 1100 1001 | -55 | 0011 0111 |
60 | 1100 0100 | -60 | 0011 1100 |
70 | 1011 1010 | -70 | 0100 0110 |
120 | 1000 1000 | -120 | 0111 1000 |
125 | 1000 0011 | -125 | 0111 1101 |
Summary
Now you can grab all the basics of two’s complement by learning this article. Once you grab the basics of this topic, you can easily solve the problem related to 2’s complement.