I have two variables(which are actually elements of two different matrices). For example i want to multiply
a[i][k]*b[k][j]
using bit manipulation, how can i do that.
I saw references to multiply constants, not variables like 3*2, 3*4, 3*8, etc. But how can i apply same techniques to multiplying variables? If a post on this exist, can you point me to that. Thanks!
Given two integral variables
unsigned X, Y;
And given a Commodore 64, Apple ][, or some other architecture that doesn't have its own multiply instruction, this will multiply the numbers.
unsigned answer = 0;
while ( X )
{
answer <<= 1;
if ( X & 1 )
answer += Y;
X >>= 1;
}
Bit shift multiplication is usable only when multiplying by a power of 2 (2, 4, 8, 16 etc). The multiplication will then be reduced to as single bit shift operation:
x1 = 2^n;
result = x2 << n; // This is the same as x2 * x1
For arbitrary cases, the most efficient way is to use normal multiplication:
a[i][k]*b[k][j]
If you're multiplying huge matrices, what matters is an efficient algorithm that has good cache behavior. For C++, check out the Eigen library . On a modern CPU you can't micro-optimize multiplication of two variables.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.