问题描述
计算机如何对两个数字(例如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.
推荐答案
重复加法是将数字相乘的一种非常低效的方法,想象一下将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.
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)
这篇关于计算机如何将两个数字相乘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!