Monday, 24 December 2012

Binary addition & Binary Subtraction

Binary addition:


The rules for binary addition are the same as those for any positional number system. One adds the digits column-wise from the right. If the sum is greater than B–1 for base B, a carry into the next column is generated. In the case of binary numbers, a sum greater than one generates a carry. Here is the binary addition table:

bi-addThe first three entries are self-explanatory. The third entry is 1+1=102, or one plus one is two; we have a sum of zero and a carry of one into the two’s place. The fourth entry is 1+1+1=112, or three ones are three. The sum is one and there is a carry into the two’s place.

Now we will add two binary numbers with more than one bit each so you can see how the carries “ripple” left, just as they do in decimal addition.

bi-addiThe three carries are shown on the top row. Normally, you would write these down as you complete the partial sum for each column.

Binary Subtraction:


Addition has the property of being commutative, that is, a+b = b+a. This is not true of subtraction. 5 – 3 is not the same as 3 – 5. For this reason, we must be careful of the order of the operands when subtracting. We call the first operand, the number which is being diminished, the minuend; the second operand, the amount to be subtracted from the minuend, is the subtrahend. The result is called the difference.

bi-subIt is possible to perform binary subtraction using the same process we use for decimal subtraction, namely subtracting individual digits and borrowing from the left. This process quickly becomes cumbersome as you borrow across successive zeroes in the minuend.. Jacobowitz describes the “carry” method of subtraction , where a one borrowed in the minuend is “paid back” by adding to the subtrahend digit to the left. This means that one need look no more than one column to the left when subtracting. Subtraction can thus be performed a column at a time with a carry to the left, analogous to addition. This is a process which can be automated, but we are left with difficulties when the subtrahend is larger than the minuend or when either operand is signed.

Since we can form the complement of a binary number easily and can add signed Numbers easily, the obvious answer to the problem of subtraction is to take the two’s complement of the subtrahend, then adds add it to the minuend. We aren’t saying anything more than that 51–22 = 51+(–22).

The subtraction of two n-digit unsigned numbers M-N in base r can be done as follows:

1. Add the minuend M, to the r’s complement of the subtrahend, N.
2. If M >= N the sum will produce an end carry which can be discarded, and we left the result M-N.
3. If M < N the sum does not produce an end carry , and we have to take r’s complement of the sum and place a negative number in front.


Example:

Given the two binary numbers X=1010100 and Y=1000011, perform the subtraction (a) X – Y (b) Y – X using 2’s complements.


bi-subtThere is no end carry .
Therefore the answer is Y – X = -(2’s complement of 1101111) = -0010001

Related Topics : C++ Aptitude & OOPs

No comments:

Post a Comment