*RESTORING DIVISION*
/**
* @(#)ResDiv.java
*
*
* @author Rohit E Iyer
* @version 1.00 2010/9/2
*/
import java.util.Scanner;
public class ResDiv
{
public static int getsize(int x)
{
int c=2;
if(x<=1)
c = 2;
else if(x < 4)
c = 2;
else if(x< 8)
c = 3;
else if(x< 16)
c = 4;
else if(x< 32)
c = 5;
else if(x< 64)
c = 6;
else if(x< 128)
c = 7;
else if(x< 256)
c = 8;
else if(x< 512)
c = 9;
return c;
}
public static int max(int x,int y)
{
if(x< y)
return(y);
else
return(x);
}
public static void main(String args[])
{
Scanner src=new Scanner(System.in);
int B,Q,Z,M,c,c1,i,j,x,y,ch,in,S,G,P;
int[] a=new int[24];
int[] b=new int[12];
int[] b1=new int[12];
int[] q=new int[12];
int carry=0,count=0,option;
long num;
do
{
System.out.print("¦-----------------------------------------------¦\n");
System.out.print("¦\t\tPROGRAM FOR DIVISION\t\t¦\n");
System.out.print("¦-----------------------------------------------¦");
System.out.print("\n\nENTER DIVIDEND\t: ");
Q=src.nextInt();
y = getsize(Q);
System.out.print("ENTER DIVISOR\t: ");
M=src.nextInt();
x = getsize(M);
Z = max(x,y);
System.out.print("\n\tTOTAL BITS CONSIDERED FOR RESULT => "+(2*Z+1));
System.out.print("\n\tINITIALLY A IS RESET TO ZERO:");
for(i=0;i<=Z;i++)
System.out.print(a[i]=0);
for(i=Z;i>=0;i--)
{
b1[i] = b[i] = M%2;
M = M/2;
b1[i] = 1-b1[i];
}
carry = 1;
for(i=Z;i>=0;i--)
{
c1 = b1[i]^carry;
carry = b1[i]&carry;
b1[i]=c1;
}
for(i=2*Z;i>Z;i--)
{
a[i] = Q%2;
Q = Q/2;
}
System.out.print("\n\n\tDivisor\t\t(M)\t: ");
for(i=0;i<=Z;i++)
System.out.print(b[i]+" ");
System.out.print("\n\t2'C Divisor\t(M)\t: ");
for(i=0;i<=Z;i++)
System.out.print(b1[i]+" ");
System.out.print("\n\tDividend\t(Q)\t: ");
for(i=Z+1;i<=2*Z;i++)
System.out.print(a[i]+" ");
System.out.print("\n\nBITS CONSIDERED:[ A ] \t[ M ]");
System.out.print("\n\t\t\t\t");
for(i=0;i<=Z;i++)
System.out.print(a[i]+" ");
System.out.print(" ");
for(i=Z+1;i<=2*Z;i++)
System.out.print(a[i]+" ");
count = Z;
do{
for(i=0;i<2*Z;i++)
a[i] = a[i+1];
System.out.print("\n\nLeft Shift\t\t");
for(i=0;i<=Z;i++)
System.out.print(a[i]+" ");
System.out.print(" ");
for(i=Z+1;i<2*Z;i++)
System.out.print(a[i]+" ");
carry=0;
for(i=Z;i>=0;i--)
{
//An attempt of using Carry LookAhead Logic for Addition
S=a[i]^(b1[i]^carry);
G=a[i]&b1[i];
P=a[i]^b1[i];
carry=G|(P&carry);
a[i]=S ;
}
System.out.print("\nA< A-M \t\t");
for(i=0;i<=Z;i++)
System.out.print(a[i]+" ");
System.out.print(" ");
for(i=Z+1;i<2*Z;i++)
System.out.print(a[i]+" ");
ch=a[0];
System.out.print("\nBIT Q:"+ch);
switch (ch)
{
case 0: a[2*Z]=1;
System.out.print(" Q0< -1\t");
for(i=0;i<=Z;i++)
System.out.print(a[i]+" ");
System.out.print(" ");
for(i=Z+1;i<=2*Z;i++)
System.out.print(a[i]+" ");
break;
case 1: a[2*Z]=0;
System.out.print(" Q0< 0\t");
for(i=0;i<=Z;i++)
System.out.print(a[i]+" ");
System.out.print(" ");
for(i=Z+1;i<2*Z;i++)
System.out.print(a[i]+" ");
carry=0;
for(i=Z;i>=0;i--)
{
S=a[i]^(b[i]^carry);
G=a[i]&b[i];
P=a[i]^b[i];
carry=G|(P&carry);
a[i]=S ;
}
System.out.print("\nA< -A+M");
System.out.print("\t\t\t");
for(i=0;i<=Z;i++)
System.out.print(a[i]+" ");
System.out.print(" ");
for(i=Z+1;i<=2*Z;i++)
System.out.print(a[i]+" ");
break;
}
count--;
}while(count!=0);
num=0;
System.out.print("\n\t\t< < QUOTIENT IN BITS>> :");
for(i=Z+1;i<=2*Z;i++)
{
System.out.print(a[i]+" ");
num=num+(int)Math.pow(2,2*Z-i)*a[i];
}
System.out.print("\n\t\tQUOTIENT IN DECIMAL :"+num);
num=0;
System.out.print("\n\t\t< < REMAINDER IN BITS>>:");
for(i=0;i<=Z;i++)
{
System.out.print(a[i]+" ");
num=num+(int)Math.pow(2,Z-i)*a[i];
}
System.out.print("\n\t\tREMAINDER IN DECIMAL :"+num);
System.out.print("\n\tDO YOU WANT TO CONTINUE PRESS 0-ESC 1-CONT.:");
option=src.nextInt();
}while(option!=0);
}
}
I recommend you to read these Computer Arithmetic algorithms at least once from
Computer Organisation and Architecture - Stallings
Superb explanation....

