Sie sind auf Seite 1von 9

Signed Binary Multiplication

#include<iostream>
#include<conio.h>

using namespace std;

void add(int a[], int x[], int qrn);


void complement(int a[], int n)
{
int i;

int x[8] = { NULL };


x[0] = 1;
for (i = 0; i < n; i++)
{
a[i] = (a[i] + 1) % 2;
}
add(a, x, n);
}

void add(int ac[], int x[], int qrn)


{
int i, c = 0;
for (i = 0; i < qrn; i++)
{
ac[i] = ac[i] + x[i] + c;
if (ac[i] > 1)
{
ac[i] = ac[i] % 2;
c = 1;
}
else
c = 0;
}

void ashr(int ac[], int qr[], int &qn, int qrn)


{
int temp, i;

temp = ac[0];
qn = qr[0];
cout << "\t\tashr\t\t";
for (i = 0; i < qrn - 1; i++)
{
ac[i] = ac[i + 1];
qr[i] = qr[i + 1];
}
qr[qrn - 1] = temp;
}

void display(int ac[], int qr[], int qrn)


{
int i;

for (i = qrn - 1; i >= 0; i--)


cout << ac[i];
cout << " ";
for (i = qrn - 1; i >= 0; i--)
cout << qr[i];

int main(int argc, char **argv)


{
int mt[10], br[10], qr[10], sc, ac[10] = { 0 };
int brn, qrn, i, qn, temp;
cout
<< "\n--Enter the multiplicand and multipier in signed 2's comlement form if
negative--";

cout << "\n Number of multiplicand bit=";


cin >> brn;
cout << "\nmultiplicand=";

for (i = brn - 1; i >= 0; i--)


cin >> br[i]; //multiplicand

for (i = brn - 1; i >= 0; i--)


mt[i] = br[i]; // copy multipier to temp array mt[]

complement(mt, brn);

cout << "\nNo. of multiplier bit=";


cin >> qrn;

sc = qrn; //sequence counter

cout << "Multiplier=";


for (i = qrn - 1; i >= 0; i--)
cin >> qr[i]; //multiplier

qn = 0;
temp = 0;

cout << "qn\tq[n+1]\t\tBR\t\tAC\tQR\t\tsc\n";


cout << "\t\t\tinitial\t\t";
display(ac, qr, qrn);
cout << "\t\t" << sc << "\n";

while (sc != 0)
{
cout << qr[0] << "\t" << qn;
if ((qn + qr[0]) == 1)
{
if (temp == 0)
{
add(ac, mt, qrn);
cout << "\t\tsubtracting BR\t";
for (i = qrn - 1; i >= 0; i--)
cout << ac[i];
temp = 1;
}
else if (temp == 1)
{
add(ac, br, qrn);
cout << "\t\tadding BR\t";
for (i = qrn - 1; i >= 0; i--)
cout << ac[i];
temp = 0;
}
cout << "\n\t";
ashr(ac, qr, qn, qrn);
}
else if (qn - qr[0] == 0)
ashr(ac, qr, qn, qrn);

display(ac, qr, qrn);


cout << "\t";

sc--;
cout << "\t" << sc << "\n";
}
cout << "Result=";
display(ac, qr, qrn);
}

In C Language
#include <stdio.h>
#include <math.h>

int a = 0,b = 0, c = 0, a1 = 0, b1 = 0, com[5] = { 1, 0, 0, 0, 0};


int anum[5] = {0}, anumcp[5] = {0}, bnum[5] = {0};
int acomp[5] = {0}, bcomp[5] = {0}, pro[5] = {0}, res[5] = {0};

void binary(){
a1 = fabs(a);
b1 = fabs(b);
int r, r2, i, temp;
for (i = 0; i < 5; i++){
r = a1 % 2;
a1 = a1 / 2;
r2 = b1 % 2;
b1 = b1 / 2;
anum[i] = r;
anumcp[i] = r;
bnum[i] = r2;
if(r2 == 0){
bcomp[i] = 1;
}
if(r == 0){
acomp[i] =1;
}
}
//part for two's complementing
c = 0;
for ( i = 0; i < 5; i++){
res[i] = com[i]+ bcomp[i] + c;
if(res[i] >= 2){
c = 1;
}
else
c = 0;
res[i] = res[i] % 2;
}
for (i = 4; i >= 0; i--){
bcomp[i] = res[i];
}
//in case of negative inputs
if (a < 0){
c = 0;
for (i = 4; i >= 0; i--){
res[i] = 0;
}
for ( i = 0; i < 5; i++){
res[i] = com[i] + acomp[i] + c;
if (res[i] >= 2){
c = 1;
}
else
c = 0;
res[i] = res[i]%2;
}
for (i = 4; i >= 0; i--){
anum[i] = res[i];
anumcp[i] = res[i];
}

}
if(b < 0){
for (i = 0; i < 5; i++){
temp = bnum[i];
bnum[i] = bcomp[i];
bcomp[i] = temp;
}
}
}
void add(int num[]){
int i;
c = 0;
for ( i = 0; i < 5; i++){
res[i] = pro[i] + num[i] + c;
if (res[i] >= 2){
c = 1;
}
else{
c = 0;
}
res[i] = res[i]%2;
}
for (i = 4; i >= 0; i--){
pro[i] = res[i];
printf("%d",pro[i]);
}
printf(":");
for (i = 4; i >= 0; i--){
printf("%d", anumcp[i]);
}
}
void arshift(){//for arithmetic shift right
int temp = pro[4], temp2 = pro[0], i;
for (i = 1; i < 5 ; i++){//shift the MSB of product
pro[i-1] = pro[i];
}
pro[4] = temp;
for (i = 1; i < 5 ; i++){//shift the LSB of product
anumcp[i-1] = anumcp[i];
}
anumcp[4] = temp2;
printf("\nAR-SHIFT: ");//display together
for (i = 4; i >= 0; i--){
printf("%d",pro[i]);
}
printf(":");
for(i = 4; i >= 0; i--){
printf("%d", anumcp[i]);
}
}

void main(){
int i, q = 0;
printf("\t\tBOOTH'S MULTIPLICATION ALGORITHM");
printf("\nEnter two numbers to multiply: ");
printf("\nBoth must be less than 16");
//simulating for two numbers each below 16
do{
printf("\nEnter A: ");
scanf("%d",&a);
printf("Enter B: ");
scanf("%d", &b);
}while(a >=16 || b >=16);

printf("\nExpected product = %d", a * b);


binary();
printf("\n\nBinary Equivalents are: ");
printf("\nA = ");
for (i = 4; i >= 0; i--){
printf("%d", anum[i]);
}
printf("\nB = ");
for (i = 4; i >= 0; i--){
printf("%d", bnum[i]);
}
printf("\nB'+ 1 = ");
for (i = 4; i >= 0; i--){
printf("%d", bcomp[i]);
}
printf("\n\n");
for (i = 0;i < 5; i++){
if (anum[i] == q){//just shift for 00 or 11
printf("\n-->");
arshift();
q = anum[i];
}
else if(anum[i] == 1 && q == 0){//subtract and shift for1
printf("\n-->");
printf("\nSUB B: ");
add(bcomp);//add two's complement to implement subtraction
arshift();
q = anum[i];
}
else{//add ans shift for 01
printf("\n-->");
printf("\nADD B: ");
add(bnum);
arshift();
q = anum[i];
}
}

printf("\nProduct is = ");
for (i = 4; i >= 0; i--){
printf("%d", pro[i]);
}
for (i = 4; i >= 0; i--){
printf("%d", anumcp[i]);
}
}

DIVISION
void unsigned_divide(unsigned int dividend,
unsigned int divisor,
unsigned int &quotient,
unsigned int &remainder )
{
unsigned int t, num_bits;
unsigned int q, bit, d;
int i;

remainder = 0;
quotient = 0;

if (divisor == 0)
return;

if (divisor > dividend) {


remainder = dividend;
return;
}

if (divisor == dividend) {
quotient = 1;
return;
}

num_bits = 32;

while (remainder < divisor) {


bit = (dividend & 0x80000000) >> 31;
remainder = (remainder << 1) | bit;
d = dividend;
dividend = dividend << 1;
num_bits--;
}

/* The loop, above, always goes one iteration too far.


To avoid inserting an "if" statement inside the loop
the last iteration is simply reversed. */

dividend = d;
remainder = remainder >> 1;
num_bits++;

for (i = 0; i < num_bits; i++) {


bit = (dividend & 0x80000000) >> 31;
remainder = (remainder << 1) | bit;
t = remainder - divisor;
q = !((t & 0x80000000) >> 31);
dividend = dividend << 1;
quotient = (quotient << 1) | q;
if (q) {
remainder = t;
}
}
} /* unsigned_divide */

#define ABS(x) ((x) < 0 ? -(x) : (x))

void signed_divide(int dividend,


int divisor,
int &quotient,
int &remainder )
{
unsigned int dend, dor;
unsigned int q, r;

dend = ABS(dividend);
dor = ABS(divisor);
unsigned_divide( dend, dor, q, r );

/* the sign of the remainder is the same as the sign of the dividend
and the quotient is negated if the signs of the operands are
opposite */
quotient = q;
if (dividend < 0) {
remainder = -r;
if (divisor > 0)
quotient = -q;
}
else { /* positive dividend */
remainder = r;
if (divisor < 0)
quotient = -q;
}

} /* signed_divide */

Das könnte Ihnen auch gefallen