Comment

Comment by fefe23

A few points.

1. Looking at the Java bytecode is practically meaningless, you would have to look at the machine code the JIT is creating.

2. A division by 2 is identical to a shift right by 1 only if the integer is unsigned. Java integers are signed. Try this program in C to see for yourself:

int foo(int a) { return a/2; } int bar(int a) { return a>>1; }

Run gcc -S -O2 to get assembly output in text form.

Basically, the problem is this:

5/2 -> 2 (ok, rounds down)

5>>1 -> 2 (ok, same)

-5/2 -> -2 (ok, rounds down)

-5>>1 -> -3 (oops!)

3. The question is really about the JIT backend for the target platform, which means CPU platform, not OS platform. So "on Android" does not make much sense here, as Android exists for x86 and ARM and those JIT backends might behave differently.

parent fefe23 6 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton