0

I have been reading a Elements of Programming Interview and am struggling to understand the passage below:

"The algorithm taught in grade-school for decimal multiplication does not use repeated addition- it uses shift and add to achieve a much better time complexity. We can do the same with binary numbers- to multiply x and y we initialize the result to 0 and iterate through the bits of x, adding (2^k)y to the result if the kth bit of x is 1.

The value (2^k)y can be computed by left-shifting y by k. Since we cannot use add directly, we must implement it. We can apply the grade-school algorithm for addition to the binary case, i.e, compute the sum bit-by-bit and "rippling" the carry along.

As an example, we show how to multiply 13 = (1101) and 9 = (1001) using the algorithm described above. In the first iteration, since the LSB of 13 is 1, we set the result to (1001). The second bit of (1101) is 0, so we move on the third bit. The bit is 1, so we shift (1001) to the left by 2 to obtain (1001001), which we add to (1001) to get (101101). The forth and final bit of (1101) is 1, so we shift (1001) to the left by 3 to obtain (1001000), which we add to (101101) to get (1110101) = 117.

My Questions are:

  1. What is the overall idea behind this, how is it a "bit-by-bit" addition
  2. where does (2^k)y come from
  3. what does it mean by "left-shifting y by k"
  4. In the example, why do we set result to (1001) just because the LSB of 13 is 1?
VickTree
  • 889
  • 11
  • 26

1 Answers1

3

The algorithm relies on the way numbers are coded in binary.

Let A be an unsigned number. A is coded by a set of bits an-1an-2...a0 in such a way that A=∑i=0n-1ai×2i

Now, assume you have two numbers A and B coded in binary and you wand to compute A×B

B×A=B×∑i=0n-1ai×2i
=∑i=0n-1B×ai×2i

ai is equal to 0 or 1. If ai=0, the sum will not be modified. If ai=1, we need to add B×ai
So, we can simply deduce the multiplication algorithm

result=0
for i in 0 to n-1
  if a[i]=1 // assumes a[i] is the ith bit
    result = result + B * 2^i
  end
end

What is the overall idea behind this, how is it a "bit-by-bit" addition

It is just an application of the previous method where you process successively every bit of the multiplicator

where does (2^k)y come from

As mentioned above from the way binary numbers are coded. If ith bit is set, then there is a 2i in the decomposition of the number.

what does it mean by "left-shifting y by k"

Left shift means "pushing" the bits leftwards and filling the "holes" with zeroes. Hence if number is 1101 and it is left shifted by three, it becomes 1101000.
This is the way to multiply the number by 2i (just as when "left shifting" by 2 a decimal number and putting zeroes at the right places is the way to multiply by 100=102)

In the example, why do we set result to (1001) just because the LSB of 13 is 1?

Because there is a 1 at right most position, that corresponds to 20. So we left shift by 0 and add it to the result that is initialized to 0.

Alain Merigot
  • 10,667
  • 3
  • 18
  • 31