How computer multiplies 2 numbers?

ckv picture ckv · Jun 17, 2010 · Viewed 18.3k times · Source

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.

Answer

David Sykes picture David Sykes · Jun 17, 2010

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)