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.
Answer from willeM_ Van Onsem on Stack OverflowAn 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.
In general,
byte 8 bits, short 16 bits, int 32 bits, long 64 bits
For Integer(int) you can store a value in between -2^31 and 2^31 i.e., from -2,147,483,648 to 2,147,483,647
If you really want to calculate powers of 2 without any limitations (atleast as per theory), you can use strings and make multiplications accordingly.
Edit:
As Commusoft suggested, BigIntegers can also be used which improves performance over Strings.
Videos
This is due to overflow of the int data type.
Java's int size is 32 bits, so the range is -2,147,483,648 to 2,147,483,647.
2^31 = 2147483648
So it is overflowing to -2147483648 as the binary value of 2,147,483,647 is 01111111111111111111111111111111 (one zero and 31 ones), where the first bit is the "sign bit" (2's complement form).
If you try to go beyond this limit (2,147,483,647) by 1 (i.e. adding 1 to it), it changes the sign bit to 1, making this int negative.
So it will become 10000000000000000000000000000000 (1 one and 31 zeros), giving you the answer -2147483648.
larger exponents return 0 (however I think this may have to do with the fact that we need to use ints vs longs.)
Correct.
Copyint i = (int) 2147483648L; // -2147483648 due to over flow
int j = i * 2; // 0 due to overflow.
You can use long however this has the same problem but for a higher value.
Copypublic static long recPower(int baseNum, int power) {
if (power < 0) throw new IllegalArgumentException();
return power == 0 ? 1L : baseNum * recPower(baseNum, power - 1);
}
One way to check for an overflow is to see
Copypublic static long recPower(int baseNum, int power) {
if (power < 0) throw new IllegalArgumentException();
return power == 0 ? 1L : baseNum * recPower(baseNum, power - 1);
}
or to check for overflow
Copypublic static long recPower(int baseNum, int power) {
if (power < 0) throw new IllegalArgumentException();
return power == 0 ? 1L
: Math.multiplyExact(baseNum, recPower(baseNum, power - 1));
}
You can use BigInteger which has a much, much greater limit.
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.
Answer from willeM_ Van Onsem on Stack OverflowWhen it's a power of 2 keep in mind that you can use a simple and fast shift expression: 1 << exponent
For example:
22 = 1 << 2 = (int) Math.pow(2, 2)
210 = 1 << 10 = (int) Math.pow(2, 10)
For larger exponents (over 31) use long instead:
232 = 1L << 32 = (long) Math.pow(2, 32)
BTW, in Kotlin you have shl instead of <<:
(Java) 1L << 32 = 1L shl 32 (Kotlin)
Integers are only 32 bits. This means that its max value is 2^31 -1. As you see, for very small numbers, you quickly have a result which can't be represented by an integer anymore. That's why Math.pow uses double.
If you want arbitrary integer precision, use BigInteger.pow. But it's of course less efficient.
Because Java can support max signed int as 0x7fffffff which is 2^31-1.
2^31 = 0x80000000 is negative so Positive is 2^31-1
Binary level comparasion would be:
10000000000000000000000000000000 --> 2147483648 --> 2^31
01111111111111111111111111111111 --> 2147483647 --> 2^31 -1
^ Sign bit
The same rule does apply... 7 is 2^3 - 1. And yes, it's because of the 0. :)
In contrast, negatives go to -(2^31)
So there's 2^31 negative numbers, one 0, and 2^31-1 strict positives, which add to...
2^31 + 1 + 2^31 - 1 = 2 * 2^31 = 2^32
You can use BigInteger.pow() to take a large exponent. Since 109 fits into an int and is also exactly representable as a double, you can do this:
int exp = (int) Math.pow(10, 9);
BigInteger answer = BigInteger.valueOf(2).pow(exp);
This obviously breaks down for exponents larger than Integer.MAX_VALUE. However, you can then use BigInteger.modPow(BigInteger exponent, BigInteger m) to raise a BigInteger to another BigInteger as a power, module a third BigInteger. You just need to first create a BigInteger that is larger than your expected answer to serve as a modulus.
If you have 2^x, where x is some large number then you can do this through bit shifting. Example:
2^4 == (1 << 4);
2^12 == (1 << 12);
With BigIntegers you can do the same thing with the shiftLeft() and shiftRight() methods.