c语言版本的大数乘法

// 类型定义

typedef unsigned int        u32;

typedef unsigned char       u8;

typedef unsigned short      u16;

typedef unsigned long long  u64;

// u32数据初始化为某一位数

/*

参数:

p_dst:被初始化的数据的指针

p_src:初始化数据

len:p_dst的长度

*/
void Data_u4_Clear(u32 *p_dst, u32 p_src,u32 len)
{
    while(len--)
    {
        *(p_dst++)=p_src;
    }
}

// 大数乘法(此大数乘法是按照逆序顺序进行计算的)

/*

res:乘法后的结果(res对应的数据的长度为aLen+bLen)

a:乘法的左参数

b:乘法的右参数

aLen:a的长度

bLen:b的长度

*/

void BigMul(u32*res,u32*a,u32*b,u32 aLen,u32 bLen)
{
        int ix, iy;
u32 C;
u64 _W;
Data_u4_Clear(res, 0x00, bLen);
for (ix = 0; ix < aLen; ix++)
{
C = 0;
for (iy = 0; iy < bLen; iy++)
{
_W = (u64)C + (u64)res[(ix + iy)] + ((u64)a[ix]) * ((u64)b[iy]);
res[(ix + iy)] = (u32)_W;
C = (u32)(_W >> 32);
}
res[(ix + aLen)] = C;
}
return;
}

// 数据翻转

void top_swap_bottom(u32 *res,u32 resLen)

{

int i;

for (i=0;i<resLen/2;i++)

{

res[i] = res[i] ^res[resLen-i-1];

res[resLen-i-1]= res[i] ^res[resLen-i-1];

res[i] = res[i] ^res[resLen-i-1];

}

}

// 举个例子

int main()

{

u32 res[16]={0x00};

u32 a[8]={0x11223344,0x55667788,0x35363738,0x11223344,0x55667788,0x35363738,0x11223344,0x55667788};

u32 b[8]={0x11223344,0x55667788,0x35363738,0x11223344,0x55667788,0x35363738,0x11223344,0x55667788};

// 数据翻转

top_swap_bottom(a,sizeof(a)/sizeof(u32));

// 数据翻转

top_swap_bottom(b,sizeof(b)/sizeof(u32));

// 大数乘法

BigMul(res,a,b,sizeof(a)/sizeof(u32),sizeof(b)/sizeof(u32));

// 数据翻转

top_swap_bottom(res,sizeof(res)/sizeof(u32));

return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乌苏舞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值