In the binary number system, twos complement is a method for representing “signed integers”. You might ask what a signed integer is? Well, a signed integer is a positive or negative number. In “twos complement we use the digit with the greatest value (the leftmost digit) to determine the sign of a number. A number with a leftmost digit (often called the most significant bit) of 1 is negative. Meanwhile a number with a 0 as the most significant bit is a positive number. You can look at some examples below to learn how this works.
Two’s complement works as a system that can be used to represent both positive and negative integers when working with binary numbers. To find the two’s complement of any binary number you always follow the same process. First, you invert all the bits. This requires simply flipping all the os to 1s and then all the 1s to 0s. Then importantly you need to add 1 to the number to get the two’s complement.
In a binary system two’s complement only works with a representable range. For example, in a 4-bit system, the representable range is -8 to 7, while in an 8-bit system, the range is -128 to 127. If you go outside the range then you risk integer overflow or underflow. Integer overflow means that when you add to positive numbers the result “wraps around” and you end up getting a negative number as a result. Integer underflow implies the opposite, when you add two negative numbers or subtract a large positive binary number and the result is less than the minimum end of the range you get a positive number. This is a limitation of the two’s complement system you should be aware of when working with it.
To find the two’s complement of a number let’s follow some really simple steps:
The results then represent the negative version of that number i.e (1011 = -5)
Let’s look at some examples of how to perform two’s complement. Remember you can always check these results with the above two’s complement calculator.
Binary | Decimal | Binary | Decimal |
---|---|---|---|
0111 | 7 | 1001 | -7 |
0110 | 6 | 1010 | -6 |
0101 | 5 | 1011 | -5 |
0100 | 4 | 1100 | -4 |
0011 | 3 | 1101 | -3 |
0010 | 2 | 1110 | -2 |
0001 | 1 | 1111 | -1 |
0000 | 0 | 1000 | -8 |
You can convert a two’s complement number to decimal by following these simple steps:
Zero in two’s is always 0. In a four-bit system that is commonly represented as 0000 while in an 8-bit system that is commonly represented as 00000000.
An addition with two’s complement works exactly the same as a normal binary addition. To add binary numbers add right to left bit by bit. Remeber the binary addition rules that 1 + 1 = 10, 1 + 0 = 1 and 0 + 0 = 0. Make sure that you carry values between places (bits) as required. The most important difference to look out for here is the overflow. To check this you can determine if the carry into the most significant bit (right-most digit) is different from the carry out of the most significant bit (right-most digit). If so you know that an overflow has occurred.
Subtraction with two’s complement can work just like addition! For example, say you want to subtract 5 from 7 (7 - 5) in binary (so 00000111 - 00000101). The first thing to do is find the two’s complement of 5 (Follow the steps above the answer should be 11111010). Now we add 7 and -5 (or 00000111 + 11111011). You can always use our binary addition calculator for this! The result you should get for this example is 00000010 which is 2 (and we know 7 -5 = 2). How neat is that!