Definition
Two's Complement
Two’s complement is the most common method of representing signed integers in computer systems. In this system, the most significant bit (MSB) acts as the sign bit:
0indicates a positive number or zero, and1indicates a negative number.To find the two’s complement of a number (i.e., to negate it), invert all its bits and add
1. The primary advantage of this representation is that fundamental arithmetic operations like addition and subtraction function identically for both positive and negative numbers without requiring separate logic circuits.
Examples
The following examples use 4-bit numbers to demonstrate addition. In 4-bit two’s complement, values range from (1000) to (0111). When adding, any carry out of the most significant bit is simply discarded.
Positive + Positive
Adding two positive numbers works just like standard binary addition.
0010 (2)
+ 0011 (3)
-------
0101 (5)Positive + Negative
Adding a positive and a negative number correctly yields the difference. Note how the carry beyond the 4th bit is ignored.
0011 (3)
+ 1110 (-2)
-------
10001 (1, dropping the 5th bit)Negative + Negative
Adding two negative numbers also works naturally, again dropping the overflow bit.
1110 (-2)
+ 1101 (-3)
-------
11011 (-5, dropping the 5th bit)