Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8654271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:44:44+00:00 2026-06-12T14:44:44+00:00

I have started to develop a BigInt class and i’m stuck right now. The

  • 0

I have started to develop a BigInt class and i’m stuck right now.
The problem is that when I try to add two numbers with different lengths, the result is not correct.
For example, 123 + 1 will return 223.
I know where the problem is, but I need help on fixing it.

        public static BigInt operator +(BigInt n1, BigInt n2)
    {
        Stack<char> sNew = new Stack<char>();
        Stack<char> sTemp = new Stack<char>();
        int currentDigit1, currentDigit2, sum;
        int carry = 0;

        //insert the digits, XXXyyy + ZZZ = first insert ___yyy and then calculate XXX+ZZZ
        if (n1.GetLength() > n2.GetLength())
        {
            while (n1.GetLength() > n2.GetLength())
                sNew.Push(n1.sDigits.Pop());
        }
        else if (n2.GetLength() > n1.GetLength())
        {
            while (n2.GetLength() > n1.GetLength())
                sNew.Push(n2.sDigits.Pop());
        }

        while (n1.sDigits.Count > 0)
        {
            currentDigit1 = int.Parse(n1.sDigits.Pop().ToString());
            currentDigit2 = int.Parse(n2.sDigits.Pop().ToString());
            sum = currentDigit1 + currentDigit2 + carry;
            carry = 0;

            if (sum > 10)
            {
                carry = 1;
                sum = sum % 10;
            }

            sNew.Push(char.Parse(sum.ToString()));

        }

        //if there is a carry, for example 95+18
        if (carry > 0)
            sNew.Push(char.Parse(carry.ToString()));

        //flip the stack
        while (sNew.Count > 0)
            sTemp.Push(sNew.Pop());
        while (sTemp.Count > 0)
            sNew.Push(sTemp.Pop());

        return new BigInt(sNew);
    }

Regardless of this problem, is this pattern of the class design is effective? Is there better idea for designing this kind of class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:44:46+00:00Added an answer on June 12, 2026 at 2:44 pm

    This is a rather wasteful representation, using full eight bits for a single decimal digit – roughly a 60% waste of space!

    Even if you stay with this representation, you should consider switching the internal representation from a Stack<char> to a List<char>, with the least significant digit stored at position 0, the digit for the tens stored at position 1, and so on. This will let you implement addition with a single loop that adds digits at the same position if both digits are available, or adds the carry to the digit of the longer number.

    A better representation would be to use a base-256 system, and store individual “digits” as an array of bytes.

    Note that the addition is not the trickiest operation to implement: wait till you hit multiplication and division! To take a peek at the complexity that you would need to address, download Java’s implementation of BigInteger.

    I am assuming that you are doing this for fun, not as part of a real project. Otherwise, there is no excuse for not using .NET’s built-in representation of BigInteger.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started to develop a brand new application and I have two
I used to develop/debug Javascript on Windows with Visual Studio. Now, I have started
I just started to develop with Google AppEngine and now I have my first
I have started to develop unit trajectories for a game server and for now
I have a Git repository which I have started to develop on master branch
I have just started to develop the form below and I have the following
I started using emacs and slime to develop some little service. I have found
I have started to try out to use the new Search API, the demo
I have started to develop a web app on google's app engine and i
I have recently started to develop applications for iPhone with MonoTouch and have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.