计算机如何计算2个数相乘?
计算机如何对 2 个数字(例如 100 * 55)执行乘法。
我的猜测是计算机通过重复加法来实现乘法。当然,对于整数也可能是这种情况。然而对于浮点数必须有一些其他的逻辑。
注:这是在采访中被问到的。
How does a computer perform a multiplication on 2 numbers say 100 * 55.
My guess was that the computer did repeated addition to achieve multiplication. Of course this could be the case for integer numbers. However for floating point numbers there must be some other logic.
Note: This was asked in an interview.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
重复加法是一种非常低效的数字乘法,想象一下将 1298654825 乘以 85324154。使用二进制的长乘法会快得多。
对于浮点数,使用科学计数法。
将它们相乘,将尾数相乘,然后将指数相加
计算机使用等价的二进制数来执行此操作
Repeated addition would be a very inefficient way to multiply numbers, imagine multiplying 1298654825 by 85324154. Much quicker to just use long multiplication using binary.
For floating point numbers scientific notation is used.
To multiply them together multiply the mantissas and add the exponents
The computer does this using the binary equivalents
通常使用的方法被称为部分产品,就像人类一样,因此例如
100*55
它会做类似的事情基本上旧的方法使用移位和累加算法,在该算法中,您保留总和,同时移动第二个数字的每个数字的部分积。这种方法的唯一问题是数字以 2 补码形式存储,因此您无法在移位时进行简单的逐位乘法。
现在,大多数优化都能够在 1 个周期内对所有部分求和,从而使您能够进行更快的计算和更轻松的并行化。
看看这里: http://en.wikipedia.org/wiki/Binary_multiplier
最后您可以找到一些实现,例如
The method that is usually used is called partial products like humans do, so for example having
100*55
it would do something likeBasically old approaches used a shift-and-accumulate algorithm in which you keep the sum while shifting the partial products for every digit of the second number. The only problem of this approach is that numbers are stored in 2-complement so you can't do a plain bit per bit multiplication while shifing.
Nowaday most optimization are able to sum all the partials in just 1 cycle allowing you to have a faster calculation and a easier parallelization.
Take a look here: http://en.wikipedia.org/wiki/Binary_multiplier
In the end you can find some implementations like
一种方法是使用二进制的长乘法:
这有时称为移位和添加方法。
One way is using long multiplication, in binary:
This is sometimes called the shift and add method.
好的,开始吧。我不久前写过这篇文章(1987 年!),所以有些事情发生了变化,而另一些则保持不变......
http:// /moneybender.com/transactor_article.pdf
Okay, here ya go. I wrote this a while back (1987!), so some things have changed while others remain the same...
http://moneybender.com/transactor_article.pdf
计算机使用布斯算法或其他进行移位和加法运算的算法。如果您学过计算机体系结构课程,有关计算机体系结构的书籍应该是学习这些算法的好地方。
Computers use Booth's algorithm or other algorithms that do shifting and adding for arithmetic operations. If you had studied computer architecture courses, books on computer architecture should be a good place to study these algorithms.
要将两个浮点数相乘,请使用以下过程:
因此,以 10 为底,将 5.1E3 和 2.6E-2
相乘 尾数相乘 => ; 5.1 * 2.6 = 13.26 (注意,只要您跟踪小数点应该在哪里,这可以通过整数乘法来完成)
添加指数 => 3 + -2 = 1
给出结果 13.26E1
Normalize 13.26E1 => 1.326E2
To multiply two floating point numbers the following procedure is used:
So, in base 10 to multiply 5.1E3 and 2.6E-2
Multiply the mantissas => 5.1 * 2.6 = 13.26 (NB this can be done with integer multiplication as long as you track where the decimal point should be)
Add the exponents => 3 + -2 = 1
Gives us a result of 13.26E1
Normalise 13.26E1 => 1.326E2
直观上,您还可以通过使用移位来最大程度地减少重复添加。
就浮动而言,这篇文章看起来非常相关:
http://oopweb.com/Assembly/Documents/ArtOfAssembly/Volume/Chapter_14/CH14-1.html#HEADING1-19
Intuitively you can minimise repeated additions by also using shifting.
In terms of floats this article looks pretty relevant:
http://oopweb.com/Assembly/Documents/ArtOfAssembly/Volume/Chapter_14/CH14-1.html#HEADING1-19
答案是,这取决于情况。正如其他人指出的那样,您可以使用与我们在学校教授的算法相同的算法,但使用二进制代替。但对于少数人来说,还有其他方法。
假设你想将两个 8 位数字相乘,这可以通过一个大的查找表来完成。您只需将两个 8 位数字连接起来形成一个 16 位数字,然后使用该数字对包含所有产品的表进行索引。
或者,您可以拥有一个大型门网络来直接计算函数。然而,这些门网络对于大数的乘法往往会变得相当笨重。
The answer is, it depends. As others have pointed out, you can use the same algorithm as we were taught in school, but using binary instead. But for small numbers there are other ways.
Say that you want to multiply two 8 bit numbers, this can be done with a big lookup table. You just concatenate the two 8 bit numbers to form a 16 bit number and use that nunber to index into a table with all the products.
Or, you can just have a big network of gates that computes the function in a direct way. These gate network tend to get quite unwieldy for multiplication of large numbers though.
我只是编写了一个简单的程序,使用算法长乘法将存储在文件中的 2 行中的两个数字相乘。它可以将两个超过 10 亿的数字相乘
示例:
源代码:
请查看并给出您的评论
http://code.google.com/p/juniormultiply /source/browse/#svn/trunk/src
I just code a simple program multiply two number stored in 2 line in file using algorithm long multiplication. It can multiply two number which have more than 1 billion number in each other
Example:
Source code:
Please review and give your comment
http://code.google.com/p/juniormultiply/source/browse/#svn/trunk/src