asfe
asfe

Reputation: 63

binary multiplication in 2 parts

When I take a binary value and split this value into two parts. E.g.: 11110000 -> 1111 | 0000

My goal ist to multiply this value - split into two - with another value e.g. 1010.

When I multiply 11110000 with 1010 I get

   00000000
  11110000
 00000000
11110000 
-----------
100101100000

but how do I get this result when partially multiplying the 1111 and then 0000 with 1010 (or any other such partial binary multiplication). The reason I have to understand this is that I have two 64 bit numbers in x86-64 assembly and must multiply them with another 64-bit register.

Upvotes: 1

Views: 704

Answers (1)

Fra93
Fra93

Reputation: 2082

You can think about this in decimal, if it helps you.

When you try to multiply two numbers you can exploit the associativity of multiplication, hence a(b+c) = ab + ac.

So if you want to multiply, say

9876 x 12

you know that

9876 = 98*100 + 76

and finally

(98*100 + 76)*12 = 98*12*100 + 76*12

But multiplying by 10^x in base ten is shifting by x to the right, hence:

9876 x 12 = (12*98) << 2 + 12*76

A pseudo code for this could then be:

op1 <= 0b11110000  // operand 1
op2 <= 0b1010      // operand 2

split1 = AND   ( op1   , 0xf0 ) // 0b11110000 = 240 
split1 = RSHIFT( split1, 4    ) // 0b1111 = 15
split2 = AND   ( op2   , 0x0f ) // 0b0000 = 0

// Take care of overflow!
part1  = MUL   ( split1, op2  ) //  0b1111 * 0b1010 = 0b10010110 = 150
part1  = LSHIFT( part1 , 4    ) //  0b100101100000  = 2400
part2  = MUL   ( split2, op2  ) //  0b0000 * 0b1010 = 0b0000

res    = SUM   ( part1 , part2) //  0b100101100000   = 2400

Upvotes: 1

Related Questions