Hacker News new | ask | show | jobs
by userbinator 3174 days ago
The real way to multiply without loops is to use a lookup table, or unroll the (fixed iteration) shift-and-add loop.

That page is both hilarious and sad at the same time. Hilarious because the second "solution" clearly has a loop, and sad because sites like those don't really help anyone. Some of the pages on that site are downright WTFs:

http://www.techiedelight.com/generate-binary-numbers-1-n/

2 comments

>"or unroll the (fixed iteration) shift-and-add loop"

Can you elaborate on this? I understand the shifting but didn't understand the "loop unrolling fixed iteration part"

The shift-and-add algorithm for multiplication is usually implemented as a loop that iterates for the number of bits of the operand, so e.g. for an 8-bit x 8-bit multiplication, the loop runs 8 times. (An "early out" algorithm when one of the operands becomes 0 is also common, but let's not complicate things here.) It's trivial to unroll this loop into the 8 individual shift-and-add steps.
"What I told you the question was either or" haha