locked
booth's algorithm for twos complement multiplication method

    Question

  • i want method calculate booth's algorithm for twos complement multiplication method
    Friday, December 17, 2010 12:50 PM

Answers

  • 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”);
    
    Multiplier = Convert.ToInt32(Console.ReadLine());
    
    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”);
    
    Multiplicand = Convert.ToInt32(Console.ReadLine());
    
    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))
    
    {
    
    Add(A, M);
    
    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);
    
    Add(A, 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”);
    
    string Option = Console.ReadLine();
    
    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);
    
    BoothAlgorithm.Add(PlusOne, Twos_Compliment);
    
    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;
    
    }
    
    }
    
    }
    

    Devlin Liles http://twitter.com/devlinliles http://www.devlinliles.com/ If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by othmanoo Thursday, December 23, 2010 6:21 PM
    Friday, December 17, 2010 1:30 PM

All replies

  • 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”);
    
    Multiplier = Convert.ToInt32(Console.ReadLine());
    
    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”);
    
    Multiplicand = Convert.ToInt32(Console.ReadLine());
    
    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))
    
    {
    
    Add(A, M);
    
    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);
    
    Add(A, 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”);
    
    string Option = Console.ReadLine();
    
    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);
    
    BoothAlgorithm.Add(PlusOne, Twos_Compliment);
    
    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;
    
    }
    
    }
    
    }
    

    Devlin Liles http://twitter.com/devlinliles http://www.devlinliles.com/ If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by othmanoo Thursday, December 23, 2010 6:21 PM
    Friday, December 17, 2010 1:30 PM
  • thanks for you interest
    othmanoo
    Thursday, December 23, 2010 5:54 PM
  • thank you for interesting
    othmanoo
    Thursday, December 23, 2010 6:22 PM
  • Thanks, Othmanoo acutally I have written this article at BiggestGuru.Com

    Please check more articles on Programming and if you want any help you can contact me.

    Saturday, January 08, 2011 8:38 PM
  • Hi, Capscdev thanks for sharing my article i have written this article. Please see more of my articles at http://www.biggestguru.com/
    Saturday, January 08, 2011 8:40 PM