0% found this document useful (0 votes)
5 views7 pages

DLCAPrac

The document implements Booth's multiplication and restoring division algorithms in C code. It takes two integers as input, converts them to binary, and performs the multiplication or division using the respective algorithm. For multiplication, it initializes arrays to store the multiplier, multiplicand, product, and performs bit shifts and additions. For division, it initializes arrays for the divisor, dividend, quotient, performs bit shifts, subtractions and additions. It outputs the steps and final result at each iteration.

Uploaded by

sashurosh16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

DLCAPrac

The document implements Booth's multiplication and restoring division algorithms in C code. It takes two integers as input, converts them to binary, and performs the multiplication or division using the respective algorithm. For multiplication, it initializes arrays to store the multiplier, multiplicand, product, and performs bit shifts and additions. For division, it initializes arrays for the divisor, dividend, quotient, performs bit shifts, subtractions and additions. It outputs the steps and final result at each iteration.

Uploaded by

sashurosh16
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 7

Experiment no: 07

Implement Booth’s multiplication algorithm


#include <stdio.h>
#include <math.h>
int AQQ0[100], AQQ0Size, Q[100], QSize, M[100], MSize, M2C[100];
int PrintArr(int *Target, int TargetSize);
int Add(int *ToAdd, int ToAddSiize);
int RShift();
int IntToBin(int Num, char Target);
int Twos();
int main(){
int Multiplier, Multiplicand, Temp[100], MNeg = 0, M1, M2;
printf("\n\tEnter Number 1:"); scanf("%d", &M1);
printf("\n\tEnter Number 2:"); scanf("%d", &M2);

if(M1>M2){
Multiplicand = M1;
Multiplier = M2;
}
else{
Multiplicand = M2;
Multiplier = M1;
}

IntToBin(abs(Multiplier), 'M');
IntToBin(Multiplicand, 'Q');
while(MSize!=QSize){
MSize++;
for(int i = MSize; i>=1; i--){
M[i]=M[i-1];
}
printf("\n");
PrintArr(&M, MSize);
M[0]=0;
}

Twos();
if(Multiplier<0){

for(int i = 0; i<=MSize; i++){


M[i] = abs(M2C[i]+M[i]);
M2C[i] = abs(M[i]-M2C[i]);
M[i] = abs(M[i]-M2C[i]);
}
}
AQQ0Size = 2*(QSize+1);

for(int i = 0; i<=AQQ0Size; i++){


if(i<=QSize||i == AQQ0Size){
AQQ0[i]=0;
}
else{
AQQ0[i]=Q[i-QSize];
}
}
printf("\n\tMultiplier(M):\t\t"); PrintArr(&M, MSize);
printf("\n\tMultiplicand (A):\t"); PrintArr(&Q, QSize);
printf("\n\t2s compliment(M):\t");PrintArr(&M2C, MSize);
printf("\n\tA Q Q0:\t\t"); PrintArr(&AQQ0, AQQ0Size);
for(int i = 0; i<=QSize+1; i++){
int QQ0 = AQQ0[AQQ0Size-1]*10+AQQ0[AQQ0Size];
printf("\n\t----Count:%d----", i);
if(QQ0 == 10){
Add(&M2C, MSize);
printf("\n\t"); PrintArr(&AQQ0, AQQ0Size); printf("\tA<-A-M");

}
if(QQ0 == 1){
Add(&M, MSize);
printf("\n\t"); PrintArr(&AQQ0, AQQ0Size); printf("\tA<-A+M");
}
RShift();
printf("\n\t"); PrintArr(&AQQ0, AQQ0Size); printf("\tShift right");

}
printf("\n\nFinal answer:\t"); PrintArr(&AQQ0, AQQ0Size-1);
return 0;
}

int IntToBin(int Num, char Target){


int Temp[100];

int Size = 0;
for(int i = 0; Num != 0; i++){
Temp[i] = abs(Num%2);
Num = Num/2;
Size = i;

}
Size = Size+1;
if(Num<0){
Temp[Size] = 1;

}
else{
Temp[Size] = 0;
}
for(int i = Size; i>=0; i--){
switch(Target){
case 'M':
M[Size-i] = abs(Temp[i]);
MSize = Size;
break;
case 'Q':
Q[Size-i] = Temp[i];
QSize = Size;
break;
case 'A':
AQQ0[Size-i] = Temp[i];
AQQ0Size = Size;

}
return 0;
}

int Twos(){
int Carry = 1;
for(int i = 0; i<=MSize; i++){
M2C[i] = (M[i] == 0)?(1):(0);
}
for(int i = MSize; i>=0 && Carry!=0; i--){
if(abs(M2C[i]+Carry) == 2){
M2C[i] = 0;
Carry = 1;
}
else{
M2C[i] = 1;
Carry = 0;
}

}
}

int PrintArr(int *Target, int TargetSize){


for(int i=0; i<=TargetSize; i++){
printf("%d", Target[i]);
}
return 0;
}

int Add(int *ToAdd, int ToAddSize){


int Carry = 0;
int Sum = 0;
for(int i = 0; i<=ToAddSize; i++){
Sum = AQQ0[QSize-i]+ToAdd[ToAddSize-i]+Carry;

switch(Sum){
case 0:
AQQ0[QSize-i]=0;
Carry = 0;
break;
case 1:
AQQ0[QSize-i]=1;
Carry = 0;
break;
case 2:
AQQ0[QSize-i]=0;
Carry = 1;
break;
case 3:
AQQ0[QSize-i]=1;
Carry = 1;
break;
}
}
}

int RShift(){
for(int i = AQQ0Size; i>=1; i--){
AQQ0[i]=AQQ0[i-1];
}
}

Output:
Experiment
no: 08
Implement
Restoring
division
algorithm
#include <stdio.h>
#include <math.h>
int AQ[100], AQSize, Q[100], QSize, M[100], MSize, M2C[100];
int PrintArr(int *Target, int TargetSize);
int Add(int *ToAdd, int ToAddSiize);
int LShift();
int IntToBin(int Num, char Target);
int Twos();
int main(){
int Numerator, Denominator, Temp[100], MNeg = 0, M1, M2;
printf("\n\tEntter numerator:"); scanf("%d", &Numerator);
printf("\n\tEnter denominator:"); scanf("%d", &Denominator);

IntToBin(abs(Denominator), 'M');
IntToBin(Numerator, 'Q');
while(MSize!=QSize){

MSize++;
for(int i = MSize; i>=1; i--){
M[i]=M[i-1];
}
printf("\n");
PrintArr(&M, MSize);
M[0]=0;
}

Twos();
if(Numerator<0){

for(int i = 0; i<=MSize; i++){


M[i] = abs(M2C[i]+M[i]);
M2C[i] = abs(M[i]-M2C[i]);
M[i] = abs(M[i]-M2C[i]);
}
}
AQSize = 2*(QSize+1)-1;

printf("\nDenominator(M):\t\t"); PrintArr(&M, MSize);


printf("\nNumerator (Q):\t"); PrintArr(&Q, QSize);
printf("\n2s compliment(M):\t");PrintArr(&M2C, MSize);

for(int i = 0; i<=AQSize; i++){


if(i<=QSize){
printf("y");
AQ[i]=0;
}
else{
printf("N");
AQ[i]=Q[i-QSize-1];
}
}

printf("\nA Q:\t\t"); PrintArr(&AQ, AQSize);


for(int i = 0; i<=QSize; i++){
LShift();
printf("\n\t"); PrintArr(&AQ, AQSize); printf("\tShift left");
Add(&M2C, MSize);

printf("\n\t"); PrintArr(&AQ, AQSize); printf("\tA<-A-M");


if(AQ[0] == 0){
AQ[AQSize] = 1;
printf("\n\t"); PrintArr(&AQ, AQSize); printf("\tQ0<-1");

}
else{
AQ[AQSize] = 0;
printf("\n\t"); PrintArr(&AQ, AQSize); printf("\tQ0<-0\t");
Add(&M, MSize);
printf("\n\t"); PrintArr(&AQ, AQSize); printf("\tA<-A+M\t");
}
printf("\n----Count:%d----", i);
}
printf("\n\nFinal answer:\t"); PrintArr(&AQ, AQSize);

return 0;
}

int IntToBin(int Num, char Target){


int Temp[100];

int Size = 0;
for(int i = 0; Num != 0; i++){
Temp[i] = abs(Num%2);
Num = Num/2;
Size = i;
}
for(int i = Size; i>=0; i--){
switch(Target){
case 'M':
M[Size-i] = abs(Temp[i]);
MSize = Size;
break;
case 'Q':
Q[Size-i] = Temp[i];
QSize = Size;
break;

}
return 0;
}

int Twos(){
int Carry = 1;
for(int i = 0; i<=MSize; i++){
M2C[i] = (M[i] == 0)?(1):(0);
}
for(int i = MSize; i>=0 && Carry!=0; i--){
if(abs(M2C[i]+Carry) == 2){
M2C[i] = 0;
Carry = 1;
}
else{
M2C[i] = 1;
Carry = 0;
}

}
}

int PrintArr(int *Target, int TargetSize){


for(int i=0; i<=TargetSize; i++){
printf("%d", Target[i]);
}
return 0;
}

int Add(int *ToAdd, int ToAddSize){


int Carry = 0;
int Sum = 0;
for(int i = 0; i<=ToAddSize; i++){
Sum = AQ[QSize-i]+ToAdd[ToAddSize-i]+Carry;

switch(Sum){
case 0:
AQ[QSize-i]=0;
Carry = 0;
break;
case 1:
AQ[QSize-i]=1;
Carry = 0;
break;
case 2:
AQ[QSize-i]=0;
Carry = 1;
break;
case 3:
AQ[QSize-i]=1;
Carry = 1;
break;
}
}
}

int LShift(){
for(int i = 0; i<AQSize; i++){
AQ[i]=AQ[i+1];
}
AQ[AQSize] = 0;
}

Output:

You might also like