Sei sulla pagina 1di 7

Number Systems Theory

Binary Subtraction Using the "Two's Complement" Method


Introduction
The content of this document assumes that the reader is familiar with number systems theory in general, including the binary number systems theory. This method for subtracting binary numbers is based on two steps called the "1's complement" step, and the "2's complement" step

These steps are applied to the binary number that is to be subtracted from another binary number, as demonstrated below. Applying these steps allows one to perform a subtraction by adding a negative binary number to a positive binary number, a slight advantage, since the process for adding binary numbers is a bit less complicated than that of subtracting binary numbers. Also, this method eliminates the need to keep track of the "debt" that in some cases must be created in position 9 in a binary number when eight-bit binary numbers are subtracted (or in position n+1 for n-bit binary numbers) when using the "standard" method for binary subtraction. This method simply focuses on performing the subtraction directly for one column (position) at a time.

Document Structure
For the sake of facilitating the comprehension of the content in this document, a following graphical view of the structure of the document is shown in Fig. 1. One of the reasons for including the graph is the difference in the calculation steps required depending on whether the result of the subtraction is positive or negative.

Fig. 1
Example 1: Positive Result The C1 Step The C2 Step Performing the Subtraction Summary of Subtraction Method when Result is Positive

Introduction

Document Structure

Examples

Example 2: Negative Result

The C1 Step (1st time)

The C2 Step (1st time)

Performing the Subtraction

The C1 Step (2nd time)

The C2 Step (2nd time)

Summary of Subtraction Method when Result is Negative

www.dabbleklix.com

12/03/07

Number Systems Theory

Notations
For the sake of simplicity and consistency, the following notations will be used in this document: C1 = The "1's complement" step C2 = The "2's complement" step n10 = Indicates that the number (decimal system) n2 = Indicates that the number system) n n is a number in the 10-Base system

is a number in the 2-Base system (binary

A number that consists of 0's and 1's only is always a binary number unless otherwise specified.

Examples
In order to facilitate the explanation the "2's complement" subtraction method, the following examples, one with a positive result, and one with a negative result, will be used: Example 1: 12210-4610=7610 Example 2: 4610-12210 = -7610

Example 1: Performing a Subtraction with a Positive Result


The "1's Complement" Step
The number 4610 is expressed as 0 0 1 0 1 1 1 0 4610 (Line 1)

Complementing each 0 with a 1, and each 1 with a 0 in the binary number in Line 1 results in 0 1 0 1 1 0 0 1 1 0 1 0 1 0 0 1 C1 4610 20910 (Line 2) (Line 3)

Note that the binary number represented by C1 in Line 3 represents the value 20910 since the C1 step always results in the value

www.dabbleklix.com

12/03/07

Number Systems Theory

111111112 = 25510

(Line 4)

because the sum of the binary numbers in Line 2 and Line 3 is always 11111111, as shown below. 0 1 1 0 1 1 1 0 1 0 1 1 1 0 1 1 0 1 1 0 1 0 1 1 C1 4610 20910 25510 (Line 5) (Line 6) (Line 7)

Thus, the C1 step always results in the number 25510-n10 where n10 is the original binary number to which the C1 step is applied, as demonstrated in Line 2 and 3.

The "2's Complement" Step


The C2 step is performed by adding a 1 to position 1 (rightmost position) in the binary number in Line 3, as follows: 1 1 0 1 0 0 0 1 1 1 1 0 1 0 0 1 0 C1 C2 20910 110 21010 (Line 8) (Line 9) (Line 10)

Note that adding the binary numbers in Line 5 and Line 10 results in 0 1 1 0 0 1 0 1 0 0 0 1 0 1 0 0 1 0 0 1 1 0 0 0 0 4610 21010 25610 =-4610 (Line 11) (Line 12) (Line 13)

Note that this transaction resulted in the creation of one quantity 29 in position 9. This quantity is a "debt", as shown in the equation in Line 18.

www.dabbleklix.com

12/03/07

Number Systems Theory

Note that Line 13 equals the sum of the binary numbers in Line 14 and Line 15. 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 25510 110 25610 (Line 14) (Line 15) (Line 16)

Performing the Subtraction


Performing the subtraction 12210 - 4610 = 7610 (Line 17)

using the binary number for the value -4610 in Line 12 actually reflects the subtraction 12210 + 21010 - 25610 = 7610 (Line 18)

since the C1 and C2 steps have increased the value of the original binary number for 4610 by a quantity of 29 = 25610 in Line 13, as shown by adding the binary numbers in Line 11 and Line 12. The binary digit quantity 12 in position 9 in Line 13 is a "debt", and is, therefore, negative, as shown in the equation in Line 18. The value -4610 comes from the last two terms on the left-hand side in the equation in Line 18, as follows: 21010 - 25610 = -4610 (Line 19) Thus, it is the creation of a "debt", equal to 29 = 25610, in position 9 that causes the binary number in Line 10 to function as if it had the value -4610. Line 10 actually represents the result of the equation in Line 19. The subtraction, represented by the equation in Line 17, mathematically takes on the 10-base format 12210 +(- 4610) = 7610 and the binary format 0 1 1 0 1 1 1 1 0 0 1 1 0 1 0 1 0 0 1 1 1 0 0 0 0 12210 21010 7610 = -4610 (Line 21) (Line 22) (Line 23) (Line 20)

Note that, in accordance with the format of the equation in Line 20, the binary numbers in Line 21 and Line 22 are added. Adding the values from position 1 through 8 in Line 23 and converting to 10-base yields the value 7610, which corresponds to the result from the equation in Line 20, and is, therefore, verified.

www.dabbleklix.com

12/03/07

Number Systems Theory

The digit in position 8 is called the high bit. The digits in the positions 1 through 7 are called low bits. If the high bit in the result, in this case in Line 23, is a 0 (zero), the result is positive. If the high bit in the result is a 1 (one), the result is negative, s demonstrated in Example 2.

Summary of the C1, C2 Subtraction Method when the Result is Positive


The C1, C2 method can be summarized as follows: 1. Select the binary number to be subtracted. 2. Apply the C1 step to the selected binary number. 3. Apply the C2 step to the binary number resulting from the C1 step. 4. Add the binary number resulting from the C2 step to the binary number from which the binary number selected in step 1 is to be subtracted. 5. Verify the result by converting the resulting binary number to a 10-base (decimal) number.

Example 2: Performing a Subtraction with a Negative Result


Performing the subtraction 4610-12210 = -7610 (Line 24)

yields a negative result. It is, therefore, necessary to apply the C1 and C2 steps twice in order to arrive at the desired solution, as shown below.

The "1's Complement" Step, 1st Time


The 1st step in performing this subtraction of binary numbers is to apply the C1 step to the binary value for the value 12210, as follows: 0 1 1 0 1 0 1 0 1 0 0 1 1 0 0 1 C1 12210 13310 (Line 25) (Line 26)

The C1 step results in the value 13310, which is correct since the sum of the two binary numbers in Line 25 and Line 26 is 12210+13310=25510 (Line 27)

The "2's Complement" Step, 1st Time


Applying the C2 step to the binary number in Line 26 results in
www.dabbleklix.com 5 12/03/07

Number Systems Theory

1 1

C1 C2

13310 110 13410 =-12210

(Line 28) (Line 29) (Line 30)

Performing the Subtraction


The subtraction represented by the equation in Line 24, but by using the binary numbers for the terms on the left hand side in the equation, is performed as follows: 0 1 1 1 0 0 0 1 0 1 0 0 1 1 0 0 1 1 1 1 1 0 0 0 0 4610 13410 18010 = -12210 = -7610 (Line 31) (Line 32) (Line 33)

Note that the binary numbers in Line 32 and Line 33 are added. Converting the binary number in Line 33 encompassing position 1 through 8 to a 10-base number results in the value 18010. However, the debt of 25610 created in the C2 step (compare Example 1, Lines 11 and 12) is reflected by the value 1 in position 9 in Line 33. Thus, one obtains 18010 -25610=-7610 (Line 34)

Since it is cumbersome to have to remember the "debt" in position 9 in Line 33 when converting the binary number to a 10-base value, applying the C1 and the C2 steps once more, as follows, eliminates the "debt":

The "1's Complement" Step, 2nd Time


Applying the C1 step to the binary number (position 1 through 8) in Line 33 results in 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 1 C1 18010 7510 =-7610 (Line 35) (Line 36)

www.dabbleklix.com

12/03/07

Number Systems Theory

The "2's Complement" Step, 2nd Time


Applying the C2 step to the binary number in Line 36 results in 0 1 0 0 1 0 1 1 1 0 1 0 0 1 1 0 0 C1 C2 7510 110 7610 = -7610 (Line 37) (Line 38) (Line 39)

Converting the binary value in Line 39 to a number in the 10-base system yields the value 7610. However, since the high bit in position 8 in Line 39 is a 0 (zero), the value is negative, that is, -7610.

Summary of the C1, C2 Subtraction Method when the Result is Negative


The C1, C2 method can be summarized as follows: 1. Select the binary number to be subtracted. 2. Apply the C1 step to the selected binary number. 3. Apply the C2 step to the binary number resulting from the C1 step. 4. Add the binary number resulting from the C2 step to the binary number from which from which the binary number selected in step 1 is to be subtracted. 5. Apply the C1 step to the result. 6. Apply the C2 step to the result obtained in step 5. Note that the steps 5 and 6 are performed in order to eliminate the debt created in position 9 as a result from the C1 and C2 steps in step 2 and 3. 7. Verify the result by converting the resulting binary number to a 10-base (decimal) number.

www.dabbleklix.com

12/03/07

Potrebbero piacerti anche