1's complement and 2's complement are two ways of representing signed (positive and negative) integers in binary. To form the 1's complement of a number you invert every bit; to form the 2's complement you invert every bit and then add 1. Almost every modern processor and digital design uses 2's complement, because it has a single zero and lets the same adder handle both addition and subtraction.
Key takeaways
- 1's complement: negate a number by inverting all its bits. It has two zeros (+0 = 0000 and -0 = 1111) and needs an end-around carry when adding.
- 2's complement: negate a number by inverting all its bits and adding 1. It has one zero, and the carry out of the most significant bit is simply discarded.
- Range for n bits: 2's complement covers -2n-1 to 2n-1 - 1 (for 4 bits, -8 to +7); 1's complement covers -(2n-1 - 1) to 2n-1 - 1 (for 4 bits, -7 to +7).
- Overflow: in both systems, adding two numbers of the same sign and getting a result of the opposite sign means overflow. In 2's complement hardware this is the XOR of the carry into and the carry out of the sign bit.
Signed number representation in digital logic
In digital logic design, one of the ways to represent negative numbers is by using signed number representation. One of the commonly used signed number representation methods is 2's complement, while the other is 1's complement. In both, the most significant bit (MSB) acts as the sign bit: 0 for positive numbers and 1 for negative numbers. Positive numbers look the same in both systems; only the negative numbers differ.
Here are the differences between the two.
1's complement
In 1's complement, negative numbers are represented by taking the bitwise complement of the corresponding positive number. For example, to represent -5 in 1's complement, we take the bitwise complement of 5:
5 = 0101
-5 = 1010 (1's complement of 5)
To get the decimal value of a number represented in 1's complement, we need to determine whether the number is negative or positive. If the most significant bit (MSB) is 1, the number is negative. To get the magnitude of the negative number, we take the 1's complement of the binary number and then convert it to decimal. For example, 1010 has MSB 1, so it is negative; inverting it gives 0101 = 5, so 1010 represents -5.
The disadvantage of using 1's complement is that it has two representations of zero (+0 = 0000 and -0 = 1111), and addition needs an extra step: any carry out of the MSB must be added back into the least significant bit (the end-around carry). This makes the adder and the zero test slightly more complicated than in 2's complement.
2's complement
In 2's complement, negative numbers are represented by taking the 2's complement of the corresponding positive number. To get the 2's complement of a binary number, we first take the 1's complement of the number and then add one to it. For example, to represent -5 in 2's complement:
5 = 0101
1's complement of 5 = 1010
2's complement of 5 = 1011
-5 = 1011
To get the decimal value of a number represented in 2's complement, we can use the same weighted sum as for unsigned numbers, except that the MSB carries a negative weight of -2n-1. Alternatively, if the MSB is 1, the number is negative, and we take the 2's complement of the binary number to find its magnitude, then convert it to decimal.
The advantage of using 2's complement is that there is only one representation of zero, and addition and subtraction use exactly the same binary adder as unsigned numbers, with no end-around carry.
Shortcut for finding the 2's complement
Scan the number from the right (LSB). Copy every bit up to and including the first 1, then invert all the remaining bits to the left. For 0110 (6): copy "10", invert "01" to "10", giving 1010, which is -6.
The main difference between 1's and 2's complement
The main difference between 1's complement and 2's complement is the way they represent negative numbers. While 1's complement uses the bitwise complement of the corresponding positive number, 2's complement uses the 2's complement of the corresponding positive number. 2's complement is widely used in digital logic design due to its simplicity and the fact that it only has one representation of zero.
| Property | 1's complement | 2's complement |
|---|---|---|
| How to negate | Invert all bits | Invert all bits, then add 1 |
| Representations of zero | Two (+0 and -0) | One |
| Range for n bits | -(2n-1 - 1) to 2n-1 - 1 | -2n-1 to 2n-1 - 1 |
| Range for 8 bits | -127 to +127 | -128 to +127 |
| Carry out of the MSB during addition | Added back to the LSB (end-around carry) | Discarded |
| Sign extension | Copy the sign bit | Copy the sign bit |
| Typical use today | Internet checksums (IPv4, TCP, UDP) | Integer arithmetic in almost all processors and HDL designs |
4-bit values in unsigned, 1's complement and 2's complement
The table lists every 4-bit pattern and the value it represents in each system. Note how the patterns 0000 to 0111 mean the same thing in all three columns, and how 1111 is -0 in 1's complement but -1 in 2's complement.
| Binary | Unsigned | 1's complement | 2's complement |
|---|---|---|---|
| 0000 | 0 | +0 | 0 |
| 0001 | 1 | +1 | +1 |
| 0010 | 2 | +2 | +2 |
| 0011 | 3 | +3 | +3 |
| 0100 | 4 | +4 | +4 |
| 0101 | 5 | +5 | +5 |
| 0110 | 6 | +6 | +6 |
| 0111 | 7 | +7 | +7 |
| 1000 | 8 | -7 | -8 |
| 1001 | 9 | -6 | -7 |
| 1010 | 10 | -5 | -6 |
| 1011 | 11 | -4 | -5 |
| 1100 | 12 | -3 | -4 |
| 1101 | 13 | -2 | -3 |
| 1110 | 14 | -1 | -2 |
| 1111 | 15 | -0 | -1 |
Worked examples
Example 1: represent -6 in 8-bit 2's complement
- Write +6 in 8 bits: 0000 0110
- Invert every bit (1's complement): 1111 1001
- Add 1: 1111 1010
So -6 = 1111 1010. In 8-bit 1's complement, -6 would be 1111 1001 (step 2 only).
Example 2: convert 1111 1010 (2's complement) back to decimal
Using the weighted sum with a negative MSB weight: -128 + 64 + 32 + 16 + 8 + 0 + 2 + 0 = -128 + 122 = -6. Taking the 2's complement gives the same answer: invert to 0000 0101, add 1 to get 0000 0110 = 6, so the value is -6.
Example 3: 5 + (-3) in 4-bit 2's complement
0101 (+5)
+ 1101 (-3)
------
1 0010 carry out of the MSB is discarded
0010 = +2
Example 4: 5 + (-3) in 4-bit 1's complement
0101 (+5)
+ 1100 (-3)
------
1 0001 carry out of the MSB
+ 1 end-around carry added to the LSB
------
0010 = +2
Example 5: 3 - 5 as 3 + (-5)
In 2's complement: 0011 + 1011 = 1110, which is -2, with no carry out. In 1's complement: 0011 + 1010 = 1101, which is -2 (the 1's complement of 0010), again with no carry to wrap around. This shows rule (4) below in action: subtraction is done by adding the negated operand.
Example 6: sign extension
To widen a signed number, copy the sign bit into the new upper bits. In 2's complement, 1010 (-6) becomes 1111 1010 (-6) in 8 bits, and 0110 (+6) becomes 0000 0110. The same rule works for 1's complement: 1010 (-5) becomes 1111 1010 (-5).
Overflow rules
Overflow happens when the true result of an operation does not fit in the available bits. Because the range is fixed (-8 to +7 for 4-bit 2's complement), the result wraps around and the sign bit becomes wrong.
- Adding numbers of opposite sign never overflows. The result always lies between the two operands.
- Adding two numbers of the same sign overflows if the result has the opposite sign. Two positives giving a negative, or two negatives giving a positive, signals overflow. This rule holds for both 1's and 2's complement (for 1's complement, apply it after the end-around carry).
- Hardware rule for 2's complement: overflow V = Cin XOR Cout, where Cin is the carry into the sign bit and Cout is the carry out of it.
- Carry is not overflow. A carry out of the MSB is normal in 2's complement arithmetic (Example 3) and does not by itself mean the answer is wrong. The carry flag matters for unsigned arithmetic; the overflow flag matters for signed arithmetic.
- Negating the most negative number overflows. In 4-bit 2's complement, -8 = 1000; inverting and adding 1 gives 1000 again, because +8 cannot be represented.
| Operation (4-bit 2's complement) | Binary | Result | Cin to MSB | Cout | Overflow? |
|---|---|---|---|---|---|
| 5 + 4 | 0101 + 0100 = 1001 | -7 (true answer +9) | 1 | 0 | Yes |
| -6 + -5 | 1010 + 1011 = 1 0101 | +5 (true answer -11) | 0 | 1 | Yes |
| -3 + -2 | 1101 + 1110 = 1 1011 | -5 | 1 | 1 | No |
| 5 + (-3) | 0101 + 1101 = 1 0010 | +2 | 1 | 1 | No |
In Verilog, the sign-based rule can be written directly for two 4-bit operands:
wire [3:0] sum = a + b;
wire overflow = (a[3] == b[3]) && (sum[3] != a[3]);
If you declare operands as signed in Verilog, watch for mixed signed and unsigned expressions, which silently fall back to unsigned arithmetic. The post on the Verilog multiplication gotcha with signed operands shows a related trap.
Alternate explanation: why 2's complement became the standard
In general, we (human beings) express negative numbers by placing a minus (-) sign at the left end of the number. Similarly, while representing integers in binary format, we can let the left-most bit be the sign bit. If the left-most bit is a zero, the integer is positive; if it is a one, it is negative.
To make it easy to design computers which do integer arithmetic, integers should obey the following rules:
- Zero is positive and -0 = 0.
- The top-most bit should tell us the sign of the integer.
- The negative of a negative integer is the original integer, that is, -(-55) is 55.
- x - y should give the same result as x + (-y). That is, 8 - 3 should give us the same result as 8 + (-3).
- Negative and positive numbers shouldn't be treated in different ways when we do multiplication and division with them.
A simple and elegant way to represent integers which obeys these rules is called 2's complement. The 2's complement of an integer is calculated by changing all bits of the integer from 1 to 0 and 0 to 1, then adding 1 to the result. One small exception is worth knowing: rule (3) fails only for the most negative value (for example -128 in 8 bits), whose negation cannot be represented.
2's complement has become the standard method of storing signed binary integers. With n bits it allows the representation of numbers in the range -2n-1 to 2n-1 - 1, and has the major advantage of only having one encoding for 0.
1's complement arithmetic in more depth
1's complement addition is distinguished from the 2's complement addition typically encountered in computer arithmetic by how the carry out of the MSB is handled. In 1's complement, that carry is carried around back into the sum (the end-around carry); in 2's complement it is discarded.
In general, the inverse of a number under a given mathematical operation is the value which, when operated on with that number, returns the identity element. The 1's complement additive inverse of a number is its bitwise complement (replace 0s with 1s and 1s with 0s). This relies on a number and its complement summing to zero (the additive identity element). Actually, they sum to negative zero (all 1s): 1's complement addition effectively has two identity elements. Recall that an identity element under a given operation is a value which leaves any other number unchanged when the operation is applied. Under 1's complement arithmetic, the addition of either zero (all 0s) or negative zero (all 1s) to a number will generate a sum equal to the original number.
1's complement addition is both associative and commutative. Mathematically, it is addition modulo 2n - 1, with the two zero patterns treated as the same value, so it behaves as an Abelian group. As a result it is immaterial whether an identity element is added to a number or the number is added to an identity element, or whether the number operates on its inverse or the inverse operates on the number: both arrangements have the same result. Also note that the operation of subtraction is equivalent to adding the inverse (complement) of the number.
This order-independence is the reason the 16-bit 1's complement sum is still used for the Internet checksum in IPv4, TCP and UDP headers (described in IETF RFC 1071): the sum can be computed in any order, and on machines of either byte order.
Common mistakes and what interviewers check
- Forgetting the word width. "The 2's complement of 101" has no single answer until the width is fixed. In 4 bits, -5 is 1011; in 8 bits it is 1111 1011. Interviewers often ask for -1 in 8 bits (1111 1111) to see whether you sign-extend correctly.
- Treating carry as overflow. 5 + (-3) produces a carry out but the answer is correct. 5 + 4 produces no carry out but the answer is wrong. Expect a question that asks you to tell these two cases apart.
- Zero-extending a negative number. Padding 1010 (-6) with zeros gives 0000 1010 (+10). Signed values must be sign-extended.
- Assuming the range is symmetric. 8-bit 2's complement goes to -128 but only +127. Taking the absolute value of -128 in an 8-bit register returns -128. This shows up in real designs as a corner case in absolute-value and negation blocks of DSP datapaths, which is why saturating logic often clamps -128 to -127.
- Mixing signed and unsigned in HDL. In Verilog, one unsigned operand in an expression makes the whole expression unsigned, so a comparison such as
a < 0on a mixed expression can be silently false. - Dropping the end-around carry in 1's complement. Without it, 5 + (-3) gives 0001 (1) instead of 0010 (2).
Test yourself
Try these before reading the answers. All values are 4-bit unless stated.
- What is -7 in 1's complement and in 2's complement?
- What decimal value does 1001 represent in each system?
- Compute 6 + 3 in 2's complement. Does it overflow?
- Compute -4 + -4 in 2's complement. Does it overflow?
- Sign-extend the 2's complement value 1101 to 8 bits. What is its value?
- Compute (-2) + (-3) in 1's complement, showing the end-around carry.
Answers:
- +7 = 0111. 1's complement: 1000. 2's complement: 1001.
- Unsigned 9, 1's complement -6, 2's complement -7.
- 0110 + 0011 = 1001, which reads as -7. Two positives gave a negative, so yes, it overflows (the true answer +9 exceeds +7).
- 1100 + 1100 = 1 1000. Discard the carry: 1000 = -8. Both operands are negative and the result is negative, so there is no overflow; -8 is in range.
- 1111 1101, which is -3 (the same value as 1101).
- -2 = 1101 and -3 = 1100. 1101 + 1100 = 1 1001. Add the carry back: 1001 + 1 = 1010, which is -5.
Practice and related reading
- Test yourself with the short set of 2's complement arithmetic practice questions.
- More beginner questions on number systems and gates: logic design interview questions for beginners.
- For how processors use these representations: top 50 interview questions on CPU architecture.
- Look up related terms in the design and architecture glossary on The Semiconductor Hub.
Frequently asked questions
How do you find the 2's complement of a binary number?
Invert every bit (this gives the 1's complement) and add 1. For example, 0101 (5) becomes 1010, and adding 1 gives 1011, which is -5. A quicker method is to copy bits from the right up to and including the first 1, then invert the rest.
Why is 2's complement preferred over 1's complement?
2's complement has only one zero, one extra negative value, and needs no end-around carry, so a plain binary adder handles signed and unsigned addition and subtraction alike. Comparisons with zero are also simpler.
What is the range of an 8-bit 2's complement number?
-128 to +127. In general, an n-bit 2's complement number ranges from -2n-1 to 2n-1 - 1. An 8-bit 1's complement number ranges from -127 to +127.
How do you detect overflow in 2's complement addition?
Overflow occurs when two operands of the same sign produce a result of the opposite sign. In hardware, overflow equals the XOR of the carry into the sign bit and the carry out of the sign bit.
Where is 1's complement still used?
The best-known current use is the Internet checksum used in IPv4, TCP and UDP headers, which is a 1's complement sum of 16-bit words. Integer arithmetic in processors and HDL designs uses 2's complement.


Thanks..its a simple and understanding tutorial of complement methods..
ReplyDeletei love it..talaga
ReplyDeletegood Explanation .
ReplyDelete