8199843: Optimize Integer/Long.highestOneBit()

Reviewed-by: redestad, plevart
This commit is contained in:
Ivan Gerasimov 2018-03-26 17:30:14 -07:00
parent 902048c325
commit 0af73c4306
2 changed files with 2 additions and 15 deletions

View file

@ -1574,13 +1574,7 @@ public final class Integer extends Number implements Comparable<Integer> {
* @since 1.5
*/
public static int highestOneBit(int i) {
// HD, Figure 3-1
i |= (i >> 1);
i |= (i >> 2);
i |= (i >> 4);
i |= (i >> 8);
i |= (i >> 16);
return i - (i >>> 1);
return i & (MIN_VALUE >>> numberOfLeadingZeros(i));
}
/**

View file

@ -1719,14 +1719,7 @@ public final class Long extends Number implements Comparable<Long> {
* @since 1.5
*/
public static long highestOneBit(long i) {
// HD, Figure 3-1
i |= (i >> 1);
i |= (i >> 2);
i |= (i >> 4);
i |= (i >> 8);
i |= (i >> 16);
i |= (i >> 32);
return i - (i >>> 1);
return i & (MIN_VALUE >>> numberOfLeadingZeros(i));
}
/**