BigInteger in C# 4.0

In C# 4.0 Microsoft has added so many features and I love all most all the features. In today’s post we are going to discuss BigInteger Class. During programming some complex systems often we need a very big numbers. For example if we use some of asymmetrical cryptographic feature which require to use large numbers or we can give simple example of factorial where we sometime reached to limit of data type provided by C# compiler. At that time this BigInteger data type can be very handy. You can store 232 to 264 number in this data type. So its very big and you can copy very very big number in that data type.

So let’s take a simple example to write a factorial program. BigInteger data type comes under System.Numerics namespace. So first we have to add reference to our program to System.Numerics assembly like following.

Big Integer In C# 4.0 - Add Reference to System. Numeric

Now we have added the reference to System.Numeric so we are ready for code. Here in code I have just taken a very large number in big integer which is out of range of integer to test out. So I have assigned ‘39242937852522522’ to big integer object and printed that. Following is a code for that.

using System;
using System.Numerics;

namespace ExperimentConsole
{
class Program
{
static void Main(string[] args)
{
BigInteger n = 39242937852522522;
Console.WriteLine(n);
}
}
}
Let’s run code and see output like following as expected its printing value.

Output

If you are run same program with integer it will not compile. It will give ‘Can not covert type long to integer error’. That’s it. Hope you liked it. Stay tuned for more.. Till then happy programming. Namaste!!
Shout it
Published Wednesday, September 14, 2011 2:39 AM by Jalpesh P. Vadgama
Filed under: ,

Comments

# re: BigInteger in C# 4.0

Tuesday, September 13, 2011 5:28 PM by L.

Adding BigInteger support in .Net was great, but I have found the feature set somewhat lacking compared to common multiprecision libraries (e.g. libgmp).  For example, how would you do modular inversion (i.e. finding x such that ax = 1 (mod n); it's a very important operation for cryptography)?

In C with libgmp, it would look like this:

====

mpz_t a, n, x;

mpz_init_set_str(a, "19", 10);

mpz_init_set_str(a, "65537", 10);

mpz_init(x);

mpz_invert(x, a, n);

gmp_printf("x=%Zd\n", x);

====

# re: BigInteger in C# 4.0

Tuesday, September 13, 2011 5:55 PM by LuxIt

I expected this in earlier versions of .net :)

# re: BigInteger in C# 4.0

Wednesday, September 14, 2011 6:03 AM by Pranav

Useful feature. Thanks for the post!

# re: BigInteger in C# 4.0

Wednesday, September 14, 2011 2:37 PM by RichardD

"You can store 2^32 to 2^64 number in this data type."

No, you can store 2^32 numbers in an Int32 (int) and 2^64 numbers in an Int64 (long). A BigInteger has no theoretical limit.

"The BigInteger type ... represents an arbitrarily large integer whose value in theory has no upper or lower bounds."

msdn.microsoft.com/.../system.numerics.biginteger.aspx

Leave a Comment

(required) 
(required) 
(optional)
(required)