[Solved] Why does this Java code behave differently than this C code?


These codes are not equal:

  1. C version allocates byte array where as Java’s – integer array
  2. C version does not do clamping of array elements as Java does

So in Java you should make a byte array instead:

byte[] tape = new byte[30000];

And remove clamping in adjust() function:

 // if(tape[dp] < 0) tape[dp] += 255;
 // if(tape[dp] > 255) tape[dp] -= 255;

Then Java’s version terminates too

OR if you want to keep integer array – then you must change your overflow/underflow rules :

 if(tape[dp] < 0) tape[dp] = 256-Math.abs(tape[dp]) % 256;
 if(tape[dp] > 255) tape[dp] = tape[dp] % 256;

because you implemented them not right

2

solved Why does this Java code behave differently than this C code?