I have the following question:
If asked whether to use a shift vs a multiply or divide for example the answer would be, let the JVM optimize.
Example here: is-shifting-bits-faster-than-multiplying
Now I was looking at the jdk source, for example Priority Queue and the code uses only shifting for both multiplication and division (signed and unsigned).
Taking for granted that the post in SO is the valid answer I was wondering why in jdk they prefer to do it by shifting?
Is it some subtle detail unrelated to performance? I am suspecting it must have something to do with over/underflow multiplication and division but I am not sure.
Anyone has an idea? Are subtly overflow issues handled better using shifting? Or it is just a matter of taste?
I think they do it in this specific example to use the sign bit. Java lacks unsigned types, so it is not possible to emulate
a >>> 1witha /= 2for numbers that use the most significant bit. Note how the code uses only>>>throughout your example. I am reasonably certain that this is to get full use of the entire bit range.