计算机如何计算2个数相乘?

发布于 2024-09-05 16:14:49 字数 122 浏览 8 评论 0原文

计算机如何对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

水染的天色ゝ 2024-09-12 16:14:49

重复加法是一种非常低效的数字乘法,想象一下将 1298654825 乘以 85324154。使用二进制的长乘法会快得多。

1100100
0110111
=======
0000000
-1100100
--1100100
---0000000
----1100100
-----1100100
------1100100
==============
1010101111100

对于浮点数,使用科学计数法。

100 is 1 * 10^2 (10 to the power of 2 = 100)
55 is 5.5 * 10^1 (10 to the power of 1 = 10)

将它们相乘,将尾数相乘,然后将指数相加

= 1 * 5.5 * 10^(2+1)
= 5.5 * 1000
= 5500

计算机使用等价的二进制数来执行此操作

100 = 1.1001 * 2^6
55  = 1.10111* 2^5
-> 1.1001 * 1.10111 * 2^(6+5)

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.

1100100
0110111
=======
0000000
-1100100
--1100100
---0000000
----1100100
-----1100100
------1100100
==============
1010101111100

For floating point numbers scientific notation is used.

100 is 1 * 10^2 (10 to the power of 2 = 100)
55 is 5.5 * 10^1 (10 to the power of 1 = 10)

To multiply them together multiply the mantissas and add the exponents

= 1 * 5.5 * 10^(2+1)
= 5.5 * 1000
= 5500

The computer does this using the binary equivalents

100 = 1.1001 * 2^6
55  = 1.10111* 2^5
-> 1.1001 * 1.10111 * 2^(6+5)
泡沫很甜 2024-09-12 16:14:49

通常使用的方法被称为部分产品,就像人类一样,因此例如100*55它会做类似的事情

  100 X
   55
 ----
  500 +
 500
 ----

基本上旧的方法使用移位和累加算法,在该算法中,您保留总和,同时移动第二个数字的每个数字的部分积。这种方法的唯一问题是数字以 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 like

  100 X
   55
 ----
  500 +
 500
 ----

Basically 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

败给现实 2024-09-12 16:14:49

一种方法是使用二进制的长乘法:

       01100100 <- 100
     * 00110111 <- 55
     ----------
       01100100
      01100100
     01100100
   01100100
  01100100
---------------
  1010101111100 <- 5500

这有时称为移位和添加方法。

One way is using long multiplication, in binary:

       01100100 <- 100
     * 00110111 <- 55
     ----------
       01100100
      01100100
     01100100
   01100100
  01100100
---------------
  1010101111100 <- 5500

This is sometimes called the shift and add method.

友谊不毕业 2024-09-12 16:14:49

好的,开始吧。我不久前写过这篇文章(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

陌上青苔 2024-09-12 16:14:49

计算机使用布斯算法或其他进行移位和加法运算的算法。如果您学过计算机体系结构课程,有关计算机体系结构的书籍应该是学习这些算法的好地方。

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.

夏九 2024-09-12 16:14:49

要将两个浮点数相乘,请使用以下过程:

  1. 尾数相乘
  2. 添加指数
  3. 标准化结果(小数点位于第一个非零数字之后)

因此,以 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:

  1. Multiply the mantissas
  2. Add the exponents
  3. Normalise the result (decimal point comes after first non zero digit)

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

偏爱你一生 2024-09-12 16:14:49

直观上,您还可以通过使用移位来最大程度地减少重复添加。

就浮动而言,这篇文章看起来非常相关:

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

2024-09-12 16:14:49

答案是,这取决于情况。正如其他人指出的那样,您可以使用与我们在学校教授的算法相同的算法,但使用二进制代替。但对于少数人来说,还有其他方法。
假设你想将两个 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.

葬花如无物 2024-09-12 16:14:49

我只是编写了一个简单的程序,使用算法长乘法将存储在文件中的 2 行中的两个数字相乘。它可以将两个超过 10 亿的数字相乘

示例:

            23958233
            5830 ×
         ------------
            00000000  ( =      23,958,233 ×     0)
           71874699   ( =      23,958,233 ×    30)
          191665864   ( =      23,958,233 ×   800)
         119791165    ( =      23,958,233 × 5,000)

源代码:

请查看并给出您的评论
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:

            23958233
            5830 ×
         ------------
            00000000  ( =      23,958,233 ×     0)
           71874699   ( =      23,958,233 ×    30)
          191665864   ( =      23,958,233 ×   800)
         119791165    ( =      23,958,233 × 5,000)

Source code:

Please review and give your comment
http://code.google.com/p/juniormultiply/source/browse/#svn/trunk/src

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文