当前位置: 代码迷 >> 数据结构与算法 >> 怎么计算两个128位二进制数的相乘
  详细解决方案

怎么计算两个128位二进制数的相乘

热度:506   发布时间:2016-05-23 09:14:41.0
如何计算两个128位二进制数的相乘
我是做RSA运算,两个128位(或更多位)二进制数相乘怎么算?

------解决方案--------------------
给你个以前写的大数相乘的例子,是十进制数的,但方法一样的:
#include <stdio.h>
#include <stdlib.h>
#define MAX 6000
int main(){

/*the Array to store two number and result*/
int Left[MAX],Right[MAX],Result[MAX*MAX],Temp[MAX*MAX];
int LeftLength,RightLength; /*user define the number 's length*/
FILE *fp; /*open file to store the two random number*/
int MulTemp=0; /*the temp varible for each multipe*/
int AddTemp=0; /*the temp varible for each add*/
int MulCarray=0,AddCarray=0; /*each mul and add carray*/
int i,j,k; /*the loop variable*/

for(i=0;i <MAX;){ /*new*/
Left[i]=0;
Right[i]=0;
Result[i]=0;
Temp[i]=0;
i++;
}
printf( "\n************************************************************ ");
printf( "\n| | ");
printf( "\n| To multipe tow very big integer number | ");
printf( "\n| the number 's length less than 5000 will be do correct | ");
printf( "\n| result will be saved to e:\\ result.txt | ");
printf( "\n| author:w_chen 2007.4 | ");
printf( "\n************************************************************ ");
printf( "\n---------Please input the two number 's length---- ");
printf( "\ninput a number and press Enter to input another... ");
printf( "\nfirst number 's length: ");
scanf( "%d ",&LeftLength);
printf( "second number 's length: ");
scanf( "%d ",&RightLength);
if((fp=fopen( "e:\\result.txt ", "wb+ "))==NULL){
printf( "\nopenfile error!! ");
return 0;
}
printf( "\nnow we 're make the random number and multipe... ");
printf( "\n ");
fputs( "\n ",fp);
for(i=MAX-LeftLength;i <=MAX-1;){
Left[i]=random(10);
fputc(48+Left[i],fp);
printf( "%d ",Left[i]);
i++;
}
printf( "\n\nX\n\n ");
fputc(13,fp);
fputs( "\n\n X \n\n ",fp);
for(i=MAX-RightLength;i <=MAX-1;){
Right[i]=random(10);
fputc(48+Right[i],fp);
printf( "%d ",Right[i]);
i++;
}
printf( "\n ");
for(i=MAX-1;i> =MAX-RightLength;){
for(k=0;k <=MAX-1;){
Temp[k]=0;
k++;
}
MulCarray=0;AddCarray=0;
for(j=MAX-1;j> =MAX-LeftLength;){
MulTemp=Left[j]*Right[i]+MulCarray;
if(MulTemp> =10)
MulCarray=MulTemp/10;
else
MulCarray=0;
Temp[i-MAX+1+j]=MulTemp-10*MulCarray;
j--;
}
if(MulCarray!=0){
Temp[i-MAX+1+j]=MulCarray;
MulCarray=0;
}
for(k=MAX-1;k> =i-MAX+1+j;){
AddTemp=Result[k]+Temp[k]+AddCarray;
if(AddTemp> =10)
AddCarray=AddTemp/10;
else
AddCarray=0;
Result[k]=AddTemp-10*AddCarray;
k--;
}
i--;
}
printf( "\n--------------------------\n ");
fputc(13,fp);
fputs( "\n\n------------------------\n\n ",fp);
fputc(13,fp);
for(k=0;Result[k]==0;k++);
  相关解决方案
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!
 
  • 最近登录:Sat Oct 21 14:52:32 CST 2017
  • 最近登录:Sat Oct 21 14:52:32 CST 2017
  • 最近登录:Sat Oct 21 14:52:32 CST 2017
  • 最近登录:Sat Oct 21 14:52:32 CST 2017
  • 最近登录:Sat Oct 21 14:52:32 CST 2017