**⌚ November 3, 2014**

In Lesson 10, we saw how to perform binary subtraction using a set of rules for each column of bits.

Now that we have seen how to use signed numbers in binary, we can subtract in binary by performing algebraic addition using the two’s complement method.

Neither technique is more correct than the other. They are two different ways that produce the same result.

Personally, I find the two’s complement method to be easier to compute than the longhand method. It might sound conflicting to “subtract” by “adding,” but that is what we are doing when we add two signed numbers together with opposite signs. (+5) + (-3) = 2, which is the same as 5 – 3 = 2. Same result, but two different thought processes.

**The Process**

- Place the
**minuend**above the subtrahend. - Convert the
**subtrahend**into its two’s complement. Do not convert both values. - Pad both values to one bit greater than the number of bits in the minuend to accommodate the sign bit and to help avoid alignment mistakes.
- Add in binary. Discard the carry bit.

The result will be a binary signed number with the sign retained. The final value will contain the same number of bits as the largest value.

### Subtract 1001b – 11b

When subtracting, we usually place the larger number, the minuend, above the smaller number, the subtrahend.

1001b <-- Minuend - 11b <-- Subtrahend -------

Pad to one bit greater than the minuend.

01001b - 00011b -----------

Padding to a sign bit is not always necessary. In fact, in this example, the result will be correct without padding. The issue is that when we convert to two’s complement, we are entering the world of signed binary where the most significant bit represents the sign of the value. When the most significant value is 1, as in the original value 1001b, confusion can arise. 1001b is positive even though its most significant bit is 1. To avoid ambiguity, we pad the original value with a most significant bit of 0 to denote its positive status.

Next, convert the subtrahend (00011b) to its two’s complement (11101b).

This is correct,

01001b - 11101b <--- Two's complement of 00011b --------

But this,

10111b <--- Two's complement of 01001b - 00011b --------

will produce a result with an incorrect sign, and it will require an additional step to convert the result into its two’s complement in order to obtain a result with the correct sign. We want to avoid this extra step, so we convert the subtrahend.

This process works because the two’s complement forms the negative of a binary value (inverts the sign and adjusts the data bits to reflect the change). Algebraic addition simply adds a positive value and a negative value together to obtain the same result that would be obtained through plain subtraction.

After converting to two’s complement, we are no longer subtracting. We are adding, so change the subtraction symbol from (-) to (+).

01001b + 11101b <--- Two's complement of 00011b --------

Now, add in binary. Please see Lesson 9 to review binary addition if necessary.

01001b (+9) + 11101b (-3) -------------- 1 00110b (+6) <-- Answer with positive sign intact ^ | Carry bit. Discard.

In the example above, we converted 00011b (+3d) to its additive inverse (-3d) and then added it to +9. The result is +6. The carry bit is normal, but do not be fooled. It does not indicate sign nor does it contribute to the final answer. Only the bit length matching the longest bit length of the values will matter. In this case, the maximum bit length is 5 (01001b), so only the first five bits (starting from the least significant bit) of the six bits in the answer matter, so we discard the sixth carry bit.

We can verify that 01001b + 11101b = 00110b by converting the original to decimal and subtracting in decimal. Remember, we are dealing with signed binary, so 11101b is -3d, not 29d.

## What Happens When Converting the Minuend to Two’s Complement?

Order does not matter with addition, so we should, in theory, still achieve the correct answer. *Right?*

Not quite. The absolute value will be correct, but not the sign. We must add an extra step of converting the result to its two’s complement to obtain the correct value with the proper sign. The following example will help explain why.

### Subtract 1001b – 11b using the two’s complement of 1001b

**First, set up** the subtraction problem normally with the greater/larger value as the minuend.

1001b - 11b --------

**Pad **the subtrahend to four digits for a start. Since we are using signed numbers with two’s complement values, a better practice is to pad both values to five bits so 1001b is not misinterpreted as a negative value. This reserves the most significant bit for the sign bit.

In this example, the answer will be correct regardless of the extra padding, and we will demonstrate this by showing both 4-bit and 5-bit versions.

4-bit 5-bit Decimal ---------------------------------------- 1001b 01001b 9 - 0011b - 00011b - 3 ---------- ---------- ----

In all three cases shown, we are subtracting 9 – 3.

**Convert 1001b to its two’s complement.**

4-bit 5-bit Decimal ---------------------------------------- 0111b 10111b (-9) + 0011b + 00011b + (+3) ---------- ---------- ------

0111b is the two’s complement of 1001b, and 10111b is the two’s complement of 01001b. 10111b is actually the correct way to represent a signed 4-bit value, but both will work in this case.

The conversion to two’s complement means we are using signed numbers, so we also change the subtraction symbols to addition symbols. We will be using binary addition, not subtraction, to solve the problem.

**Next, add.**

4-bit 5-bit Decimal ---------------------------------------- 0111b 10111b (-9) + 0011b + 00011b + (+3) ---------- ---------- ------ 11010b 11010b (-6)

*Wait a minute!* In the previous example, 1001b – 11b = 110b (6d). 110b is also written as 00110b. 11010b looks nothing like 00110b. What happened? Actually, the answer is correct, but we have a negative sum. 00110b is a positive sum. So, why do we have a negative sum?

Before, we added (+9) + (-3) to get +6, or 110b. That is the correct answer to the original problem. This time, we converted 1001b (+9) to its two’s complement to form 10111b, which is -9 in decimal. Because of this, the larger number is negative, and we are algebraically adding a small positive number to a larger negative number, so the result is negative. In this case, the result is -6d, or 11010b. (-9) + (+3) = (-6), which is correct as it is, but it is incorrect when we look at the original problem: **Subtract 1001b – 11b**.

1001b – 11b does not equal 11010b because 9 – 3 = +6, not -6. *The sign is important!* We need to perform one additional step to obtain answer with the correct sign: Convert 11010b to its two’s complement.

00110b <--- Two's complement of 11010b

Now, the answer is correct, but this adds extra work. For this reason it is preferable to convert the minuend to its two’s complement instead of the subtrahend.

### Subtract 11110000b – 11000000b

With larger binary values, it helps to convert to decimal so we can double check the binary result. Also, note that both values are normal, unsigned values.

11110000b = 240d 11000000b = 192d

240 – 192 = 48d. Therefore, the result of the binary subtraction should convert to 48d.

**First, align** with the greater value as the minuend.

11110000b - 11000000b -----------

**Pad** an extra sign bit because we are using the two’s complement method. This is quite easy to do. Since both values are positive, simply add an extra most significant 0 bit to each. Both values are now nine bits in length.

011110000b - 011000000b ------------

**Convert the subtrahend**, the lesser value, to its two’s complement. Switch to an addition symbol.

We should see this,

011110000b (+240d) + 101000000b (-192d) <--- Two's complement of 011000000b (+192d) ------------

Not this,

100010000b (-240d) <--- Two's complement of 011110000b (+240d) + 011000000b (+192d) ------------

**Add.**

011110000b +240d + 101000000b + -192d ------------- -------- 1 000110000b +48d | Carry bit. Discard. Only the first nine bits are relevant.

000110000b converts to +48d, which we compare with the +48d we obtained earlier. This verifies that we have the correct answer and that 11110000b (240d) – 11000000b (192d) = 110000b (48d).

Since that was so much fun, let’s solve another!

### Subtract 1111b – 11000b

This problem is stated improperly because we normally subtract a smaller value from a larger value.

**Subtract 11000b – 1111b.** This reads better. 11000b is the minuend, and 1111b is the subtrahend.

11000b 24d - 1111b 15d --------

Both are unsigned, positive integers, so we pad them both out to an extra sign bit.

011000b +24d - 001111b +15d ---------

Convert the subtrahend (001111b) to its two’s complement and change the subtraction symbol to an addition symbol.

011000b +24d + 110001b -15d ----------

**Add.**

011000b +24d + 110001b + -15d --------- ------ 1 001001b 9d | Discard the carry bit.

11000b (24d) - 1111b (15d) = 1001b (9)

## Practice

Subtract using the two’s complement method. Use longhand subtraction and decimal conversion to verify the answers.

1. 1001b - 11b 2. 1b - 1b 3. 101010b - 101b 4. 11011b - 11011b 5. 100000000b - 1b 6. 0xfe - 0x2a

### Answers

**1.** 110b

**2.** 0. Yes, this was easy, but did you solve it using the two’s complement or did you simply look at it and subtract 1 – 1 without the two’s complement conversion and algebraic addition steps? How about padding for a sign bit in both values? The full process should have been something like this in binary:

1 01 01 - 1 --> - 01 --> + 11 ---- ---- ---- 1 00 <--- 2-bit result simplified to 0. ^ | Discard the carry bit.

**3.** 100101b (37d)

101010 0101010 0101010 +42d - 101 --> - 0000101 --> + 1111011 + -5d -------- --------- --------- ------ 1 0100101 +37d ^ | Discard

**4.** 0. Yes, this is another contrived problem that is solved easily by looking at it, but did you complete the full process anyway? Understanding is key, and working the full process helps demonstrate how algebraic addition in binary produces the same result as longhand binary subtraction.

11011 011011 011011 (+27) - 11011 --> - 011011 --> + 100101 (-27) ------- -------- -------- 1 000000 ^ | Discard carry bit

**5.** 11111111b (255d). Consecutive bits make it easy to lose track of location. This is why careful alignment is important. Also, notice how the bit length grows to accommodate the sign bit and the carry bit.

100000000 0100000000 0100000000 (+256) - 00000001 --> - 0000000001 --> + 1111111111 (-1) ----------- ------------ ------------ 1 0011111111

**6.** 11010100b (213d)

Hexadecimal? This is solvable without performing hexadecimal subtraction. Convert both hex values into binary first and then subtract.

0xfe = 11111110b = 254d 0x2a = 101010b = 42d

11111110 011111110 011111110 (+254d) - 00101010 --> - 000101010 --> + 111010110 (-42d) ---------- ----------- ----------- 1 011010100b ^ | Discard the carry bit