你真的不需要右移来实现乘法。看看如何做到这一点,C 中的示例代码:
#include <stdio.h>
typedef unsigned char uint8;
typedef unsigned short uint16;
uint16 Mul8x8(uint8 a, uint8 b)
{
int cnt;
uint16 prod = 0;
for (cnt = 8; cnt > 0; cnt--)
{
prod += prod;
if (a & 0x80)
prod += b;
a += a;
}
return prod;
}
const uint8 Multipliers[][2] =
{
{ 0x00, 0x01 },
{ 0x01, 0x00 },
{ 0x33, 0x10 },
{ 0x11, 0x0C },
{ 0x0F, 0x0F },
{ 0x80, 0x80 },
{ 0xFF, 0xFF },
};
int main(void)
{
int i;
for (i = 0; i < sizeof(Multipliers) / sizeof(Multipliers[0]); i++)
{
uint8 a = Multipliers[i][0];
uint8 b = Multipliers[i][1];
uint16 p = a * b;
uint16 p2 = Mul8x8(a, b);
printf("0x%02X * 0x%02X = 0x%04X %c= 0x%04X\n",
a, b, p, "!="[p == p2], p2);
}
return 0;
}
输出([ideone])(http://ideone.com/NwsykN)):
0x00 * 0x01 = 0x0000 == 0x0000
0x01 * 0x00 = 0x0000 == 0x0000
0x33 * 0x10 = 0x0330 == 0x0330
0x11 * 0x0C = 0x00CC == 0x00CC
0x0F * 0x0F = 0x00E1 == 0x00E1
0x80 * 0x80 = 0x4000 == 0x4000
0xFF * 0xFF = 0xFE01 == 0xFE01