Working with BigInteger in .NET 4.0

By | June 27, 2011

One of the interesting feature that is added with .NET 4.0 is the support of BigInteger. Big numbers are needed when a number cannot be held with any of the existing data types available with .NET framework.
Long has the highest value of 9,223,372,036,854,775,807 which is 0x7FFFFFFFFFFFFFFF in hexadecimal. This is the highest value of a variable which you can store in .NET. Or rather you can say UInt64 to store the highest value of 18,446,744,073,709,551,615 if you don’t need negatives. But this is not actually sufficient when dealing with certain situations like dealing with yearly analytical reports, statistical data etc.
BigInteger is a new type(struct) introduced in .NET 4.0 which deals with the situation and can store any value. To use it you need to add System.Numarics.dll.

For instance :

BigInteger binteger = new BigInteger(3824783785434543);
Console.WriteLine(binteger);

Here in the constructor I have specified the value with which the binteger is to be initialized. You can also use assignment operator and any operators with this object.

BigInteger binteger = 3824783785434543;
Console.WriteLine(binteger);
Console.WriteLine(binteger > 32948454);
Console.Read();

You can associate any normal operators with BigInteger class. You can even store decimal values in this class and it works too.
Some Methods that comes handy at times with BitInteger :
• Pow – Represents Power of a value
• Modulus
• Remainder
• Log
• Min
• Max

To know more about it, please refer to BigInteger

Thanks for reading.
Happy Coding.