oracle乘法截取数字位数,使用 32位整型实现 64位的乘法运算 | 学步园

留下备忘,呵呵。

随便实现了一下,不知道是否还有更好的方法,随着学习进度修改本实现

(该思想通过拆分高低位实现,原则上可以实现所有运算中的越界)

#include

#include

void split (unsigned int src,unsigned int &high,unsigned int &low){

high=src>>16;

low=src&((1<<16) -1);

}

void myintxint(unsigned int e1,unsigned int e2,unsigned int &first,unsigned int &second ){

unsigned int e11;//e1 的高16位

unsigned int e12;//e1 的低16位

split(e1,e11,e12);

unsigned int e21;//e2 的高16位

unsigned int e22;//e2 的低16位

split(e2,e21,e22);

//计算乘积

//低*低 +低*高 +高*低 + 高*高

first=e11*e21;

second=e12*e22;

unsigned int tmp1;

unsigned int tmp2;

unsigned int tmp3;

unsigned int tmp4;

unsigned int tmp5;

unsigned int tmp6;

split(e12*e21,tmp1,tmp2);

first+=tmp1;

split(second,tmp3,tmp4);

split(tmp2+tmp3,tmp5,tmp6);

first+=tmp5;

second+=tmp2<<16;

split(e21*e12,tmp1,tmp2);

first+=tmp1;

split(second,tmp3,tmp4);

split(tmp2+tmp3,tmp5,tmp6);

first+=tmp5;

second+=tmp2<<16;

}

void multiply(unsigned int first,unsigned int second,unsigned int& high,unsigned int& low)

{

unsigned int h1 = first>>16;

unsigned int l1 = first&((1<<16)-1);

unsigned int h2 = second>>16;

unsigned int l2 = second&((1<<16)-1);

unsigned int a1h = (h1*l2)>>16;

unsigned int a1l = (h1*l2)&((1<<16)-1);

unsigned int a2h = (h2*l1)>>16;

unsigned int a2l = (h2*l1)&((1<<16)-1);

unsigned int lh = (l1*l2)>>16;

unsigned int ll = (l1*l2)&((1<<16)-1);

unsigned int ah =(a2l+a1l+lh)>>16;

unsigned int al =(a2l+a1l+lh)&((1<<16)-1);

high = h1*h2+a1h+a2h+ah;

low = (al<<16)+ll;

}

void main(int args,char** argv){

unsigned int first;

unsigned int second;

unsigned int e1=1000000000;

unsigned int e2=1000000000;

unsigned __int64 e3=e1;

unsigned __int64 e4=e2;

myintxint(e1,e2,first,second);

printf(" %08x*%08x = 0x%08x%08x /n",e1,e2,first,second);

multiply(e1,e2,first,second);

printf(" %08x*%08x = 0x%08x%08x /n",e1,e2,first,second);

printf(" %08I64x*%08I64x = 0x%016I64x /n",e3,e4,e3*e4);

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值