Introducing: System.Numeric.BigInteger [Inbar Gazit]

Introducing: System.Numeric.BigInteger [Inbar Gazit]

Rate This
  • Comments 26

Update: BigInteger was cut from the .NET 3.5 release.

Have you ever needed a really huge number?  So big it just couldn't fit into any other base-type in the framework?  Well, no problem anymore, just use the new BigInteger type. You can now have the ability to use integers of any arbitrary size, so they can exponentiate until the cows come home... or memory runs out, whichever comes first :-)

So, what's so "Big" about BigInteger?

I saw some people wondering whether we should have called this new type UnboundedInteger instead of BigInteger and I would like to tell you what I think. First, you should know that the BCL team does not take naming lightly. We spend a lot of time and energy making sure we choose the best names for our types, methods, properties etc. I believe that good names are not only technically accurate but also intuitive and easy to remember and use. That's one of the reasons that made us decide to use the name BigInteger. The other reason was that other languages are using the prefix "Big" for unbounded types.  Isn't that huge? Enormous? Gigantic? Well, I think it's simply Big...

Using the new BigInteger

So, what do you need to do in order to use this new type?

First you need to get the latest CTP for the new version of Visual Studio (code name – "Orcas") and install it so that you also get the new version of the .NET Framework including this new type. It would be a good idea to install it on a separate computer or make sure you back-up your important work first. Remember, this is pre-beta software... (in the January CTP you can choose either a Virtual PC image or to use the actual installation bits)

If you do install the bits, all you need is the new System.Core.dll as a reference in your project.  The new BigInteger class is in this new assembly.

From this point on I'm going to be talking C# although this can all work for other languages (such as VB.NET).

Create a new C# project and add a new Using System.Numeric at the top of your cs module. Now, you're ready to use BigInteger in your code.

Here is a sample code to calculate factorials:

using System;

using System.Numeric;


namespace TryOutBigInteger {

    class MyBigIntApp {

        static BigInteger BigFactorial(BigInteger x) {

            if (x < 0)

                return BigFactorial(-x);

            else if (x <= 1)

                return x;

            else return x * BigFactorial(--x);



        static void Main(string[] args) {

            BigInteger x = BigInteger.Parse(Console.ReadLine());






Here is what we get when we run it:






Wow!  I wish that was my bank account balance :-)

BigInteger supports most of the operations that int supports including +, -, *, /, ++, --, %, DivRem GreatestCommonDivisor, Pow, ModPow, Abs and Negate.  All of these are static methods you can find using intellisense by typing "BigInteger".

Bitwise operations are not supported, but we will have an MSDN article coming soon with how to write your own library extension of BigInteger.

Please try the new BigInteger type and let us know what you think. We'd love to hear your feedback!

  • wow, bitwise operations are a part of all Smalltalk  integer types. Simply because they inherit from the class Integer.  

  • Congratulations on the implementation of the BigInteger class.

    Now we need a BitStreamReader and a BitStreamWriter class. :)

    The .NET Framework is immense, but I feel it's quite lacking.

  • Nice class, gonna try to take fun!

    And don't forget that 0! = 1! = 1

  • System.Core.dll


    It's cool name but is it good?

  • În versiunea Orcas de .NET s-a introdus un nou namespace: System.Numeric. În acest namespace se regăseşte

  • Незабаром в нас буде нова версія .Net Framework 3.5, яка схоже, як і 3.0, буде спиратися на базовий .Net

  • I would like to propose the inclusion of

    a function

    BigInteger BinaryShift(BigInteger b, int n) {

       return b * BigInteger.Pow(2, n); }

    This function is a convenience only, but an

    important one when porting code from other

    languages. For example it could replace the

    ubiquitous Java functions

     BigInteger shiftLeft(int n)  [this << n]

     BigInteger shiftRight(int n) [this >> n]

    And, of course, it can be implemented much more

    efficiently compared to the function above.

    My second suggestion is a much more important

    one. I think it is almost a must to have the


     Product(T[] a)

     Sum(T[] a)

    and for slices of T[]

     Product(T[] a, int start, int end)

     Sum(T[] a, int start, int end)

    for T in {int, long, BigInteger}.

    Well, you might answer a simple foreach loop

    will solve this task. And I think a great many

    people will indeed implement it in this way.

    However, this is the most inefficient way to

    compute a sum or a product. For *big* numbers

    sequential adding or multiplying tends to be

    'unbalanced', a small term or factor is paired

    with an increasingly large one. Bad.

    Much better is the strategy of binary splitting. Just for illustration of the idea:

    BigInteger recProduct(BigInteger[] s, int len) {

     if (len == 0) return BigInteger.One;

     if (len == 1) return s[index++];

     int len2 = len >> 1;

     return recProduct(s, len - len2)

            * recProduct(s, len2);


    And the only 'right' place to implement this

    is BigInteger, of course.

    Furthermore, something like Product(T[] a) and

    Sum(T[] a) assists the trend to functional

    programming in C# 3.

    So go ahead and show us some improvement over the BigInteger library of Java, written 15 years ago.

    Cheers SharpFool

  • So, you know I was thinking, Int64 is just too limiting for my integers. I mean, I might generate a product...

  • I'm pleased to let you all know that Microsoft released the first beta version of the next version of

  • While installing VisualStudio 2008 Beta2 I was surprised that the NET framework 2.0 installation got

  • Joshua Goodman&#39;s session on CLR futures was packed - the crowd of developers wanted to learn what&#39;s

Page 2 of 2 (26 items) 12