-4

I just played around in AndroidStudio and came across this number: 8198552921648689606

my code looks like this:

int x = 1;

for (int i = 0; i<64; i++)
{
x++;
x *= 10;
}
print(x);

For some reason it gets stuck on this weird number. I expected it to jump around on the integer line but for all the numbers above 64 it just stays like this.

It only happens with the numbers 10 or 100 or 1000(or any factor of ten i would assume) as a factor and it happens in Java as well.

The numbers that it gets stuck on change depending on the type of bit system and the numbers used but it gets stuck all the time.

Seems to be a weird coincidence regarding the integer line but i would really like to know whats going on.

Update: Seems to happen with even factors but not with odd ones. Still cant wrap my head around this though.

Kallefinn
  • 1
  • 1

1 Answers1

0

I don't understood quite well what you are trying to do, but here is what is happen:

First of all, if you want to follow the "transformation" of values in your 'x' variable, you should include the print statement inside the loop:

for (int i = 0; i<64; i++)
{
x++;
x *= 10;
print(x); // <- inserted in here
}
print(x);

Second, if you do this you should be able to see that scenario:

i x (after "x++") after ( x *= 10)
1 1 2 20
2 20 21 210
3 210 211 2110
... ... ... ...

When i = 63 your x will be = 211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110 (2.1*10^127). Notice that this number is way greater than the Java's int limit 2147483647

Lukasavicus
  • 137
  • 8
  • I knew that it is way to big for the int limit, but try the same code with the number 15 as a factor and it wont get stuck on the same number. – Kallefinn Apr 23 '21 at 06:16