JavaScript code:
alert( -123456 >>> 0 ); // Prints 4294843840
Java code:
System.out.println( -123456 >>> 0 ); // Prints -123456
Why? I've read documentation, but I didn't find the difference. How do I port JavaScript code to Java?
JavaScript code:
alert( -123456 >>> 0 ); // Prints 4294843840
Java code:
System.out.println( -123456 >>> 0 ); // Prints -123456
Why? I've read documentation, but I didn't find the difference. How do I port JavaScript code to Java?
Both are the logical right shift, but JavaScript has some weirdness in how it handles numbers. Normally numbers in JavaScript are floats, but the bitwise operations convert them to unsigned 32 bit integers. So even though the value looks like it shouldn't change, it converts the number to a 32 bit unsigned integer.
The value that you see 4294843840
is just the same bits as -123456
, but interpreted as unsigned instead of signed.