How to find out how many bits are set (equal 1) in the product of two integers [closed]

The question asks for an algorithm. The problem can be solved without holding a complete product, by computing each bit at a time, starting with bit 0.

Bit C0 is found from the product of A0 * B0 and is either 0 or 1.

Bit C1 is found from the sum of the products A0 * B1 + A1 * B0 and remembering any carry.

Bit C2 is found from the sum of carry + A0 * B2 + A1 * B1 + A2 * B0 and so on.

As you go, you sum the single product bits, which gives the required answer: number of set bits.

Leave a Comment