Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

booth multiplication -URGENT-

Hi Everybody,

I need a function that does the booth multiplication, if someone can help me, I would be very thnkful.

Thank you very much in advance.

Comments

  • ChinjooChinjoo Member Posts: 1
    : Hi Everybody,
    :
    : I need a function that does the booth multiplication, if someone can help me, I would be very thnkful.
    :
    : Thank you very much in advance.
    :

    Here is ur program dear:
    /* Program to implement Booth's multiplication algorithm */
    #include
    #include
    #include
    long a=0,q=0;
    int p=0,i=1;
    long addition(long a,long b)
    {
    long c=0;
    int j=0;
    int n1,n2,sum,cy=0;
    while(a!=0 || b!=0)
    {
    n1=a%10;
    n2=b%10;
    sum=n1^n2^cy;
    cy=(n1&(n2|cy))|(n2&cy);
    c+=sum*pow(10,j);
    j++;
    a/=10;
    b/=10;
    }
    if(j==2)
    c+=cy*pow(10,++j);
    printf("
    %ld %ld %d Addition",a,q,p);
    return c;
    }
    long subtract(long a,long b)
    {
    long c=0;
    int j=0,n1,n2,dif=0,bo=0;
    while(a!=0 || b!=0)
    {
    n1=a%10;
    n2=b%10;
    dif=n1^n2^bo;
    bo=(~n1&(n2|bo))|(n2&bo);
    c+=dif*pow(10,j);
    j++;
    a/=10;
    b/=10;
    }
    while(j<4)
    c+=bo*pow(10,j++);
    printf("
    %ld %ld %d Subtraction",a,q,p);
    return c;
    }

    void shift()
    {
    int a1,a4,q4;
    a1=a/1000;
    a4=a%10;
    a=a/10;
    a=a+(a1*1000);
    q4=q%10;
    q=q/10;
    q=q+a4*1000;
    p=q4;
    printf("
    %ld %ld %d Shift ",a,q,p);
    }
    long dectobin(long a)
    {
    long c=0;
    int i=0;
    while(a>0)
    {
    if((a%2)==1)
    c=c+pow(10,i);
    i++;
    a=a/2;
    }
    return c;
    }

    long twoscomp(long a)
    {
    long c=0;
    int i=0,n1,j;
    while(a!=0)
    {
    n1=a%10;
    if(n1==0)
    c+=pow(10,i);
    i++;
    a/=10;
    }
    if((i%4)!=0)
    {
    for(j=(i%4);j<4;j++)
    {
    c+=pow(10,i);
    i++;
    }
    }
    c=addition(c,1);
    return c;
    }

    void main()
    {
    int c;
    long x,y,m;
    clrscr();
    printf("
    Enter 1st no in Decimal : ");
    scanf("%ld",&x);
    printf("
    Enter 2nd no in Decimal : ");
    scanf("%ld",&y);
    q=dectobin(abs(x));
    printf("

    First binary number = %ld",q);
    m=dectobin(abs(y));
    printf("

    Second binary number = %ld",m);
    if(x<0)
    {
    q=twoscomp(q);
    printf("

    Two's complement of first binary number = %ld",q);
    }
    if(y<0)
    {
    m=twoscomp(m);
    printf("

    Two's complement of second binary number = %ld",m);
    }
    printf("

    A Q Q_-1 ");
    c=4;
    while(c!=0)
    {
    while(((q%10)==0 && p==0) || ((q%10)==1 && p==1))
    {
    printf("

    Step %d:",i);
    shift();
    i++;
    c--;
    }
    while((q%10)==0 && p==1)
    { printf("

    In Step %d:",i);
    a=addition(a,m);

    shift();
    i++;
    c--;
    }
    while((q%10)==1 && p==0)
    { printf("

    In Step %d:",i);
    a=subtract(a,m);

    shift();
    i++;
    c--;
    }

    }
    printf("

    The answer is = %ld",a*10000+q);
    getch();
    }

    /* Program Output:

    Enter 1st no in Decimal : 5

    Enter 2nd no in Decimal : 4


    First binary number = 101

    Second binary number = 100

    A Q Q_-1

    Step 1:
    0 101 0 Subtraction
    1110 10 1 Shift

    Step 2:
    0 10 1 Addition
    1 1 0 Shift

    Step 3:
    0 1 0 Subtraction
    1110 1000 1 Shift

    Step 4:
    0 1000 1 Addition
    1 100 0 Shift

    The answer is = 10100

    */

















































































































Sign In or Register to comment.