So I have recently made a simple calculator in c++ that, among other things, can calculate exponents, but it only works until 2^31. Here is the problem, my input/output is as follows:
I: 2^31
O: 131072
I: 2^32
O: 0
I: 13107*2
O: 262144
Basically it can't do 2^32 but it can do (2^31)*2, and I just can't understand why. If any one can help me and explain why I would really appreciate it. Here is the code that calculates exponents btw:
long exp(long x, int y) {
int p;
if (y % 2 == 0) {
p = 1;
}
else {
p = x;
}
while (y > 1) {
x *= x;
y /= 2;
}
return x*p;
}