P. 1
ResDiv

ResDiv

|Views: 0|Likes:
Published by Viraj Shah

More info:

Published by: Viraj Shah on Oct 20, 2011
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as TXT, PDF, TXT or read online from Scribd
See more
See less

10/20/2011

pdf

text

original

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 maint x,int y)
{
if(x< y)
return;
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\nENTER DIVIDEND\t: ");
Q=src.nextInt();
y = getsize(Q);
System.out.print("ENTER DIVISOR\t: ");
M=src.nextInt();
x = getsize(M);
Z = max,y);
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.println();
System.out.println();
System.out.println();
/*
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];
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);
}
}

println().i++) System.i--) { c1 = b1[i]^carry.i<=Z.out.out. System. for(i=0.println().print("\n\nLeft Shift\t\t"). for(i=0. System. for(i=Z.out.i++) System.print("\n\nBITS CONSIDERED:[ A ] \t[ M ]"). /* System.out.println().print(" ").print(b[i]+" ").for(i=Z.out. for(i=Z+1.i++) a[i] = a[i+1].i<=Z. for(i=0.i++) System. for(i=Z+1.print(a[i]+" "). System.out.print(a[i]+" "). System.print(b1[i]+" ").i>=0.out. Q = Q/2. System.out.print(a[i]+" ").i++) System. b1[i] = 1-b1[i]. M = M/2.out.i<=2*Z.i--) { //An attempt of using Carry LookAhead Logic for Addition .i<2*Z. System. */ count = Z.print(a[i]+" ").out. } System.i>=0.out. for(i=0.i<=2*Z.out.out.i<=Z. System.out. carry = b1[i]&carry.print("\n\t2'C Divisor\t(M)\t: ").i--) { b1[i] = b[i] = M%2.i++) System.out. } carry = 1.out. do{ for(i=0.out.i++) System. for(i=Z+1. for(i=Z.print("\n\t\t\t\t").print("\n\n\tDivisor\t\t(M)\t: "). b1[i]=c1.print(a[i]+" ").print("\n\tDividend\t(Q)\t: ").i++) System. /* System.i<2*Z. System. */ carry=0.print(" ").out. } for(i=2*Z.i--) { a[i] = Q%2.i<=Z.i>Z.i>=0.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->