src/java.base/share/classes/java/lang/Integer.java
changeset 50211 5afedc9e4662
parent 49426 7f3986bad197
child 51388 0dcd27526967
--- a/src/java.base/share/classes/java/lang/Integer.java	Tue May 22 05:20:48 2018 -0400
+++ b/src/java.base/share/classes/java/lang/Integer.java	Tue May 22 14:44:18 2018 +0200
@@ -1618,16 +1618,15 @@
      */
     @HotSpotIntrinsicCandidate
     public static int numberOfLeadingZeros(int i) {
-        // HD, Figure 5-6
+        // HD, Count leading 0's
         if (i <= 0)
             return i == 0 ? 32 : 0;
-        int n = 1;
-        if (i >>> 16 == 0) { n += 16; i <<= 16; }
-        if (i >>> 24 == 0) { n +=  8; i <<=  8; }
-        if (i >>> 28 == 0) { n +=  4; i <<=  4; }
-        if (i >>> 30 == 0) { n +=  2; i <<=  2; }
-        n -= i >>> 31;
-        return n;
+        int n = 31;
+        if (i >= 1 << 16) { n -= 16; i >>>= 16; }
+        if (i >= 1 <<  8) { n -=  8; i >>>=  8; }
+        if (i >= 1 <<  4) { n -=  4; i >>>=  4; }
+        if (i >= 1 <<  2) { n -=  2; i >>>=  2; }
+        return n - (i >>> 1);
     }
 
     /**