Facebook Badge

Monday, September 13, 2010

Java Program for Implementation of Restoring Division Algorithm

*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....

Thursday, September 2, 2010

Is SUCCESS different from FAILURE..?


On the eve of Semester results being announced and a lot of my friends failing to succeed in clearing them, I wanted to share with you a few words of encouragement which may help you in redefining what failure actually is!

These are not mere words of encouragement or consolation but they are really true!

Java Program for IEEE754 representation of floating point number




/*This program can find IEEE754 representation of a floating point number upto the range of long
   i.e. 9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

The program gives error result for inputs like 9,223,372,036,854,775,809.23  etc */


/**
 * @(#)IEEE754_Exp.java
 *
 *
 * @author Rohit Iyer
 * @version 1.00 2010/9/2
 */

 import java.util.*;

class IEEE754
{
    float n,f;
    long x;
    int sign;
    int pos;
    int[] exp=new int[8];
    int[] temp1=new int[128];   
     // since decimal point can shift up to max 127 places to the left

    int[] temp2=new int[128];   
     // since decimal point can shift up to max 127 places to the right.

    int[] mantissa=new int[23];
    int ans[]=new int [256];
   // ans[] is used for intermediate clalculations like finding the first 1 and for extra precision of mantissa
 
   
    public void getdata()
    {
        Scanner src=new Scanner(System.in);
        System.out.print("Enter a floating point number : ");
        n=src.nextFloat();
        if(n<0)
        {
        sign=1;
        n=n*-1;
        }
         else sign=0;

        x=(long)n;
        f=n-x;

    }

    public void binary()
    {
       
        int i=127;
                   do
                {
                         temp1[i]=(int)x%2;
                           x=x/2;
                          i--;
                 }
                 while(i>0);
   
        i=0;
       
       
        do
        {
        if(f!=1.0f)
        {
        temp2[i++]=(int)(f*2);
        f=(f*2)-(int)(f*2);
        if(f==1.0f)
                break;
        }
       
        } while(i<=127);

       

    }

    public void calcExp()
    {
        int i;
        for(i=0;i<128;i++)
        ans[i]=temp1[i];
       
        for(i=128;i<256;i++)
        ans[i]=temp2[i-128];

        pos=0;
        for(i=0;i<256;i++)
        if(ans[i]==1)
        {
        pos = i+1;
        break;
        }
       

        int ex=127+(127-i);   
        // calculates how many places shifting is required

        i=7;               //exponent is 8 bit
                   do
        {
             exp[i]=ex%2;
                           ex=ex/2;
                          i--;
                         }
                         while(i>=0);

    }

    public void calcMantissa()
    {
        int i,j=0;
        for(i=pos;j<23;i++)
        mantissa[j++]=ans[i];
    }

    public  void showdata()
    {
        int i;
        System.out.print("S\t\tE\t\t M\n");
        System.out.print(sign+"\t");
        for(i=0;i<8;i++)
        System.out.print(exp[i]);
        System.out.print("\t");
        for(i=0;i<23;i++)
        System.out.print(mantissa[i]);
    }


    }
    class IEEE754_Exp
    {
        public static void main(String args[])
        {
        IEEE754 x=new IEEE754();
        x.getdata();
        x.binary();
        x.calcExp();
        x.calcMantissa();
        x.showdata();
        }
    }