Facebook Badge

Thursday, October 21, 2010

Page Replacement Policy: Least Recently Used (LRU)

import java.util.*;

class LRUExp
{

public static int min(int[] x)
{
int min=x[0],i,pos=0;
for(i=1;i<x.length;i++)
if(min>x[i])
{
min=x[i];
pos=i;
}

return pos;
}
public static boolean contains(int[]a,int x)
{
for(int i=0;i<a.length;i++)
if(a[i]==x)
return true;
return false;
}

public static void main(String args[])
{
Scanner src=new Scanner(System.in);

int count[]=new int[4];
int a[]=new int[16];
int b[]=new int[4];
int m=0,i;
System.out.print("Enter 16 elements:");
for(i=0;i<16;i++)
a[i]=src.nextInt();
int f=1; //counter used like timing unit.
int g=0;

for(i=0;i<4;i++)
{
b[i]=a[i];
count[i]=f++;
}

for(int j=0;j<16;j++)
{
g=0;
for(i=0;i<4;i++)
if(b[i]==a[j]) //Page already presnt in RAM
{
System.out.print("\nNo page fault\n");
count[i]=f++;
g=0;
break;
}
else
g=1;
if(g==1) // Page fault
{
System.out.print("\nPage fault\n");
m=min(count);
System.out.print("\nPage "+b[m]+" replaced by "+a[j]+"\n");
//m is INDEX of element which is least recently used
b[m]=a[j];
count[m]=f++;
}
}
}
}

Page Replacement Policy : First In First Out (FIFO)

/**
* @(#)PageRep.java
*
*
* @author
* @version 1.00 2010/10/3
*/

import java.util.*;

class Queue
{
int a[];
int front,rear;

public Queue(int n)
{
front=rear=-1;
a=new int [n];
for(int i=0;i<a.length;i++)
a[i]=-1;
}

public void insert(int x)
{
if(rear==a.length-1)
System.out.print("Queue overflow");
else
{
rear++;
a[rear]=x;
if(front==-1)
front=0;
}
}

public int delete()
{
if(front==-1)
{
//System.out.print("Queue underflow");
}

else
{
//System.out.print("Element deleted is "+a[front]);
}
return(a[front++]);
}

public void display()
{
int i;
if(front==-1)
System.out.println("\n********Queue empty**********");
else
for(i=front;i<=rear;i++)
System.out.print(a[i]+" ");
}

public void destroy()
{
System.out.println("\n***********Queue is destroyed***********");
front=rear-1;
}
public boolean isFull()
{
if(rear==a.length-1)
return true;
else
return false;
}
public boolean contains(int ele)
{
for(int i=0;i<a.length;i++)
if(a[i]==ele)
return true;

return false;
}
public boolean isEmpty()
{
if(front==-1)
return true;
else
return false;
}

}


class PageRep
{

public static void main(String[] args)
{
int ram[]=new int[4];
int n=0;
int age[]=new int[4];
int use[]=new int[4];
int p=0,x=0,ch=0;
int count=0;

Scanner src=new Scanner(System.in);
Queue q=new Queue(4);
do
{
System.out.print("Enter page request :");
p=src.nextInt();
if(count<ram.length && !q.contains(p))
ram[count++]=p;

if(!q.isFull()&&!q.contains(p))
{
q.insert(p);

}
if(q.isFull() && !q.contains(p))
{
System.out.print("\n\nRAM is full.. Replacement Algorithm needed..\n\n");
if(!q.isEmpty()) x=q.delete();
System.out.print("\nPage removed is :"+x);
for(int i=0;i<ram.length;i++)
if(ram[i]==x)
ram[i]=p;
}
System.out.print("\nRam contains following pages..\n");
for(int i=0;i<count;i++)
System.out.print(ram[i]+" ");
System.out.print("\nDo u want to continue: 1/0: ");
ch=src.nextInt();
}while(ch!=0);

}


}

Java Implementation of Non Restoring Division

import java.util.*;

class ResDiv
{

static int a[]={0,0,0,0,0},q[]=new int[4],b[]=new int[5],b2c[]=new int[5];

public static void comp()
{
int i=4;
do
{
b2c[i]=b[i];
i--;
}while(b[i+1]!=1);
while(i>=0)
{
b2c[i]=(b[i]+1)%2;
i--;
}
System.out.print("\n\tB's complement:");
for(i=0;i< 5;i++)
System.out.print(b2c[i]);
System.out.print("\n");
}


public static void nonresdiv()
{
shiftleft();
if(a[0]==0)
a_minus_b();
else
a_plus_b();
q[3]=(a[0]+1)%2;
}

public static void shiftleft()
{
int i;
for(i=0;i< 4;i++)
a[i]=a[i+1];
a[4]=q[0];
for(i=0;i< 3;i++)
q[i]=q[i+1];
}

public static void a_minus_b()
{
int i,carry=0,sum=0;
for(i=4;i>=0;i--)
{
sum=(a[i]+b2c[i]+carry);
a[i]=sum%2;
carry=sum/2;
}
}

public static void a_plus_b()
{
int i,carry=0,sum=0;
for(i=4;i>=0;i--)
{
sum=(a[i]+b[i]+carry);
a[i]=sum%2;
carry=sum/2;
}
}


public static void main(String args[])
{
Scanner src=new Scanner(System.in);
int i,j,k;
System.out.print("Enter dividend in binary form\t: ");
for(i=0;i< 4;i++)
q[i]=src.nextInt();;
System.out.print("Enter divisor in binary form\t: ");
for(i=0;i< 5;i++)
b[i]=src.nextInt();
comp();
System.out.print("\n\t[A]\t[M]\n");
for(i=0;i< 4;i++)
{
nonresdiv();
System.out.print("\t");
for(j=0;j< 5;j++)
System.out.print(a[j]);
System.out.print("\t");
for(k=0;k< 4;k++)
System.out.print(q[k]);
System.out.print("\n");
}
if(a[0]==1)
a_plus_b();
System.out.print("\t");
for(j=0;j< 5;j++)
System.out.print(a[j]);
System.out.print("\t");
for(k=0;k< 4;k++)
System.out.print(q[k]);
System.out.print("\n");
System.out.print("\n\tThe Quotient Is\t: ");
for(k=0;k< 4;k++)
System.out.print(q[k]);
System.out.print("\n\tThe Remainder Is\t: ");
for(j=0;j< 5;j++)
System.out.print(a[j]);
}
}

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();
        }
    }

Wednesday, August 25, 2010

Java Program for implementation of Booth's Algorithm

/**

* @(#)Booth.java

*

*

* @author Rohit.E.Iyer

* @version 1.00 2010/8/22

*/



/* Java Program For Implementation Of Signed Multiplication

OR

Java Program for the Implementation Of Booth's Algorithm */



import java.util.*;



class Booth

{

public static int get(int a)

{

Scanner src=new Scanner(System.in);

char ch='B';

int flag=0;

if(a==1)

ch='A';

do

{

System.out.print("¦ ENTER VALUE OF "+ch+" : ");

a=src.nextInt();;

if(a< 0)

{

a = a * -1;

flag = 1;

}

if(127<a)

System.out.print("¦\n!INVALID NUMBER.ENTER VALUE (-127 < A < 128)!");

}while(127< a);

if(flag==1)

a = a *-1;

return(a);

}



public static void add(int a[],int b[])

{

int x,i,c=0;

for(i=7;i>=0;i--)

{

x=a[i];

a[i]=c^x^b[i];

if(((c==1)&&(x==1))||((x==1)&&(b[i]==1))||((b[i]==1)&&(c==1)))

c = 1;

else

c = 0;

}

}



public static void binary(int x,int arr[])

{

int i,p=x;

int[]c={0,0,0,0,0,0,0,1};

for(i=0;i< 8;i++)

arr[i] = 0;

if(x < 0)

x = x *-1;

i = 7;

do

{

arr[i]=x%2;

x = x/2;

i--;

}while(x!=0);

if(p< 0)

{

for(i=0;i< 8;i++)

arr[i]=1-arr[i];

add(arr,c);

}

System.out.print("\n\nTHE BINARY EQUIVALENT OF "+p+" IS : ");

for(i=0;i< 8;i++)

System.out.print(arr[i]);

}



public static void rshift(int x,int y[])

{

int i;

for(i=7;i>0;i--)

y[i] = y[i-1];

y[0] = x;

}



public static void main(String args[])

{

int q=0,i,j,a,b,temp;

int[] A={0,0,0,0,0,0,0,0},C={0,0,0,0,0,0,0,1},C1={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};

int s=0,z=0;

int[] Q=new int[8];

int[] M=new int[8];

int[] temp1=new int [8];

int[] ans=new int[16];

int y,x=0,c=0;



System.out.print("\n¦----------------------------------------------------\n");

a = get(1);

b=get(0);

System.out.println("\n¦---------------------------------------------------\n");

binary(a,M);

binary(b,Q);

System.out.print("\n\n---------------------------------------------------\n");

System.out.print(" OPERATION\t\t A\t\t Q\t\t Q'\t M");

System.out.print("\n\n INITIAL\t\t");

for(i=0;i< 8;i++)

System.out.print(A[i]);

System.out.print("\t");

for(i=0;i< 8;i++)

System.out.print(Q[i]);

System.out.print("\t");

System.out.print(q+"\t");

for(i=0;i< 8;i++)

System.out.print(M[i]);

for(j=0;j< 8;j++)

{

if((Q[7]==0)&&(q==1))

{

System.out.print("\n A=A+M \t\t\t");

add(A,M);

for(i=0;i< 8;i++)

System.out.print(A[i]);

System.out.print("\t");

for(i=0;i< 8;i++)

System.out.print(Q[i]);

System.out.print("\t"+q+"\t");

for(i=0;i< 8;i++)

System.out.print(M[i]);

}

if((Q[7]==1)&&(q==0))

{

System.out.print("\n A=A-M \t\t\t");

for(i=0;i< 8;i++)

temp1[i] = 1-M[i];

add(temp1,C);

add(A,temp1);

for(i=0;i< 8;i++)

System.out.print(A[i]);

System.out.print("\t");

for(i=0;i< 8;i++)

System.out.print(Q[i]);

System.out.print("\t"+q+"\t");

for(i=0;i< 8;i++)

System.out.print(M[i]);

}

System.out.print("\n Shift \t\t\t");

y = A[7];

q = Q[7];

rshift(A[0],A);

rshift(y,Q);

for(i=0;i< 8;i++)

System.out.print(A[i]);

System.out.print("\t");

for(i=0;i< 8;i++)

System.out.print(Q[i]);

System.out.print("\t");

System.out.print(q+"\t");

for(i=0;i< 8;i++)

System.out.print(M[i]);

}

System.out.print("\n\n---------------------------------------------------\n");

System.out.print("\nTHE ANSWER IN BINARY IS : ");

for(i=0;i< 8;i++)

ans[i]=A[i];

for(i=0;i< 8;i++)

ans[i+8]=Q[i];

if(((a< 0)&&(b>0))

((a>0)&&(b< 0)))

{

for(i=0;i< 16;i++)

ans[i]=1-ans[i];

for(i=15;i>=0;i--)

{

x = ans[i];

ans[i]=c^x^C1[i];

if(((c==1)&&(x==1))||((x==1)&&(C1[i]==1))||((C1[i]==1)&&(c==1)))

c=1;

else

c=0;

}

}

for(i=0;i< 16;i++)

System.out.print(ans[i]);

for(i=15;i>=0;i--)

{

s = s + ((int)Math.pow(2,z) * ans[i]);

z = z+1;

}

if(((a< 0)&&(b>0))||((a>0)&&(b< 0)))

System.out.print("\nTHE ANSWER IN DECIMAL IS :- "+s+"\n");

else

System.out.print("\nTHE ANSWER IN DECIMAL IS : "+s+"\n");



}

}

Sunday, August 22, 2010

My Speech

This is the speech that I gave on the occassion of FE induction programme of VIT

Date:      23rd August,2010 

Venue:   Reading Hall,
              Vidyalankar Institute of Technology.



-----------------------------------------------------------------------------
Good Morning everyone…!!







Firstly, I thank my institute for giving me an opportunity to share my opinions with the new-comers in this institute.




Let me ask you …..


What is your ultimate goal in your life? To be successful and affluent? Just to be happy? To achieve a unique identity in society? Or, if you have already matured as a person of substance, to give back something to society? Or to seek pleasure and nothing but pleasure as a goal of life?


Your answer to this question may be, “Probably all these, though that’s quite impossible.” Yet my purpose here is to share with you a single formula that, I believe, can help a person as it has at least helped me realize all the above goals and more.




As you know “Success doesn’t come without Perseverance”.


Mere Perseverance is not essential. What is more crucial is that Perseverance must be coupled with PROPER GUIDANCE.


While the first one depends wholly on an individual, the second one I gained from Vidyalankar. To say that VIT teachers guided me to SUCCESS would be an under-statement because they served not only as mere guides to me but they also proved to be an indispensable source of knowledge in my course of First Year of Engineering... Ocean of knowledge... I was thirsty.... They quenched my thirst.


I am here to share with you what made me worthy enough to be delivering this speech.






If you want to get what everyone is getting, do what everyone is doing.


If you want to excel do what others don’t! BE DIFFERENT!


Being different means: - Not being afraid to challenge the norm. - Being willing to take a chance.- Asking why.- Making your own track, not just following the well trodden path.- Charting your own course and destiny.- Being the person that you were meant to be.






MY ADVICE?


If I can leave you with one piece of advice it would be this: READ


Society celebrates mediocrity so much that it does not take much to set yourself above the rest. Doing that one thing regularly will put you way above the rest. Read books that challenge you and that make you think.


I have decided to read at least one book every month. I’m already amazed at the results.






MY FINAL WORDS


In closing I’d like to say to those of you that are skeptics out there, those of you that are saying “oh, he is just experienced ripe success. He’ll get over it, and then he’ll be just like everyone else.”


My words to you are:


“I am not going out like that. I know that I have an abundance of potential within me and I will bring it out to use. Why?”


“Because…I’M DIFFERENT!”


Lastly I would like to thank VIT for making me a student worthy enough of delivering this speech. I wish them good luck in continuing their arduous task of providing students with an education of superlative quality for the years to come.






Thank you…


-----------------------------------------------------------------------------


Saturday, August 21, 2010

Carry LookAhead Adder

Theory of Operation


 
A ripple-carry adder works in the same way as pencil-and-paper methods of addition. Starting at the rightmost (least significant) digit position, the two corresponding digits are added and a result obtained. It is also possible that there may be a carry out of this digit position (for example, in pencil-and-paper methods, "9+5=4, carry 1"). Accordingly all digit positions other than the rightmost need to take into account the possibility of having to add an extra 1, from a carry that has come in from the next position to the right.

 

 

 
This means that no digit position can have an absolutely final value until it has been established whether or not a carry is coming in from the right. Moreover, unless the sum without a carry is 9 (in pencil-and-paper methods) or 1 (in binary arithmetic), it is not even possible to tell whether or not a given digit position is going to pass on a carry to the position on its left. At worst, when a whole sequence of sums comes to ...99999999... (in decimal) or ...11111111... (in binary), nothing can be deduced at all until the value of the carry coming in from the right is known, and that carry is then propagated to the left, one step at a time, as each digit position evaluated "9+1=0, carry 1" or "1+1=0, carry 1". It is the "rippling" of the carry from right to left that gives a ripple-carry adder its name, and its slowness. When adding 32-bit integers, for instance, allowance has to be made for the possibility that a carry could have to ripple through every one of the 32 one-bit adders.

 

 

 
Carry lookahead depends on two things:

 

 

 
1.Calculating, for each digit position, whether that position is going to propagate a carry if one comes in from the right.

 
2.Combining these calculated values so as to be able to deduce quickly whether, for each group of digits, that group is going to propagate a carry that comes in from the right.

 
Supposing that groups of 4 digits are chosen. Then the sequence of events goes something like this:

 

 

 
  • 1.All 1-bit adders calculate their results. Simultaneously, the lookahead units perform their calculations.


  • 2.Suppose that a carry arises in a particular group. Within at most 3 gate delays, that carry will emerge at the left-hand end of the group and start propagating through the group to its left.

  • 3.If that carry is going to propagate all the way through the next group, the lookahead unit will already have deduced this. Accordingly, before the carry emerges from the next group the lookahead unit is immediately (within 1 gate delay) able to tell the next group to the left that it is going to receive a carry - and, at the same time, to tell the next lookahead unit to the left that a carry is on its way.

 
The net effect is that the carries start by propagating slowly through each 4-bit group, just as in a ripple-carry system, but then move 4 times as fast, leaping from one lookahead carry unit to the next. Finally, within each group that receives a carry, the carry propagates slowly within the digits in that group.

 

 

 
The more bits in a group, the more complex the lookahead carry logic becomes, and the more time is spent on the "slow roads" in each group rather than on the "fast road" between the groups (provided by the lookahead carry logic). On the other hand, the fewer bits there are in a group, the more groups have to be traversed to get from one end of a number to the other, and the less acceleration is obtained as a result.

 

 

 
Deciding the group size to be governed by lookahead carry logic requires a detailed analysis of gate and propagation delays for the particular technology being used.

 

 

 
It is possible to have more than one level of lookahead carry logic, and this is in fact usually done. Each lookahead carry unit already produces a signal saying "if a carry comes in from the right, I will propagate it to the left", and those signals can be combined so that each group of (let us say) four lookahead carry units becomes part of a "supergroup" governing a total of 16 bits of the numbers being added. The "supergroup" lookahead carry logic will be able to say whether a carry entering the supergroup will be propagated all the way through it, and using this information, it is able to propagate carries from right to left 16 times as fast as a naive ripple carry. With this kind of two-level implementation, a carry may first propagate through the "slow road" of individual adders, then, on reaching the left-hand end of its group, propagate through the "fast road" of 4-bit lookahead carry logic, then, on reaching the left-hand end of its supergroup, propagate through the "superfast road" of 16-bit lookahead carry logic.

 

 

 
Again, the group sizes to be chosen depend on the exact details of how fast signals propagate within logic gates and from one logic gate to another.

 

 

 
For very large numbers (hundreds or even thousands of bits) lookahead carry logic does not become any more complex, because more layers of supergroups and supersupergroups can be added as necessary. The increase in the number of gates is also moderate: if all the group sizes are 4, one would end up with one third as many lookahead carry units as there are adders. However, the "slow roads" on the way to the faster levels begin to impose a drag on the whole system (for instance, a 256-bit adder could have up to 24 gate delays in its carry processing), and the mere physical transmission of signals from one end of a long number to the other begins to be a problem. At these sizes carry-save adders are preferable, since they spend no time on carry propagation at all.



  
Carry lookahead method


 
Carry lookahead logic uses the concepts of generating and propagating carries. Although in the context of a carry lookahead adder, it is most natural to think of generating and propagating in the context of binary addition, the concepts can be used more generally than this. In the descriptions below, the word digit can be replaced by bit when referring to binary addition.

 

 

 
The addition of two 1-digit inputs A and B is said to generate if the addition will always carry, regardless of whether there is an input carry (equivalently, regardless of whether any less significant digits in the sum carry). For example, in the decimal addition 52 + 67, the addition of the tens digits 5 and 6 generates because the result carries to the hundreds digit regardless of whether the ones digit carries (in the example, the ones digit does not carry (2+7=9)).

 

 

 
In the case of binary addition, A + B generates if and only if both A and B are 1. If we write G(A,B) to represent the binary predicate that is true if and only if A + B generates, we have:

 
G(A,B) = A.B
 
The addition of two 1-digit inputs A and B is said to propagate if the addition will carry whenever there is an input carry (equivalently, when the next less significant digit in the sum carries). For example, in the decimal addition 37 + 62, the addition of the tens digits 3 and 6 propagate because the result would carry to the hundreds digit if the ones were to carry (which in this example, it does not). Note that propagate and generate are defined with respect to a single digit of addition and do not depend on any other digits in the sum.


 

 

 
In the case of binary addition, A + B propagates if and only if at least one of A or B is 1. If we write P(A,B) to represent the binary predicate that is true if and only if A + B propagates, we have:

 

P(A,B) = A+B


Sometimes a slightly different definition of propagate is used. By this definition A + B is said to propagate if the addition will carry whenever there is an input carry, but will not carry if there is no input carry. It turns out that the way in which generate and propagate bits are used by the carry lookahead logic, it doesn't matter which definition is used. In the case of binary addition, this definition is expressed by:

P(A,B) = A XOR B

For binary arithmetic, or is faster than xor and takes fewer transistors to implement. However, for a multiple-level carry lookahead adder, it is simpler to use the second i.e P(A,B) = A XOR B.


 

 
Given these concepts of generate and propagate, when will a digit of addition carry? It will carry precisely when either the addition generates or the next less significant bit carries and the addition propagates. Written in boolean algebra, with Ci the carry bit of digit i, and Pi and Gi the propagate and generate bits of digit i respectively,

C(i+1)=A.B + (A XOR B).C(i)

C(i+1)=A.B +  P(i) . C(i)


This approach of calculating carry is faster if we compare it with that in Ripple carry Adder


 

 

 Lets assume that the delay through an AND gate is one gate delay and through an XOR gate is two gate delays. Notice that the Propagate and Generate terms only depend on the input bits and thus will be valid after two and one gate delay, respectively. If one uses the above expression to calculate the carry signals, one does not need to wait for the carry to ripple through all the previous stages to find its proper value. Let’s apply this to a 4-bit adder to make it clear.



C1 = G0 + P0.C0                                                                                    (5)

C2 = G1 + P1.C1 = G1 + P1.G0 + P1.P0.C0                                              (6)

C3 = G2 + P2.G1 + P2.P1.G0 + P2.P1.P0.C0                                     (7)

C4 = G3 + P3.G2 + P3.P2.G1 + P3P2.P1.G0 + P3P2.P1.P0.C0         (8)

Notice that the carry-out bit, Ci+1, of the last stage will be available after four delays (two gate delays to calculate the Propagate signal and two delays as a result of the AND and OR gate).

AND THIS IS INDEPENDENT OF THE NUMBER OF BITS THAT ARE TO BE ADDED !!

 The Sum signal can be calculated as follows,

Si = AXOR Bi XOR Ci = Pi  XOR  Ci                        (9)

The Sum bit will thus be available after two additional gate delays (due to the XOR gate) or a total of six gate delays after the input signals Ai and Bi have been applied. The advantage is that these delays will be the same independent of the number of bits one needs to add, in contrast to the ripple counter.

BECAUSE IN RIPPLE COUNTER THE DELAY INCREASES WITH INCREASE IN NUMBER OF BITS TO BE ADDED....



Hope this artcle helped you...





Regards,
Rohit.



 

 

 

 

Friday, August 20, 2010

C Program For Implementation Of Look Ahead Carry Adder

/* C Program For Implementation Of Look Ahead Carry Adder */

#include< stdio.h>
#include< conio.h>
#include< math.h>

int get1(int a)
{
char ch='B';
if(a==1)
ch='A';
do
{
printf("\n\tENTER VALUE OF %c:",ch);
scanf("%d",&a);
if(a< =0)
printf("\n\t\t!INVALID NUMBER.ENTER VALUE (0< A)!");
}while(a< =0);
return(a);
}

int and(int a,int b)
{
int c;
if(a< b)
c=a;
else
c=b;
return (c);
}

int or(int a,int b)
{
int x;
if(a>b)
x=a;
else
x=b;
return x;
}

int exor(int a,int b)
{
int x;
if(a==b)
x=0;
else
x=1;
return x;
}


void add()
{
int i=7,A,B,a,b,cin,num;
int n1[8],n2[8],cg[8],cp[8],sum[8];
for(i=0;i< =7;i++)
{
n1[i]=0; // Num 1
n2[i]=0; // Num 2
cg[i]=0; // Gi
cp[i]=0; // Pi
sum[i]=0; // Sum
}
A = a = get1(1);
B = b = get1(0);
i=7;
do
{
n1[i]=a%2;
a=a/2;
n2[i]=b%2;
b=b/2;
i--;
}while((a!=0)||(b!=0));
i=0;
printf("\n\t\t Binary Form",A);
printf("\n\t A = %d : ",A);
for(i=0;i< =7;i++)
printf("%d ",n1[i]);
printf("\n\t B = %d : ",B);
for(i=0;i< =7;i++)
printf("%d ",n2[i]);
cin=0;
for(i=7;i>=0;i--)
{
sum[i]=exor(cin,exor(n1[i],n2[i])); // Sum Pi (+) Bi
cg[i]=and(n1[i],n2[i]); // Gi = Ai . Bi
cp[i]=or(n1[i],n2[i]); // Pi = Ai (+) Bi
cin=or(cg[i],and(cp[i],cin)); // Cin =Gi + PiCi
}
printf("\n\n\t\t SUM: ");
num=0;
for(i=0;i< =7;i++)
{
printf(" %d",sum[i]);
num=num + (sum[i]*pow(2,7-i));
}
printf("\n\n\t\t SUM: %d + %d= %d\n",A,B,num);
printf("\t\t The Carry Is : %d\n\n",cin);
}

void main()
{
int ch,a,b,c,d;
clrscr();
while(1)
{
M: printf("******** MENU FOR LOOK AHEAD CARRY ADDER ********");
printf("\n\t\t1.ADDITION OF TWO NUMBER");
printf("\n\t\t2.EXIT\n");
printf("*************************************************");
printf("\n\t\tEnter Your Option:");
scanf("%d",&ch);
switch(ch)
{
case 1:
add();
getch();
break;

case 2: exit(0);
break;

default:
clrscr();
printf("ERROR!!!!!!!!! INVALID ENTRY...\n");
printf("Back To Main Menu\n\n");
goto M;
}
}
}

C program for Non-Restoring Division Algorithm implementation

/*C program for Non-Restoring Division Algorithm implementation */


#include< stdio.h>
#include< conio.h>

int a[5]={0,0,0,0,0},q[4],b[5],b2c[5];

comp()
{
int i=4;
do
{
b2c[i]=b[i];
i--;
}while(b[i+1]!=1);
while(i>=0)
{
b2c[i]=(b[i]+1)%2;
i--;
}
printf("\n\tB's complement:");
for(i=0;i< 5;i++)
printf("%d",b2c[i]);
printf("\n");
}


nonresdiv()
{
shiftleft();
if(a[0]==0)
a_minus_b();
else
a_plus_b();
q[3]=(a[0]+1)%2;
}

shiftleft()
{
int i;
for(i=0;i< 4;i++)
a[i]=a[i+1];
a[4]=q[0];
for(i=0;i< 3;i++)
q[i]=q[i+1];
}

a_minus_b()
{
int i,carry=0,sum=0;
for(i=4;i>=0;i--)
{
sum=(a[i]+b2c[i]+carry);
a[i]=sum%2;
carry=sum/2;
}
}

a_plus_b()
{
int i,carry=0,sum=0;
for(i=4;i>=0;i--)
{
sum=(a[i]+b[i]+carry);
a[i]=sum%2;
carry=sum/2;
}
}


void main()
{
int i,j,k;
clrscr();
printf("Enter dividend in binary form\t: ");
for(i=0;i< 4;i++)
scanf("%d",&q[i]);
printf("Enter divisor in binary form\t: ");
for(i=0;i< 5;i++)
scanf("%d",&b[i]);
comp();
printf("\n\t[A]\t[M]\n");
for(i=0;i< 4;i++)
{
nonresdiv();
printf("\t");
for(j=0;j< 5;j++)
printf("%d",a[j]);
printf("\t");
for(k=0;k< 4;k++)
printf("%d",q[k]);
printf("\n");
}
if(a[0]==1)
a_plus_b();printf("\t");
for(j=0;j< 5;j++)
printf("%d",a[j]);
printf("\t");
for(k=0;k< 4;k++)
printf("%d",q[k]);
printf("\n");
printf("\n\tThe Quotient Is\t: ");
for(k=0;k< 4;k++)
printf("%d",q[k]);
printf("\n\tThe Remainder Is\t: ");
for(j=0;j< 5;j++)
printf("%d",a[j]);
getch();
}