booth's algorithm for twos complement multiplication method

# booth's algorithm for twos complement multiplication method

• Friday, December 17, 2010 12:50 PM

i want method calculate booth's algorithm for twos complement multiplication method

### All Replies

• Friday, December 17, 2010 1:30 PM

```Full Article found here http://www.biggestguru.com/category/blogs/computer-programming/booth-algorithm-c<br/>
<br/>
Code excerpt below.<br/>
<br/>
<br/>
using System;

using System.Collections.Generic;

using System.Text;

namespace BoothAlgo_Practice

{

class BoothAlgorithm

{

public static int[] A = new int[4];

public static int[] Q = new int[4];

public static int[] M = new int[4];

public static int QM1 = 0;

public static int Multiplier = 0, Multiplicand = 0;

static void Main(string[] args)

{

for (int i = 0; i < 4; i++)

{

A[i] = 0;

}

for (int i = 0; i < 4; i++)

{

Q[i] = 0;

}

for (int i = 0; i < 4; i++)

{

M[i] = 0;

}

Console.WriteLine(“\n\n\n\t\t=============================================”);

Console.WriteLine(“\t\t=============================================”);

Console.WriteLine(“\t\t==============         ==============”);

Console.WriteLine(“\t\t============== BOOTH ALGORITHM ==============”);

Console.WriteLine(“\t\t==============         ==============”);

Console.WriteLine(“\t\t=============================================”);

Console.WriteLine(“\t\t=============================================”);

Console.WriteLine(“\n\nEnter Multiplier”);

if (Multiplier < 0)

{

Q = DecimalToBinary(Multiplier);

Q = TwosCompliment(Q);

}

else if (Multiplier > 0)

{

Q = DecimalToBinary(Multiplier);

}

Console.WriteLine(“The Binary Conversion (Q) Is ” + Q[0] + Q[1] + Q[2] + Q[3] + “\n”);

Console.WriteLine(“Enter Multiplicand”);

if (Multiplicand < 0)

{

M = DecimalToBinary(Multiplicand);

M = TwosCompliment(M);

}

else if (Multiplicand > 0)

{

M = DecimalToBinary(Multiplicand);

}

Console.WriteLine(“The Binary Conversion (M) Is ” + M[0] + M[1] + M[2] + M[3] + “\n”);

Console.WriteLine(“\t____________________________________________________”);

Console.Write(“\t\t |\t\t |\t\t|\n\t”);

Console.WriteLine(” A\t |\t Q\t |\tQ-1\t|\t M”);

Console.WriteLine(“\t__________|______________|______________|___________”);

Console.Write(“\t\t |\t\t |\t\t|\n\t” + A[0] + A[1] + A[2] + A[3] + “\t |”);

Console.Write(“\t” + Q[0] + Q[1] + Q[2] + Q[3] + “\t |”);

Console.Write(“\t” + QM1 + “\t|”);

Console.WriteLine(“\t” + M[0] + M[1] + M[2] + M[3] + “\t”);

Console.WriteLine(“\t__________|______________|______________|___________”);

for (int i = 1; i < 5; i++)

{

if ((Q[3] == 0) && (QM1 == 1))

{

Console.Write(“\t\t |\t\t |\t\t|\n\t” + A[0] + A[1] + A[2] + A[3] +”\t |”);

Console.Write(“\t” + Q[0] + Q[1] + Q[2] + Q[3] + “\t |”);

Console.Write(“\t” + QM1 + “\t|”);

Console.WriteLine(“\t” + M[0] + M[1] + M[2] + M[3] + “\t A = A + M”);

}

else if ((Q[3] == 1) && (QM1 == 0))

{

int[] Temp = { M[0], M[1], M[2], M[3] };

TwosCompliment(Temp);

Console.Write(“\t\t |\t\t |\t\t|\n\t” + A[0] + A[1] + A[2] + A[3] + “\t |”);

Console.Write(“\t” + Q[0] + Q[1] + Q[2] + Q[3] + “\t |”);

Console.Write(“\t” + QM1 + “\t|”);

Console.WriteLine(“\t” + M[0] + M[1] + M[2] + M[3] + “\t A = A – M”);

}

//shifting:

Shift();

Console.Write(“\t\t |\t\t |\t\t|\n\t” + A[0] + A[1] + A[2] + A[3] + “\t |”);

Console.Write(“\t” + Q[0] + Q[1] + Q[2] + Q[3] + “\t |”);

Console.Write(“\t” + QM1 + “\t|”);

Console.WriteLine(“\t” + M[0] + M[1] + M[2] + M[3] + “\tShift Cycle{0}”, i);

Console.WriteLine(“\t__________|______________|______________|___________”);

}

Console.WriteLine(“\n\n”);

Console.WriteLine(“Do You Want Check Your Answer:\n\nPress Y or y\n\nPress Any Key To Quit”);

if ((Option == “Y”) || (Option == “y”))

{

Console.WriteLine(“\nAnswer In Decimal Is “+Multiplier + ” * ” + Multiplicand + ” = ” + BinaryToDecimal());

}

else

{

Console.WriteLine(“\nThank You For Using It\n\n”);

}

}

public static int[] DecimalToBinary(int Num)

{

int[] Binary = new int[4];

for (int i = 3; i >= 0; i–)

{

Binary[i] = Num % 2;

Num = Num / 2;

}

return Binary;

}

public static int[] TwosCompliment(int[] PlusOne)

{

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

BoothAlgorithm.TwosComplimentConversion(PlusOne);

return PlusOne;

}

public static void TwosComplimentConversion(int[] Compliment)

{

for (int i = 0; i <= 3; i++)

{

if (Compliment[i] == 0)

{

Compliment[i] = 1;

}

else

{

Compliment[i] = 0;

}

}

}

public static void Add(int[] Num1, int[] Num2)

{

int Carry = 0;

for (int i = 3; i >= 0; i–)

{

Num1[i] = Carry + Num1[i] + Num2[i];

if (Num1[i] == 2)

{

Carry = 1;

Num1[i] = 0;

}

else if (Num1[i] == 3)

{

Carry = 1;

Num1[i] = 1;

}

else

{

Carry = 0;

}

}

}

public static void Shift()

{

int Temp_Shift = A[3];

for (int i = 3; i >= 1; i–)

{

A[i] = A[i - 1];

}

QM1 = Q[3];

for (int i = 3; i >= 1; i–)

{

Q[i] = Q[i - 1];

}

Q[0] = Temp_Shift;

}

public static int BinaryToDecimal()

{

int Result = 0;

int[] Final = { A[0], A[1], A[2], A[3], Q[0], Q[1], Q[2], Q[3] };

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

if (((Multiplier > 0) && (Multiplicand > 0)) || ((Multiplier < 0) && (Multiplicand < 0)))

{

Result = (Final[0] * (2 * 2 * 2 * 2 * 2 * 2 * 2)) + (Final[1] * (2 * 2 * 2 * 2 * 2 * 2)) + (Final[2] * (2 * 2 * 2 * 2 * 2)) + (Final[3] * (2 * 2 * 2 * 2)) + (Final[4] * (2 * 2 * 2)) + (Final[5] * (2 * 2)) + (Final[6] * (2)) + (Final[7] * (1));

}

else if ((Multiplier < 0) || (Multiplicand < 0))

{

for (int i = 0; i <= 7; i++)

{

if (Final[i] == 0)

{

Final[i] = 1;

}

else

{

Final[i] = 0;

}

}

int Carry = 0;

for (int i = 7; i >= 0; i–)

{

Final[i] = Carry + Final[i] + Num[i];

if (Final[i] == 2)

{

Carry = 1;

Final[i] = 0;

}

else if (Final[i] == 3)

{

Carry = 1;

Final[i] = 1;

}

else

{

Carry = 0;

}

}

Result = -((Final[0] * (2 * 2 * 2 * 2 * 2 * 2 * 2)) + (Final[1] * (2 * 2 * 2 * 2 * 2 * 2)) + (Final[2] * (2 * 2 * 2 * 2 * 2)) + (Final[3] * (2 * 2 * 2 * 2)) + (Final[4] * (2 * 2 * 2)) + (Final[5] * (2 * 2)) + (Final[6] * (2)) + (Final[7] * (1)));

}

return Result;

}

}

}
```

• Marked As Answer by Thursday, December 23, 2010 6:21 PM
•
• Thursday, December 23, 2010 5:54 PM

thanks for you interest
othmanoo
• Thursday, December 23, 2010 6:22 PM

thank you for interesting
othmanoo
• Saturday, January 08, 2011 8:38 PM