Divide and conquer multiplication [00PV]

Given an n-bit number X (and assuming n is even), we can split X into two halves X_1 and X_2, each with n/2 bits. We can express this as X = 2^{n/2} X_1 + X_2 since multiplying by a power of two performs a bit shift, i.e. adding zeros to the end of a number in binary (just like multiplying by a power of ten adds zeros to the end of a number in decimal—and for the same reason).

If we have two n-bit numbers expressed in this form and multiply them, we get AB = (2^{n/2} A_1 + A_2)(2^{n/2} B_1 + B_2) = 2^n A_1 B_1 + 2^{n/2}(A_1 B_2 + A_2 B_1) + A_2 B_2. On the left-hand side we have a single n-bit multiplication; on the right-hand side we have four n/2-bit multiplications, two shifts, and three n-bit additions. We can turn this into a recursive algorithm for computing the product AB, something like the following:

\mathsf {mul}(A,B) =
Let n be the number of bits in A and B (assume they are the same)
\mathsf {if} n = 1:
Directly compute the 1-bit product AB
\mathsf {else}:
Split A and B into halves A_1, A_2 and B_1, B_2
\mathsf {return} 2^{n} \mathsf {mul}(A_1, B_1) + 2^{n/2} (\mathsf {mul}(A_1, B_2) + \mathsf {mul}(A_2, B_1)) + \mathsf {mul}(A_2, B_2)

(This works when n is a power of two; it is not hard (though a bit fiddly) to generalize to any value of n.)

Context

Divide and conquer example: multiplication [00PR]

Backlinks

Analysis of naïve divide-and-conquer multiplication [00PX]

[DC3] n-bit multiplication (1 pt) [00PW]

Related

[DC3] n-bit multiplication (1 pt) [00PW]