### 9.1. Binary arithmetic operations

posted Jun 30, 2014, 10:04 PM by Maurice Nyamoti   [ updated Jan 26, 2016, 10:29 PM ]

## Binary arithmetic operations

### Direct Subtraction

There are four main subtraction expressions namely:

1.       02 -02 =02

2.       12 -02 =12

3.       12 -12 =02

4.       102 -12 =12  (borrow 1 from the next most significant digit to make 0 which

becomes 102, hence 102 -12 =12

### Examples:

Here are some examples of binary subtraction. These are computed without regard to the word size, hence there can be no sense of "overflow" or "underflow". Work the columns right to left subtracting in each column. If you must subtract a one from a zero, you need to “borrow” from the left, just as in decimal subtraction.

### Subtraction using ones complements

The main purpose of using the ones complement in computers is to perform binary subtractions. For example to get the difference in 5-3, using the ones compliment, we proceed as follows:

1.      Rewrite the problem as 5 + (-3) to show that the computer performs binary subtraction by adding the binary equivalent of 5 to the ones compliment of 3.

2.      Convert the absolute value of 3 into 8-bit or 4-bit for smaller numbers equivalent i.e. 000000112.

3.      Take the ones compliment of 000000112 i.e. 111111002 which is the binary representation of -310.

4.      Add the binary equivalent of 5 to the one’s complement of 3 i.e.

00000101

+ 11111100

(1)00000001

Looking at the difference of the two binary numbers, you will observe that:

1. It has a ninth bit. The ninth bit is known as an overflow bit.
2. The result shows that the difference between the two numbers is 00000001. This is not true; we know that it should be 00000010. To address this problem in a system that uses ones compliment the overflow digit is added back to the magnitude of the 8-bit difference. Therefore the difference becomes 00000001+1 = 00000010 which is the correct answer.
`Example:`
Let's consider how we would solve our problem of subtracting 110 from 710 using 1's complement.
 First, we need to convert 00012 to its negative equivalent in 1's complement. ``` 0111 (7) - 0001 - (1)``` To do this we change all the 1's to 0's and 0's to 1's. Notice that the most-significant digit is now 1 since the number is negative. ` 0001 -> 1110` Next, we add the negative value we computed to 01112. This gives us a result of 101012. ``` 0111 (7) + 1110 +(-1) 10101 (?)``` Notice that our addition caused an overflow bit. Whenever we have an overflow bit in 1's complement, we add this bit to our sum to get the correct answer. If there is no overflow bit, then we leave the sum as it is. ``` 0101 + 1 0110 (6)``` This gives us a final answer of 01102 (or 610). ``` 0111 (7) - 0001 - (1) 0110 (6)```

## Easier method

### Using ones compliment evaluate 1110 - 1310

Step 1: Rewrite the equation

thus:   1110 + (- 1310)

Step 2: Convert decimals to binary

1110 = 10112

1310 = 11012

Step 3: Convert 1310 = 11012 into ones compliment (-1310)

in this case 11012 is converted to 00102 this means that 0s turn to ones and             ones turn to zeros

Step 4: insert overflows to show sign bit i.e. positive or negative

1110 = (0)10112

1310  = (1)00102

zero (0) stands in for a positive sign bit and one (1) stand in for a negative sign bit

Step 5: add (0)10112 to (1)00102

(1)00102
(0)10112

#### therefore, (1)11012 represents (-210) which is the correct answer

Now let's look at an example where our problem does not generate an overflow bit. We will subtract 710 from 110 using 1's complement.

 First, we state our problem in binary. ``` 0001 (1) - 0111 - (7)``` Next, we convert 01112 to its negative equivalent and add this to 00012. ``` 0001 (1) + 1000 +(-7) 1001 (?)``` This time our results does not cause an overflow, so we do not need to adjust the sum. Notice that our final answer is a negative number since it begins with a 1. Remember that our answer is in 1's complement notation so the correct decimal value for our answer is -610 and not 910. ``` 0001 (1) + 1000 +(-7) 1001 (-6)```

`Algebraic expression`

`Let's review the steps for subtracting ``x`` from ``y```` with an n-bit 1's complement representation.```

1. `Negate ``x```` using 1's complement. ````Reverse all the bits in ``x`` to form ``-x``.` `Add ``-x`` and ``y``.`
2. `If the sum exceeds n bits, add the extra bit to the result.`
3. `If the sum does not exceed n bits, leave the result as it is`

### Subtraction using twos complements

Like in one’s compliment, the two complement of a number is obtained by negating a positive number to its negative counterpart. For example, to get the difference in 5 – 3, using  the two’s complement, we proceed as follows:

1.      Rewrite the problem as 5 + (-3)

2.      Convert the absolute value of 3 into 8-bit binary equivalent i.e. 00000011

3.      Take the ones complement of 00000011 i.e. 11111100

4.      Add a 1 to the ones compliment i.e. 11111100 to get 11111101

5.      Add the binary equivalent of 5 to the twos complement of 3 i.e.

 00000101 11111101 (1)00000010

6.      Ignoring the overflow bit, the resulting number is 0000010 which is directly read as a binary equivalent of +2

`Example:`
Now let's consider how we would solve our problem of subtracting 110 from 710 using 2's complement.
 First, we need to convert 00012 to its negative equivalent in 2's complement. ``` 0111 (7) - 0001 - (1)``` To do this we change all the 1's to 0's and 0's to 1's and add one to the number. Notice that the most-significant digit is now 1 since the number is negative. ``` 0001 -> 1110 1 1111``` Next, we add the negative value we computed to 01112. This gives us a result of 101102. ``` 0111 (7) + 1111 +(-1) 10110 (?)``` Notice that our addition caused an overflow bit. Whenever we have an overflow bit in 2's complement, we discard the extra bit. This gives us a final answer of 01102 (or 610). ``` 0111 (7) - 0001 - (1) 0110 (6)```
`Algebraic Expression`

`Let's review the steps for subtracting ``x`` from ``y```` with an n-bit 2's complement representation.```

1. `Negate ``x```` using 2's complement. ```
2. `Reverse all the bits in ``x``.`
3. `Add 1 to form ``-x``.`
4. `Add ``-x`` and ``y``.`
5. `Discard any bits greater than n.`

`Now go back and compare these steps with the steps for ````1's complement subtraction``````. Notice that with 1's complement, you must check for an overflow bit each time you perform a subtraction. If the result has an overflow, you need to add the extra bit to your result to obtain the correct answer. However, with 2's complement, we only need to ignore this extra bit. No other computations are required to find the correct answer. This makes 2's complement a more efficient way of representing signed numbers and performing binary subtraction.```

## Trial Questions

1.       Work out the following binary subtractions

i.                     110012 – 10102 =

ii.                   1012 – 1002 =

iii.                  110112 – 1112 =

iv.                 11002 – 0112 =

v.                   1110112 – 1102 =

vi.                 10002 – 112 =

vii.                011012 – 10112 =

viii.              111111112 – 101011012 =

ix.                 111011012 – 1001112 =

x.                   1000002 – 11112 =

2.       Convert the decimal number -7 to 8-bit binary using ones and twos complement

3.       Using 8-bit length, find the twos compliment of the following decimal numbers

A)     -3110

B)      -2810

C)      -510

4.       Using ones and twos complement perform the following arithmetic operations

a)      1410 – 710 =

b)      2810 – 1210 =

c)       3410 -3310 =

d)      10010 – 5010 =

e)      10210 -6410 =

5.       Perform the following subtractions

a)      10002 -102 =

b)      111112 – 11112  =

c)       110112 – 1112 =

6.       Perform the following in ones complement, showing that a-b is the same as a+(-b) where ‘a’ is the positive number and ‘b’ is the negative number

a)      10002 – 102 =

b)      111102 – 1112 =

c)       1000002 – 1002 =

7.       Repeat number 3 above using twos complement