本来利用一般的思想的是一个数组位存一位数, 需要初始化数组a[1024],但是可以利用更节省空间的方法。
C语言unsigned int 的范围是0-2^32-1,一个数组位就可以存32bit(1个int = 4Bytes = 4*8bit = 32 bit),
1024比特用初始化数组a[1024/32]=a[32]可以储存完。在本程序中,我用a[0]代表实际数组的长度,
因此1024比特位的大数需要用数组a[33]存储。
bitmap思想
举一个例子 :其中0X代表16进制
a[33]={1, 0}代表数字为0的数
a[33]={1, 1}代表数字为1的数
a[33]={2,0xffffffff,1}所代表的数字大小是2^32的数
若要实现 a[33]={1, 0xffffffff}, b[33]={1, 0x1} 的加法 ,
二进制中32比特的加法如下图所示:
需要向前进1,我们可以将a[2]赋值为1,故数组a,b相加可以得到数组c是c[34]={2,0x0,0x1}
数组初始化大小为0的程序是
/****************************************************************************************************
大数相加
调用方式Add(x,y,z)
返回值,z=x+y
*****************************************************************************************************/
void Add_Big(unsigned int x[], unsigned int y[], unsigned int *z)
{
int i;
unsigned int t[34];
unsigned int carry = 0;
unsigned long long sum = 0;
Init(t);
Mov_Big(x, t);
if (t[0] < y[0])
{
t[0] = y[0];
}
for (i = 1; i <= t[0]; i++)
{
sum = y[i];
sum = sum + t[i] + carry;
t[i] = (unsigned long)sum;
carry = (unsigned int)(sum >> 32);
}
if (carry != 0)
{
t[t[0] + 1] = carry;
t[0] = t[0] + 1;
}
Mov_Big(t, z);
}
c语言实现1024bit大数加法的完整程序如下,使用devc++进行编译
#include
#define MAX 32
/***************************************************************************************************
初始化大数对象,且数组第一位是数组的实际长度
****************************************************************************************************/
void Init(unsigned int *x)
{
int i;
for (i = 1; i < MAX; i++)
x[i] = 0;
x[0] = 1;
}
/***************************************************************************************************
大数比较
调用方式Cmp(x, y)
返回值,若xy返回1; 若x = y返回0
*****************************************************************************************************/
int Cmp(unsigned int x[], unsigned int y[])
{
int i;
if (x[0] > y[0])
return 1;
if (x[0] < y[0])
return -1;
for (i = MAX - 1; i >= 1; i--)
{
if (x[i] < y[i])
return -1;
if (x[i] > y[i])
return 1;
}
return 0;
}
/****************************************************************************************************
大数赋值
调用方式Mov(x,y)
返回值:y被赋值为x;
*****************************************************************************************************/
void Mov_Big(unsigned int x[], unsigned int *y)
{
int i;
for (i = 0; i <= x[0]; i++)
{
y[i] = x[i];
}
if (x[0] < MAX)
for (i = x[0] + 1; i < MAX; i++)
{
y[i] = 0;
}
}
void Mov_Long(unsigned long long x, unsigned int *y)
{
int i;
if (x > 0xffffffff)
{
y[0] = 2;
y[1] = (unsigned int)x;
y[2] = (unsigned int)(x >> 32);
}
else
{
y[0] = 1;
y[1] = (unsigned int)x;
}
for (i = y[0] + 1; i < MAX; i++)
y[i] = 0;
}
/****************************************************************************************************
大数相加
调用方式Add(x,y,z)
返回值,z=x+y
*****************************************************************************************************/
void Add_Big(unsigned int x[], unsigned int y[], unsigned int *z)
{
int i;
unsigned int t[34];
unsigned int carry = 0;
unsigned long long sum = 0;
Init(t);
Mov_Big(x, t);
if (t[0] < y[0])
{
t[0] = y[0];
}
for (i = 1; i <= t[0]; i++)
{
sum = y[i];
sum = sum + t[i] + carry;
t[i] = (unsigned long)sum;
carry = (unsigned int)(sum >> 32);
}
if (carry != 0)
{
t[t[0] + 1] = carry;
t[0] = t[0] + 1;
}
Mov_Big(t, z);
}
void Add_Long(unsigned int x[], unsigned long long y, unsigned int *z)
{
unsigned int t[34];
unsigned long long sum = 0;
Init(t);
Mov_Big(x, t);
sum = t[1];
sum = sum + y;
t[1] = (unsigned int)sum;
if (sum > 0xffffffff)
{
int i = 2;
while (t[i] == 0xffffffff)
{
t[i] = 0;
i++;
}
t[i]++;
if (t[0] == i)
t[0]++;
}
Mov_Big(t, z);
}
int main()
{
unsigned int a[33]={8, 0x7C66DDDD, 0xE8C4E481, 0x09DC3280, 0xE1E40869, 0x487D01D6, 0xF5ED0704, 0x62BF718F, 0x93DE051D };
unsigned int b[33]={8, 0x0A3EA616, 0x0C464CD7, 0xFA602435, 0x1C1C00CB, 0x5C395BBC, 0x63106512, 0x4F21E607, 0x21FE8DDA};
unsigned int c[34];
Add_Big(a,b,c);
int i;
printf("a= ");
printf("\n ");
for(i=a[0];i>=1;i--)
printf("%llx",a[i]);
printf("\n ");
printf("\n ");
printf("b= ");
printf("\n ");
for(i=b[0];i>=1;i--)
printf("%llx",b[i]);
printf("\n ");
printf("\n ");
printf("a+b= ");
printf("\n ");
for(i=c[0];i>=1;i--)
printf("%llx",c[i]);
return 0;
}