Since multiplication extends over additions. It may sound mysterious, but actually it is the reason. Consider this multiplication:
100 * 111
Obviously, only 111 is shifted to the left by two: 11100
This code does this for every bit that is 1 in i , and summing up the results. Thus, it turns, for example, 111 * 111 into
001 * 111 = 00111 010 * 111 = 01110 100 * 111 = 11100 ----- + 110001
Splitting the multiplication in this way is allowed, because the multiplication is distributed by adding, which makes 001 * 111 + 010 * 111 + 100 * 111 equal to (001 + 010 + 100) * 111 , and now it is obviously equal to 111 * 111 .
source share