What is the difference between operator >>> in Java and JavaScript?

Kirillius Labutin :

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?

Iluvatar :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=421141&siteId=1
Recommended