Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
Search :       Advanced Search »
Home » Blogs Home » Blog Detail

Manipulating Huge Integers In C#

 by Krishna Garad on Feb 04, 2012

In this blog we will see how to work with big integers.
Comments: 0 Views: 514 Printable Version 

Introduction

In this blog we will see how to work with big integers. In lot of cases we required to manipulate the big inter numbers and finally we have only capacity of work still 19 digits by using Int64. Until .Net Framework version 3.5 we have to use only max Int64 but in .Net framework version 4.0 we got one more new struct using System.Numerics.BigInteger. Using this struct we are able to manipulate big integers, doubles, bytes and decimal values. Using biginteger we are able perform the calculations on the numbers which are more than 50 digits also.

In last cases like Int64 we if we pass more than 19 digits it's throws an error either input string too long or too short but using new struct i.e. BigInteger you can pass more than 50 also. So let's see some operations of using this new biginteger.

Example 1 : Addition

Using biginteger you can make addition of more than 50 digits also and this biginteger struct only provide the Add method which takes two biginteger values and returns the biginteger try it like bellow.

public BigInteger addHugeIntegers(BigInteger a, BigInteger b)
{
     return BigInteger.Add(a, b);
}

Example 2 :  Subtract

BigInteger provide the Subtract method using you can make subtraction in two huge integer values like bellow.

public BigInteger subtractHughIntegers(BigInteger a,BigInteger b)
{
     return BigInteger.Subtract(a, b);
}


Example 3 : Compare

Biginteger is able to compare two values also like first value is greater than or equal to or less than etc… like bellow.

public int Compare(BigInteger a, BigInteger b)
{
     return BigInteger.Compare(a, b);
}


Example 4 : EqualTo:

Biginteger is able to check the two values are equal or not equal to and return the bool true if they are equal and returns false if they are not equal like bellow.

public bool isEqualTo(BigInteger a,BigInteger b)
{
     return BigInteger.Equals(a,b);
}


Our biginteger having more methods also which takes input as Double, Decimal and bytes also for more you can refer MSDN. But remind the biginteger supports only Vista SP1, 7, xp sp3 and server 2008/R2, Server 2003 Sp2 these versions of os are supported by BigInteger.

Conclusion

Using BigInteger in .Net 4.0 we are able to manipulate huge integers which may have more than 50 digits also.
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
 
What do you say about this post? Post a comment here
*Title:
*Comment:
 
Comments not available.
Nevron Gauge for SharePoint
Become a Sponsor

 Blogger's Profile
Age: 25
Location:
Title: Developer
Joined: Dec 22, 2010
Education: Masters Degree
 More Blogs from this Blogger
No record available
 Latest Blogs
[Video] OnClose Handler
[Video] Storing and Loading the Window and Toolbar position
The Euclidean Algorithm
Swapping Exe Process
How Exe file is Generated by VS2005 C++ Project?
What is Exe
Header files: Multiple Inclusion problem - Solution B
Header files: Multiple Inclusion problem - Solution A
Header files: Multiple Inclusion problem - Reason
Header files: Multiple Inclusion problem
View all »
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.