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?
>> is arithmetic shift right, >>> is logical shift right. In an arithmetic shift, the sign bit is extended to preserve the signedness of the number.
Java creates applications that run in a virtual machine or browser while JavaScript code is run on a browser only. Java code needs to be compiled while JavaScript code are all in text. They require different plug-ins.
JavaScript(JS) is not similar or related to Java. Both the languages have a C-like syntax and are widely used in client-side and server-side Web applications, but there are few similarities only. Features of Javascript are as follows: JavaScript was created in the first place for DOM manipulation.
While Java is extensive, faster, and supports better app development process, JavaScript is lighter and better suited for interactive apps. So, Java or JavaScript, pick the language as per your development need.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With