我试图写一个" BigInteger"实现。 BigInteger是一个无符号整数数组。
BigInteger_1 [0] ... BigInteger_1 [15];
我试图添加两个BigInteger。
using System;
namespace BigInt {
class MainClass {
static int MAX_SIZE = 16;
public static void Main () {
// First BigInteger
uint[] BigInteger_1 = new uint[MAX_SIZE];
BigInteger_1 [15] = 0xfffffffe;
// Second BigInteger
uint[] BigInteger_2 = new uint[MAX_SIZE];
BigInteger_2 [15] = 0x00000002;
// Third BigInteger
uint[] BigInteger_3 = new uint[MAX_SIZE];
// Perform an addition
BigInteger_3 = BigInteger_Add (BigInteger_1, BigInteger_2);
int i;
// Print BigInteger_1
for (i = 1; i < MAX_SIZE; i ++) {
Console.Write ("{0:x8}", BigInteger_1 [i]);
}
Console.WriteLine ();
// Print BigInteger_2
for (i = 1; i < MAX_SIZE; i ++) {
Console.Write ("{0:x8}", BigInteger_2 [i]);
}
Console.WriteLine ();
// Print BigInteger_3
for (i = 1; i < MAX_SIZE; i ++) {
Console.Write ("{0:x8}", BigInteger_3 [i]);
}
Console.WriteLine ();
}
public static uint[] BigInteger_Add (
uint[] BigInteger_1,
uint[] BigInteger_2
) {
uint[] BigInteger_3 = new uint[MAX_SIZE];
uint BigInteger_Carry = 0x00000000;
uint BigInteger_Limit = 0xffffffff;
int i;
// Loop through all integers
for (i = 1; i < MAX_SIZE; i ++) {
if (BigInteger_1 [i] + BigInteger_2 [i] >= BigInteger_Limit) {
Console.WriteLine ("Check {0} >= {1}", BigInteger_1 [i] + BigInteger_2 [i], BigInteger_Limit);
BigInteger_Carry = (BigInteger_1 [i] + BigInteger_2 [i]) - BigInteger_Limit;
BigInteger_3 [i] = BigInteger_Limit;
} else {
Console.WriteLine ("Check {0} < {1}", BigInteger_1 [i] + BigInteger_2 [i], BigInteger_Limit);
BigInteger_3 [i] = BigInteger_1 [i] + BigInteger_2 [i];
BigInteger_Carry = 0x00000000;
}
}
if (BigInteger_Carry != 0x00000000) {
BigInteger_3 [i] = BigInteger_Carry;
}
return BigInteger_3;
}
}
}
当我将变量设置为:
// First BigInteger
BigInteger_1 [15] = 0xfffffffe;
// Second BigInteger
BigInteger_2 [15] = 0x00000001;
然后执行后我得到这个输出(向右滚动):
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 4294967295 >= 4294967295
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000fffffffe
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffff
哪个看起来正确。但是当我将变量设置为:
// First BigInteger
BigInteger_1 [15] = 0xfffffffe;
// Second BigInteger
BigInteger_2 [15] = 0x00000002;
我得到了这个(再次滚动):
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
Check 0 < 4294967295
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000fffffffe
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
因此,如果sum大于最大值,我无法比较两个整数之和和最大值。在这种情况下我能做什么(而且必须做)?
答案 0 :(得分:1)
当你添加两个整数时,如果总和大于最大可表示值,则根据定义它无法表示和操作。当你超过最大值时滚动到零是预期的,通常被认为是一个错误条件。
实现您正在尝试的一种低效方法是在BitInteger数组中保留一个额外的整数,您可以在内部维持超过Max_Size的精度。
答案 1 :(得分:1)
您正试图检查a + b > uint.MaxValue
在a + b
溢出时是否有效。{/ p>
解决方案1 :bool carry = a > uint.MaxValue - b;
解决方案2 :var result = a + b; bool carry = result < a || result < b;