Addition and subtraction by using Shift operator


Description:
This is multiple way do the Addition and subtraction . But mainly focusing on Booth's Algorithms' and recursive methods.
N.B : For Subtraction:
So when we do the subtraction then first convert to 2's complement of sub-tractor after then call recursive method itself recursive.

  1. Bitwise OR (|) –
    This operator is binary operator, denoted by ‘|’. It returns bit by bit OR of input values, i.e, if either of the bits is 1, it gives 1, else it gives 0.
    For example,
    a = 5 = 0101 (In Binary)
    b = 7 = 0111 (In Binary)
    
    Bitwise OR Operation of 5 and 7
      0101
    | 0111
     ________
      0111  = 7 (In decimal) 
  2. Bitwise AND (&) –
    This operator is binary operator, denoted by ‘&’. It returns bit by bit AND of input values, i.e, if both bits are 1, it gives 1, else it gives 0.
    For example,
    a = 5 = 0101 (In Binary)
    b = 7 = 0111 (In Binary)
    
    Bitwise AND Operation of 5 and 7
      0101
    & 0111
     ________
      0101  = 5 (In decimal) 
  3. Bitwise XOR (^) –
    This operator is binary operator, denoted by ‘^’. It returns bit by bit XOR of input values, i.e, if corresponding bits are different, it gives 1, else it gives 0.
    For example,
    a = 5 = 0101 (In Binary)
    b = 7 = 0111 (In Binary)
    
    Bitwise XOR Operation of 5 and 7
      0101
    ^ 0111
     ________
      0010  = 2 (In decimal) 
  4. Bitwise Complement (~) –
    This operator is unary operator, denoted by ‘~’. It returns the one’s compliment representation of the input value, i.e, with all bits inversed, means it makes every 0 to 1, and every 1 to 0.
    For example,

a = 5 = 0101 (In Binary)

Bitwise Compliment Operation of 5

~ 0101
 ________
  1010  = 10 (In decimal) 
Note – Compiler will give 2’s complement of that number, i.e., 2’s compliment of 10 will be -6.
5.Left shift operator (<<) –
Shifts the bits of the number to the left and fills 0 on voids left as a result. Similar effect as of multiplying the number with some power of two.
For example,
a = 5 = 0000 0101
b = -10 = 1111 0110

a << 1 = 0000 1010 = 10
a << 2 = 0001 0100 = 20 

b << 1 = 0000 1010 = -20
b << 2 = 0001 0100 = -40 

6.Signed Right shift operator (>>) –
Shifts the bits of the number to the right and fills 0 on voids left as a result. The leftmost bit depends on the sign of initial number. Similar effect as of dividing the number with some power of two.
For example,
a = 5 = 0000 0101
b = 10 = 0000 1010 (takes 2's compliment)
b = -10 = 1111 0110

b >> 1 = 1111 1011 = -5
b >> 2 = 1111 1101 (takes 2's compliment) = -3 




Example: 1101 and -1001

First, find the 2's complement of the negative number 1001. So, for finding 2's complement, change all 0 to 1 and all 1 to 0 or find the 1's complement of the number 1001. The 1's complement of the number 1001 is 0110, and add 1 to the LSB of the result 0110. So the 2's complement of number 1001 is 0110+1=0111
Add both the numbers, i.e., 1101 and 0111;
1101+0111=1  0100
By adding both numbers, we get the end-around carry 1. We discard the end-around carry. So, the addition of both numbers is 0100.
Case 2: Adding of the positive value with a negative value when the negative number has a higher magnitude.

Initially, add a positive value with the 2's complement value of the negative number. Here, no end-around carry is found. So, we take the 2's complement of the result to get the final result.


Example: 1101 and -1110

First, find the 2's complement of the negative number 1110. So, for finding 2's complement, add 1 to the LSB of its 1's complement value 0001.
0001+1=0010
Add both the numbers, i.e., 1101 and 0010;
1101+0010= 1111
Find the 2's complement of the result 1110 that is the final result. So, the 2's complement of the result 1110 is 0001, and add a negative sign before the number so that we can identify that it is a negative number.




Code 1:
?
package com.kartik;
/**
 * 
 * @author kartik 
 * This is multiple way addition algorithm. Mainly focusing on Booth's algorithms
 */

public class AdditionAndSubtraction {

public static int getSum(int p, int q)
{
    int result = p ^ q; // + without carry 0+0=0, 0+1=1+0=1, 1+1=0
    int carry = (p & q) << 1; // 1+1=2
    if (carry != 0) {
        return getSum(result, carry);
    }
    return result;
}

public static int subtract(int n1, int n2){
    // Add two's complement and return.
    return getSum(n1, getSum(~n2, 1));
}
public static void main(String[] args) {
int i=10;
int j=12;
int r=getSum(i,j);
System.out.println(r);
int r1=subtract(j,i);
System.out.println(r1);
}
}

Example 1 Addition and subtraction:





Previous
Next Post »