An int
is represented with 32 bits. Thus any value between -2^31
and 2^31-1
can be represented. Nothing out of this range.
You can use a long
(64 bits), or a BigInteger
(a datastructures that can represented all numbers up to the memory limit).
The disadvantage of using these structures (especially BigInteger
) is that a CPU does not always offer instructions for arithmetic. Thus adding two BigInteger
instances requires more time than doing this with an int
or long
. In case of a long
, if the CPU is 32-bit, it requires at least two instructions to process this.
On a sidenote. A CPU offers a better way to calculate the powers of two: shift operations.
You can simply write:
long value = 0x01L << power;//power of two, all in one simple instruction.
This works as follow: a shift moves bits to the left. Thus if the original value is:
0001 1011 << 2
= 0110 1100
Shifting to the left in a binary representation is arithmetically the same as multiplying with two.