Differences in results between Java and Javascript can be observed when working with large integers.
For example:
getCode(1747,1763,-268087281,348400)
in Java returns 1921968083
, while in Javascript it returns 2.510115715670451e+22
.
Is there a way to achieve the Java result in Javascript?
After conducting some research, it appears that the issue may be related to signed/unsigned 32/64 bit integer operations. Adding |0
at the end of all operations got me closer with 1921618368
, although not identical.
Java:
public String getCode(int intVal1, int intVal2, int intVal3, int IntA) {
int intVal5 = intVal2 ^ intVal3 ;
intVal5 = intVal5 + (intVal1 | IntA ) ;
intVal5 = intVal5 * (intVal1 | intVal3 ) ;
intVal5 = intVal5 * (intVal2 ^ IntA ) ;
return String.valueOf(intVal5);
}
Javascript:
function getCode(intVal1,intVal2,intVal3,IntA) {
var intVal5 = intVal2 ^ intVal3 ;
intVal5 = intVal5 + (intVal1 | IntA ) ;
intVal5 = intVal5 * (intVal1 | intVal3 ) ;
intVal5 = intVal5 * (intVal2 ^ IntA ) ;
return intVal5;
}