コード例 #1
0
        public static BigInt operator +(BigInt fst, BigInt snd)
        {
            fst.Trim(); snd.Trim();
            if (fst.IsNegative && snd.IsNegative)
            {
                return(-(fst.Abs() + snd.Abs()));
            }
            if (fst.IsNegative && !snd.IsNegative)
            {
                return(snd.Abs() - fst.Abs());
            }
            if (!fst.IsNegative && snd.IsNegative)
            {
                return(fst.Abs() - snd.Abs());
            }

            int  bound = CommonBound(fst, snd) + 1;
            var  res   = new BigInt(0);
            bool carry = false;

            foreach (var i in 0.to(bound))
            {
                int addition = NumberUtil.CountTrue(carry, fst[i], snd[i]);
                switch (addition)
                {
                case 0:
                    res[i] = false; carry = false; break;

                case 1:
                    res[i] = true; carry = false; break;

                case 2:
                    res[i] = false; carry = true; break;

                case 3:
                    res[i] = true; carry = true; break;

                default: break;
                }
            }
            res.Trim();
            return(res);
        }