2 ms·
TL;DR: Unsigned integer division by 2 can be optimized by the compiler only if there are unsigned integer types in the language. Java doesn't have unsigned prim
by copperx 10y ago
TL;DR: Unsigned integer division by 2 can be optimized by the compiler only if there are unsigned integer types in the language. Java doesn't have unsigned primitive types (besides char), so x >>> 1 will be faster than x / 2 if you know that x >= 0.
Please correct me if this is wrong.
- Scaevolus 10y agoCorrect. Java's >>> is unsigned, since it's a logical shift, but there's no corresponding unsigned division. Here's a clearer example of how GCC handles the different cases of signed vs unsigned division and shifting: https://godbolt.org/g/glC97v https://godbolt.org/g/glC97v Note that the signed division is the most expensive, but it still doesn't resort to a full divide instruction.