Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does BigInteger having no limit mean?

I looked into this stackoverflow question relating to Big Integer and specifically I do not understand this line (the words in italics):

In the BigInteger class, I have no limits and there are some helpful functions there but it is pretty depressing to convert your beautiful code to work with the BigInteger class, specially when primitive operators don't work there and you must use functions from this class.

I don't know what I am missing but to represent something that has no limit you would require infinite memory ? Whats is the trick here ?

like image 610
Geek Avatar asked Aug 23 '12 09:08

Geek


People also ask

Does BigInteger have a limit?

BigInteger has no cap on its max size (as large as the RAM on the computer can hold).

How do you know if BigInteger is zero?

signum() method helps us to identify whether a BigInteger is positive or zero or negative. It returns one of the following values depending on the following conditions: returns -1 when number is negative. returns 0 when number is zero.

How many bits can BigInteger hold?

The BigInteger class stores a number as an array of unsigned, 32-bit integer "digits" with a radix, or base, of 4294967296.

What is the range of BigInteger?

BigInteger must support values in the range -2 Integer.MAX_VALUE (exclusive) to +2 Integer.MAX_VALUE (exclusive) and may support values outside of that range. The range of probable prime values is limited and may be less than the full supported positive range of BigInteger . The range must be at least 1 to 2500000000.


1 Answers

There is no theoretical limit. The BigInteger class allocates as much memory as it needs for all the bits of data it is asked to hold.

There are, however, some practical limits, dictated by the memory available. And there are further technical limits, although you're very unlikely to be affected: some methods assume that the bits are addressable by int indexes, so things will start to break when you go above Integer.MAX_VALUE bits.

like image 77
Graham Borland Avatar answered Oct 01 '22 00:10

Graham Borland