3 ms·
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 i
by fefe23 6y ago
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.
- lgessler 6y agoIn defense of the author re: 2, since the application domain seems to be manipulating indices for a binary tree represented in an array, the integer will always be non-negative.
- anyfoo 6y agoAll things directly addressed by the article.
- ryenus 6y ago-5/2 -> -2 (ok, rounds down) Actually this is rounding up, to zero, as -2.5 < -2 < 0
- maxwindiff 6y agoThe article did investigate the assembly.
- pjmlp 6y agoYet another one that didn't bother to actually read the article.
- renewiltord 6y agoOne addition: You can't just divide by 2 and expect that to be equivalent to shifting right by 1. That only works if the integer is unsigned. For signed integers, that won't yield the right result. This is trivial to verify.