Take the 2-minute tour ×

I'm implementing my own BigNumber class in C# for educational purposes. For a start, I intend it to code the basic arithmetic, relational operators and certain math methods. The values will be stored in a byte array.

Could you guys give me some tips on how I would design such a class or rather the proper way of designing such a class ?


I'm not asking for help on how to implement the specific operators and methods. I'd like to know how the class should be structured internally.

share|improve this question
You probably won't get a very informative answer unless you ask a more specific question. –  Peter Olson Mar 11 '11 at 20:09
Design in terms of defining the public API? Design in terms of how you'd construct the class internally? –  Adam Robinson Mar 11 '11 at 20:09
What do you have in mind? You can tell us what your thoughts are and I guess you'll get very informative answers. –  Ilya Kogan Mar 11 '11 at 20:10
Design in terms of internal implementation. –  rohit89 Mar 11 '11 at 20:22

3 Answers 3

up vote 3 down vote accepted

I did that once in C++. I recommend that you read The Art of Computer Programming. Volume 2 has all the details of the algorithms for implementing big numbers. It's a great resource (for this and many other problems.)

The book should be available from most public libraries around you (or any university library).

BTW. No need to read the whole book, if you just want you can just use it as a reference for the algorithms that you need.

UPDATE: As for the API you should try to mimic the existing APIs for number in .NET. Something like Int32.

As for the internal class design, it should be pretty straightforward because there should be very few units interacting. You could abstract the "storage" (byte array) part away and iterate over the "digits" using standard iterators over some generic storage provider. This would allow you to change to use int arrays for example. If you do this then you can automatically change the base of your numbers and enable your implementation to store "more" per digit. This implies that the base of the operations won't be static but would be determined by the "digit" size.

I had fun implementing mine, it's a simple but nice project. In my case I didn't go fancy with the internal design. Good luck!

share|improve this answer

Wikipedia has a pretty good reference page on Arbitrary-precision arithmetic. It provides a general overview of many of the issues you'll face, as well as links to various implementations.

share|improve this answer

Apart from what has already been said, you can find the BigNumber project on codeplex.com, so you can look at the source code for several implementations. Check http://bignumber.codeplex.com/.

share|improve this answer

Your Answer


