Two's complement is a method of representing signed integers in binary. Here is an explanation of how addition works with two's complement numbers:
- In two's complement, the most significant bit indicates the sign of the number. 0 is positive, 1 is negative.
- To add two two's complement numbers, you can simply add the binary digits as if they were unsigned integers.
- The catch is if there is an overflow past the most significant bit, the result needs to wrap around to fit back into the available bits.
For example, let's add 5 and -3 in 8-bit two's complement:
5: 0000 0101
-3: 1111 1101
Adding them normally:
0000 0101
1111 1101
0001 0010
But this doesn't fit in 8 bits. To wrap the result, we discard the leftmost bit, leaving 0010. This wraps the result to -6, which is correct when adding 5 + -3.
So in summary, two's complement addition works by:
- Adding normally as if unsigned
- Wrapping if overflow past the MSB
This allows simple circuitry to add both positive and negative numbers.