public big DeepClone() { var head = this.Tail; var result = new BigIntBlock(); var current = result; while (head != null) { current.Digit = head.Digit; if (head.NextDigit != null) { current.NextDigit = new BigIntBlock(); current = current.NextDigit; } head = head.NextDigit; } return(new big { Tail = result, }); }
public static bigint Add(bigint left, bigint right) { if (left == bigint.Zero) { return(right); } if (right == bigint.Zero) { return(left); } return(Implementation(left.Tail, right.Tail)); bigint Implementation(BigIntBlock lhs, BigIntBlock rhs) { var result = new BigIntBlock(); var currentBlock = result; while (!ReferenceEquals(lhs, null) && !ReferenceEquals(rhs, null)) { if (!(ReferenceEquals(lhs.NextDigit, null) && ReferenceEquals(rhs.NextDigit, null))) { if (ReferenceEquals(lhs.NextDigit, null)) { lhs.NextDigit = bigint.Zero.Tail; } if (ReferenceEquals(rhs.NextDigit, null)) { rhs.NextDigit = bigint.Zero.Tail; } } if (currentBlock == null) { currentBlock = new BigIntBlock(); } long leftValue = lhs.Digit.Value; if (left.Sign == Sign.Negative) { leftValue *= -1; } long rightValue = rhs.Digit.Value; if (right.Sign == Sign.Negative) { rightValue *= -1; } long resultValue = leftValue + rightValue; if (resultValue <= uint.MaxValue) { currentBlock.Digit = new Digit(resultValue); } else { resultValue -= uint.MaxValue; var d = lhs.NextDigit.Digit; d.Increase(); lhs.NextDigit.Digit = d; currentBlock.Digit = new Digit(resultValue); } lhs = lhs.NextDigit; rhs = rhs.NextDigit; currentBlock = currentBlock.NextDigit; } return(new bigint { Tail = result, }); } }