These codes are not equal:
- C version allocates byte array where as Java’s – integer array
- 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?