Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Large Numbers Requiring More than 64 bit Representation

I am using java and have to deal with numbers larger than long (which is 64 bits). What should I use? What is the size of BigInteger in java?

like image 689
Ashwin Avatar asked Apr 07 '12 02:04

Ashwin


People also ask

What is the largest number that can be represented using 64 bits?

A 64-bit signed integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). A 64-bit unsigned integer.

How many numbers can be represented by 64 bits?

A 32-bit number can store 2^32 values, or 4,294,967,296. Meanwhile, a 64-bit number has 2^64 possible values, or a staggering 18,446,744,073,709,551,616. That's over 18.4 quintillion, which is so large that it's difficult to comprehend.

What is a 128-bit number?

In computer architecture, 128-bit integers, memory addresses, or other data units are those that are 128 bits (16 octets) wide. Also, 128-bit central processing unit (CPU) and arithmetic logic unit (ALU) architectures are those that are based on registers, address buses, or data buses of that size.

How many numbers can be represented with 16 bits?

There are 65,536 different unsigned 16-bit numbers. The smallest unsigned 16-bit number is 0 and the largest is 65535.


2 Answers

As you mentioned in your question, you should use BigInteger.

They can be as large as you need - until you run out of memory.

like image 178
Mark Byers Avatar answered Oct 20 '22 00:10

Mark Byers


What is the size of BigInteger in java?

That's a little bit tricky. The problem is that there is no clear specification of the limit in the javadocs.

  • The class uses an int[] to represent the magnitude. This means it could potentially represent numbers up to ((2^32)^(2^31 - 1).

  • The API has a method that returns the number as a 2's complement byte array. The limit for this is ((2^8)^(2^31 - 1).

  • The API has another method that returns the size of the number in bits ... as an int. This implies a limit of 2^(2^31 - 1) or maybe 2^(2^32).

In practice, these numbers are all so large that you will probably run into heap space limits (or CPU performance limits) first.


the problem is I have to find out the square root of a the number.

You should be able to find an algorithm for calculating square roots in your undergraduate maths text books (or Wikipedia). Coding it should be a simple task.

(I'd point you at example code, except that this smells like "homework", and I don't entirely trust the code that I found.)

Don't forget that most integers have an irrational square-root ...

like image 30
Stephen C Avatar answered Oct 20 '22 00:10

Stephen C