Why is squaring a number faster than multiplying two random numbers?

前端 未结 14 2092
终归单人心
终归单人心 2021-01-30 07:18

Multiplying two binary numbers takes n^2 time, yet squaring a number can be done more efficiently somehow. (with n being the number of bits) How could that be?

Or is i

14条回答
  •  星月不相逢
    2021-01-30 07:32

    I think that you are completely wrong in your statements

    Multiplying two binary numbers takes n^2 time

    Multiplying two 32bit numbers take exactly one clock cycle. On a 64 bit processor, I would assume that multiplying two 64 bit numbers take exactly 1 clock cycle. It wouldn't even surprise my that a 32bit processor can multiply two 64bit numbers in 1 clock cycle.

    yet squaring a number can be done more efficiently somehow.
    

    Squaring a number is just multiplying the number with itself, so that is just a simple multiplication. There is no "square" operation in the CPU.

    Maybe you are confusing "squaring" with "multiplying by a power of 2". Multiplying by 2 can be implemeted by shifting all the bits one position to the "left". Multiplying by 4 is shifting all the bits two positions to the "left". By 8, 3 positions. But this trick only applies to a power of two.

提交回复
热议问题