这是numberOfLeadingZeros(长ⅰ)在Long.java:我不认为long.java中的numberOfLeadingZeros(long i)是基于floor(log2(x))= 63 - numberOfLeadingZeros(x)!
public static int numberOfLeadingZeros(long i) {
// HD, Figure 5-6
if (i == 0)
return 64;
int n = 1;
int x = (int)(i >>> 32);
if (x == 0) { n += 32; x = (int)i; }
if (x >>> 16 == 0) { n += 16; x <<= 16; }
if (x >>> 24 == 0) { n += 8; x <<= 8; }
if (x >>> 28 == 0) { n += 4; x <<= 4; }
if (x >>> 30 == 0) { n += 2; x <<= 2; }
n -= x >>> 31;
return n;
}
和作者说,它是基于地板(LOG2(X))= 63 - numberOfLeadingZeros(x)或小区(LOG2(x)的)= 64 - numberOfLeadingZeros(x - 1),是真的吗?或者我可能很愚蠢,无法理解?
有人能解释给我吗?谢谢!
*“或者我是如此愚蠢,无法理解?”* - 我们无法回答这个问题:-)。 – 2012-03-03 04:30:22
:>。有时我很蠢! – liuxiaori 2012-03-03 05:12:36