二进制的两个整数的乘积

Multiplying two integers given in binary

本文关键字:整数 两个 二进制      更新时间:2023-10-16

我正在编写一个程序,该程序将允许我将二进制数相乘/除法/加法/减法。在我的程序中,我将所有整数表示为数字向量。

我已经设法想出了如何用加法来实现这一点,但乘法让我陷入了困境,我想知道是否有人能给我一些关于如何获得伪代码的建议,作为这个程序的指南。

提前感谢!

编辑:我正试图弄清楚如何创建乘法算法,以澄清问题。如有任何关于如何计算该算法的帮助,我们将不胜感激。我通常不使用C++,所以用它解决问题需要更长的时间。

如果您想相乘,也可以考虑Booth的算法:Booth乘法算法

伪代码中的长乘法看起来像:

vector<digit> x;
vector<digit> y;
total = 0;
multiplier = 1;
for i = x->last -> x->first   //start off with the least significant digit of x
   total = total + i * y * multiplier
   multiplier *= 10;
return total

您可以尝试模拟二进制乘法器或CPU中使用的任何其他电路。

刚刚尝试了一些东西,如果只将无符号值乘以二进制值,这将有效:

unsigned int multiply(unsigned int left, unsigned int right)
{
    unsigned long long result = 0; //64 bit result
    unsigned int R = right; //32 bit right input
    unsigned int M = left; //32 bit left input
    while (R > 0)
    {
        if (R & 1)
        {// if Least significant bit exists
            result += M; //add by shifted left
        }
        R >>= 1;
        M <<= 1; //next bit
    }
/*-- if you want to check for multiplication overflow: --
    if ((result >> 32) != 0)
    {//if has more than 32 bits
        return -1; //multiplication overflow
    }*/
    return (unsigned int)result;
}

然而,这是二进制级别的……我只是你有一个数字向量作为输入

我制作了这个算法,它使用了我在网上找到的二进制加法函数,并结合了一些首先调整";移位";发送之前要将它们相加的数字。它与视频中的逻辑一致https://www.youtube.com/watch?v=umqLvHYeGiI

这是代码:

#include <iostream>
#include <string>
using namespace std;
// This function adds two binary strings and return 
// result as a third string 
string addBinary(string a, string b)
{
    string result = ""; // Initialize result 
    int s = 0;          // Initialize digit sum 
    int flag =0;
    // Traverse both strings starting from last 
    // characters 
    int i = a.size() - 1, j = b.size() - 1;
    
    while (i >= 0 || j >= 0 || s == 1)
    {
        // Computing the sum of the digits from right to left
          //x = (condition) ? (value_if_true) : (value_if_false);
          //add the fire bit of each string to digit sum 
        s += ((i >= 0) ? a[i] - '0' : 0);
        s += ((j >= 0) ? b[j] - '0' : 0);

        // If current digit sum is 1 or 3, add 1 to result 
        //Other wise it will be written as a zero 2%2 + 0 = 0
            //and it will be added to the heading of the string (to the left)
        result = char(s % 2 + '0') + result;
        // Compute carry
        //Not using double so we get either 1 or 0 as a result
        s /= 2;
        // Move to next digits (more to the left)
        i--; j--;
    }
    return result;
}
int main()
{
    string a, b, result= "0"; //Multiplier, multiplicand, and result
    string temp="0";  //Our buffer
    int shifter = 0;  //Shifting counter 
    puts("Enter you binary values");
    cout << "Multiplicand     =  ";
    cin >> a;
    cout<<endl;
    cout << "Multiplier     =   ";
    cin >> b;
    cout << endl;
    //Set a pointer that looks at the multiplier from the bit on the most right 
    int j = b.size() - 1;
   // Loop through the whole string and see if theres any 1's
    while (j >= 0)
    {
        if (b[j] == '1')
        { 
            //Reassigns the original value every loop to delete the old shifting 
            temp = a;

            //We shift by adding zeros to the string of bits
            //If it is not the first iteration it wont add any thing because we did not "shift" yet
            temp.append(shifter, '0');
                
            //Add the shifter buffer bits to the result variable 
            result = addBinary(result, temp);
        }
        //we shifted one place 
        ++shifter; 
        
        //move to the next bit on the left
        j--;
    }

    cout << "Result     = " << result << endl;

    return 0;
}