我在某处读到:
The smallest integer larger than lg N is the number of bits required to represent N in binary, in the same way that the smallest integer larger than log10 N is the number of digits required to represent N in decimal.
The Java statement
for (lgN = 0; N > 0; lgN++, N /= 2) ;
is a simple way to compute the smallest integer larger than lg N
我可能在这里遗漏了一些东西,但是 Java 语句如何计算大于 lg N 的最小整数?
最佳答案
如果改写成while循环可能会更清楚:
lgN = 0
while( N > 0 ) {
lgN++;
N = N/2;
}
这可以被认为是“在我们移掉所有 1 之前我们必须右移多少次”(留下一个零)
关于java - 大于 lg N 的最小整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3935138/