Note: This supplement assumes you already know how to convert base 2 numbers to base 10. Look for base 2 to base 10 conversion in Epp Section 1.5.

- We use them as an action when we say to "take the two's
complement of" some binary number or bit pattern. In that case,
we mean to flip the bits and add 1 to them, ignoring any carry
that's too big to store.
For example, the two's complement of 1011 is: flip the bits to get 0100 and add 1 to get the answer 0101. It doesn't matter at all whether 1011 was positive, negative, or even whether it represented a number.

"Ignoring any carry that's too big to store" refers to taking the two's complement of a number that's all 0s like 0000. When we flip the bits, we get 1111. When we add 1, we get 0000,

*not*10000. That's because the extra 1 doesn't fit in the available four bits. - We might say about a binary value that it is "a signed
binary number that uses the two's complement representation".
This means that the bits represent a number. If the leftmost
bit is a 0, the number represented is simply the bits' value as
a base 2 number. If the leftmost bit is a 1, the number
represented is negative. To find its magnitude, we
*take the two's complement*of the bits (flip them and add 1) and interpret the result as a base 2 number.

To summarize, when we say to "take the two's complement" of a
value, we *always* mean to flip the bits and add 1. When
we say a binary number "uses the two's complement
representation", it means you may or may not need to "take the
two's complement" in order to find the number's value, depending
on whether the leftmost bit is a 1.

- If the leftmost bit is a 1, the number is negative. Do the
following... (
**1**110 starts with a 1. It's negative.)- Flip all the bits in the number. (1110 becomes 0001.)
- Add 1 to the number. (0001 + 1 = 0010.)
- Convert the result to base 10 and report its
negation. (0010
_{2}is 2_{10}. The answer is -2.)

- If the leftmost bit is a 0, the number is positive or zero.
Convert the number from base 2 to base 10 and report the
result. (
**0**101 starts with a 0. 0101_{2}is 5_{10}. The answer is 5.)

The only strange thing is that we again flip the bits and add 1, rather than subtracting 1 and then flipping the bits (the reverse of what we did above). Why is that? Because flipping and adding 1 is the same as subtracting 1 and flipping. If you doubt that, play around with some numbers to see!

We illustrate the algorithm on two examples, converting them to four-bit signed numbers: -4 and 1.

- Convert the absolute value of the number to base 2. (The absolute value of -4
_{10}is 4_{10}. That's 100_{2}.) (The absolute value of 1_{10}is 1_{10}. That's 1_{2}.) - Put enough 0s on the left side to give the base 2 number as many bits as you have available (e.g., 4 bits in our examples). (100 needs one extra 0: 0100.) (1 needs three extra 0s: 0001.)
- If the original number was positive stop and report the result. (1 is positive; so, the answer is 0001.)
- Otherwise, flip the bits in the base 2 number and add 1 (ignoring any carry that goes beyond the number of bits available), and report the result. (-4 is negative; so, we flip the bits in 0100 to get 1011 and add 1. The answer is 1100.)

Last updated sometime on