1



How a Computer Performs the Subtraction of Integers

Albert Lilly, Ph.D.
Alabama School of Mathematics and Science
1255 Dauphin St.,
Mobile, AL 36604
E-mail: lilly@asms.net
Voice: (334) 441-2168
Fax: (334) 441-3290


2



Computers do not have to find the difference between two integers in order to subtract because of the way that integers are represented. In a system known as two's complement, half of the integers that can be represented by a given sequence of bits (a bit is a one or zero) are negative, one is zero, and one half minus one are positive. Since every bit is a one or zero, the total number of integers that can be represented in an N bit system is 2^N (two raised to the Nth power). For example, if N equals four, sixteen integers can be represented since 2^4 = 16.


3



Example: N = 4

In the following four bit system, the integers are represented as shown:

Position  Four Bits
   0       0 0 0 0   equals  0
   1       0 0 0 1   equals  1    
   2       0 0 1 0   equals  2    
   3       0 0 1 1   equals  3    
   4       0 1 0 0   equals  4    
   5       0 1 0 1   equals  5    
   6       0 1 1 0   equals  6    
   7       0 1 1 1   equals  7    
   8       1 0 0 0   equals -8   
   9       1 0 0 1   equals -7      
   10      1 0 1 0   equals -6      
   11      1 0 1 1   equals -5      
   12      1 1 0 0   equals -4      
   13      1 1 0 1   equals -3      
   14      1 1 1 0   equals -2      
   15      1 1 1 1   equals -1      
In a four bit system, integers greater than seven and less than negative eight cannot be represented. In practice, integers outside the range are, in effect, converted to a position in the range by ignoring all but the N rightmost bits. The position to the left is then translated to the integer on the far right of the given position. The N rightmost bits are always the remainder obtained by dividing by 2^N. So, for example, the integer 14 would be represented as -2 since position 14 above in the table is represented as -2 as shown to the right. The integer 18 would be represented as 2 since the remainder of dividing 18 by 16 is 2 and the integer to the right of equals on the line beginning with position 2 is also a 2. The integer 30 would be represented as -2 because the remainder of 30 divided by 16 is 14 and the integer to the right of equals on the line beginning with position 14 is -2.


4



Subtraction of Integers

Subtraction is performed by adding a negative integer. It is not necessary to find the difference in absolute values when the signs of the two integers differ. The tables for binary addition are shown below:

0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 and carry the 1
1 + 1 + 1 = 1 and carry the 1
The 1 carried is referred to as a carry bit.


5



Example

Suppose we want to subtract 3 from 4 which is the same as adding 4 and -3. The subtraction operation would be performed using addition as shown below:

Position       Four Bits

              1 1  (Carry Bits)
   4            0 1 0 0   equals  4
   13           1 1 0 1   equals -3      
                -------
   1            0 0 0 1   equals  1
Note that the leftmost carry bit is lost.


6



Exercises

Exercise 1: How many integers can be represented by a 16 bit system? How many integers can be represented by a 32 bit system?

Exercise 2: The pattern for integers in 16 bit and 32 bit systems is the same as in a four bit system: the first position is zero and that last position is negative one. The integers corresponding to the positions close to the middle are largest in absolute value. As stated above, half the integers are negative, one is zero, and one half minus one are positive. What are the ranges for negative and positive integers in a 16 bit system? What are the ranges for negative and positive integers in a 32 bit system?

Exercise 3: What is 40 in a four bit system? What is 40,000 in a sixteen bit system?

Exercise 4: Using the four bit system above, perform the following additions:

Position       Four Bits

   4            0 1 0 0   equals  4
   13           1 1 0 1   equals -3      
                -------


   15           1 1 1 1   equals -1 
   12           1 1 0 0   equals -4      
                -------


   5            0 1 0 1   equals  5
   14           1 1 1 0   equals -2      
                -------


   11           1 0 1 1   equals -5
   4            0 1 0 0   equals  4      
                -------


   10           1 0 1 0   equals -6
   7            0 1 1 1   equals  7      
                -------
                         

Exercise 5: Check the answers above by performing the operations using the decimal integers and comparing answers using the table on page 3.