Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep number longer than 64-bit long

Tags:

c#

numbers

I need to keep in the program number which is longer than long or Int64. Because if I use long it will return just 0.

like image 684
PSSGCSim Avatar asked Sep 24 '13 07:09

PSSGCSim


People also ask

How do you store an integer bigger than 64 bit?

There is no standard way for having data type greater than 64 bits. You should check the documentation of your systems, some of them define 128 bits integers. However, to really have flexible size integers, you should use an other representation, using an array for instance.

What is the maximum value for a 64 bit number?

With the two most common representations, the range is 0 through 18,446,744,073,709,551,615 (264 − 1) for representation as an (unsigned) binary number, and −9,223,372,036,854,775,808 (−263) through 9,223,372,036,854,775,807 (263 − 1) for representation as two's complement.

What is longer than long in C#?

In C#, all numeric data types store limited range of values. For example, Int32 data type can store integers from -2,147,483,648 to 2,147,483,647. The long (Int64) type can store integers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, and unsigned long has the limit of 0 to 18,446,744,073,709,551,615.


2 Answers

You can use BigInteger in .Net 4.0

The BigInteger type is an immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower bounds. The members of the BigInteger type closely parallel those of other integral types (the Byte, Int16, Int32, Int64, SByte, UInt16, UInt32, and UInt64 types). This type differs from the other integral types in the .NET Framework, which have a range indicated by their MinValue and MaxValue properties.

If you don't have the luxury of moving to .Net 4. then you can use an open source project to help you out named as IntX.

IntX is an arbitrary precision integers library written in pure C# 2.0 with fast - about O(N * log N) - multiplication/division algorithms implementation. It provides all the basic arithmetic operations on integers, comparing, bitwise shifting etc. It also allows parsing numbers in different bases and converting them to string, also in any base. The advantage of this library is fast multiplication, division and from base/to base conversion algorithms - all the fast versions of the algorithms are based on fast multiplication of big integers using Fast Hartley Transform which runs for O(N * log N * log log N) time instead of classic O(N^2).

like image 146
Ehsan Avatar answered Nov 15 '22 12:11

Ehsan


Try with BigInteger

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

like image 33
Alberto Avatar answered Nov 15 '22 14:11

Alberto