C ++中的长手乘法 [英] Long Hand Multiplication In C++

查看:144
本文介绍了C ++中的长手乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对存储在两个数组BeforeDecimal1 and BeforeDecimal2中的8位二进制数实施Long Hand乘法方法.问题是我总是得到错误的结果.我试图找出问题,但无法解决.这是代码:

I am trying to implement Long Hand Multiplication method for 8 bit binary numbers stored in two arrays BeforeDecimal1 and BeforeDecimal2. The problem is I always get the wrong result. I tried to figure out the issue but couldn't do it. Here is the code:

这是比以前的代码精炼得多的代码.它给我结果,但结果不正确.

int i = 0,进位= 0;

int i=0,carry=0;

while(true)
{
    if(BeforeDecimal2[i]!=0)
        for(int j=7;j>=0;j--)
        {
            if(s[j]==1 && BeforeDecimal1[j]==1 && carry==0)
            {
                cout<<"Inside first, j= "<<j<<endl;
                carry=1;
                s[j]=0;
            }
            else
                if(s[j]==1 && BeforeDecimal1[j]==0 && carry==1)
                {
                    cout<<"Inside second, j= "<<j<<endl;
                    carry=1;
                    s[j]=0;
                }
                else
                    if(s[j]==0 && BeforeDecimal1[j]==0 && carry==1)
                    {
                        cout<<"Inside third, j= "<<j<<endl;
                        carry=0;
                        s[j]=1;
                    }
                    else
                        if(s[j]==0 && BeforeDecimal1[j]==0 && carry==0)
                        {
                            cout<<"Inside fourth, j= "<<j<<endl;
                            carry=0;
                            s[j]=0;
                        }
                        else
                            if(s[j]==0 && BeforeDecimal1[j]==1 && carry==0)
                            {
                                cout<<"Inside fifth, j= "<<j<<endl;
                                carry=0;
                                s[j]=1;
                            }

                            else
                                if(s[j]==1 && BeforeDecimal1[j]==1 && carry==1)
                                {
                                    //cout<<"Inside fifth, j= "<<j<<endl;
                                    carry=1;
                                    s[j]=1;
                                }
                                else
                                    if(s[j]==1 && BeforeDecimal1[j]==0 && carry==0)
                                    {
                                        //cout<<"Inside fifth, j= "<<j<<endl;
                                        carry=0;
                                        s[j]=1;
                                    }
                                    else
                                        if(s[j]==0 && BeforeDecimal1[j]==1 && carry==1)
                                        {
                                            //cout<<"Inside fifth, j= "<<j<<endl;
                                            carry=1;
                                            s[j]=0;
                                        }

        }

        for(int h=7;h>=0;h--)
        {
            if(h==0)
            {
                BeforeDecimal1[0]=0; // that is inserting zeros from the right
            }
            else
            {
                BeforeDecimal1[h]=BeforeDecimal1[h-1];
                BeforeDecimal1[h-1]=0;
            }

        }
    if(i==3)
        break;

    i++;
}

致谢

推荐答案

也许备份和存储为8位二进制数的8位二进制数开始是最容易的.就像我们进行十进制乘法一样,我们从许多数字开始.我们将这些值乘以这些单个数字,然后将它们相加在一起得出最终结果.区别(或一个明显的区别)是,因为我们使用二进制,所以所有数字都代表2的幂,因此我们可以通过简单地对输入进行位移位来获得每个中间结果.

Maybe it would be easiest to back up and start with 8-bit binary numbers stored as 8-bit binary numbers. Much like when we do decimal multiplication, we start with a number of digits. We take the values of multiplying by those individual digits, and add them together to get the final result. The difference (or one obvious difference) is this since we're working in binary, all our digits represent powers of two, so we can get each intermediate result by simply bit shifting the input.

由于它是二进制数,所以每个数字只有两种可能性:如果它是0,那么我们需要将0乘以其他数字,将其他数字左移适当的位数.显然,0时间仍然是0,因此在这种情况下我们什么也不做.另一种可能性是我们有1,在这种情况下,我们将其他数字左移1乘以适当的位数.

Since it's binary, we have only two possibilities for each digit: if it's a 0, then we need to add 0 times the other number shifted left the appropriate number of places. Obviously, 0 time whatever is still 0, so we simply do nothing in this case. The other possibility is that we have a 1, in which case we add 1 times the other number shifted left the appropriate number of places.

例如,让我们考虑17 x 5或(二进制)10001 x 101之类的东西.

For example, let's consider something like 17 x 5, or (in binary) 10001 x 101.

     10001
       101
    ------
     10001
 + 1000100
  --------
 = 1010101      

将其转换为更易于识别的内容,我们得到0x55或85 d .

Converting that to something more recognizable, we get 0x55, or 85d.

在代码中,该过程非常简短.从0的结果开始.检查是否设置了一个操作数中的最低有效位.如果是这样,则将另一个操作数添加到结果中.将一个操作数向右移一点,将另一个操作数向左移一点,重复直到您要向右移的操作数等于0为止:

In code, that process comes out fairly short and simple. Start with a result of 0. Check whether the least significant bit in one operand is set. If so, add the other operand to the result. Shift the one operand right a bit and the other left a bit, and repeat until the operand you're shifting to the right equals 0:

unsigned short mul(unsigned char input1, unsigned char input2) { 
    unsigned short result = 0;

    while (input2 != 0) {
        if (input2 & 1)
            result += input1;
        input1 <<= 1;
        input2 >>= 1;
    }
    return result;
}

如果要处理带符号的数字,通常最简单的方法是分别计算结果的符号,然后对绝对值进行乘法.

If you want to deal with signed numbers, it's generally easiest to figure up the sign of the result separately, and do the multiplication on the absolute values.

这篇关于C ++中的长手乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆