大于 lg N 的最小整数
我在某处读到:
大于lg N 的最小整数 是所需的位数 用二进制表示N,同理 大于的最小整数 log10 N 是位数 要求用十进制表示 N。
Java 语句
for (lgN = 0; N > 0; lgN++, N /= 2) ;
是一种简单的计算方法 大于 lg N 的最小整数
我可能在这里遗漏了一些东西,但是 Java 语句如何计算大于 lg N 的最小整数?
I was reading somewhere that:
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
I maybe missing something here but how does the Java statement calculate the smallest integer larger than lg N?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果重写为 while 循环可能会更清楚:
这可以被认为是“在移走所有 1 之前我们必须右移多少次”(留下零)
it might be clearer if rewritten as a while loop:
Which can be thought of as "how many times do we have to right shift before we have shifted off all the 1s" (leaving us with a zero)
把它写在纸上。例如,N = 1750
Write it out on paper. Example, for N = 1750
这是您应该问的问题的答案,即如何最好地计算它:
不要用循环来执行此操作。直接计算:
注意不要让
n == 0
。This is an answer to the question you should ask, namely how to best compute this:
Don't do this with a loop. Calculate it directly:
Be careful not to let
n == 0
.仅在示例输入上追踪此内容有任何问题吗?
lgN最后是4。这是大于
log(2, 10)
的最小整数Have any problem just tracing this on sample input?
lgN is 4 in the end. That's the smallest integer larger than
log(2, 10)
看起来这是在计算 N 的 log_2。那么用二进制来思考,需要多少位来表示 N?找出答案的方法是计算 N 除以 2 的次数(将 N 中的位向右移 1 个空间)。在 N 达到 0 之前执行此操作的次数就是您正在计算的值。
It appears this is calculating log_2 of N. So think in terms of binary, how many bits does it take to represent N? The way you find out is count the number of times you can divide N by 2 (shift the bits in N to the right 1 space). The number of times you do this before N reaches 0 is the value you're calculating.
对于那些偏离正题并试图通过为未提出的问题提供更有效的解决方案来“停止循环疯狂”的人,我建议这同时更高效和可读:
作为 Javadoc
Integer.numberOfLeadingZeros()< /code>
说:
我之前没有发布此内容,因为正如我所说,它是切线的。 OP 试图理解循环,而不是找到做某事的“最佳”方式。
For those going off on a tangent and trying to "stop the loop madness" by providing a more efficient solution to a question that wasn't asked, I propose this which is simultaneously more efficient and readable:
As the Javadoc for
Integer.numberOfLeadingZeros()
says:I didn't post this before since it was tangential as I said. The OP was trying to understand the loop, not find the "best" way of doing something.
考虑二进制形式的 N,左侧没有所有零。
例如,对于 19,则为 10011。
每个 N/=2 向右移动一位:
当 N 等于 0 时,lgN(步数)等于 log (N)。
Consider N in binary form, without all the zeros at the left.
For example, for 19 it would be 10011.
Each N/=2 is shifting this one bit at the right:
When N is equal to 0 then lgN (which is the number of step) is equal to log(N).
lg N 小于 k:
因此,大于 lg N 的最小整数是除以 2 所需的次数才能达到 0,这就是循环的作用。
lg N is smaller than k:
So, the least integer greater than lg N is the number of divisions by 2 required to get to 0, and that's what the loop does.
假设其他一切都正确,代码将使用循环外部存在的“lgN”变量。当循环结束时,“lgN”将是答案。
Assuming everything else is correct, the code would be using a "lgN" variable that exists outside of the loop. When the loop finishes, "lgN" would be the answer.
是的,对于以 2 为底的对数... (ln N)/(ln 2),不是自然对数
yes, for base 2 logarithm... (ln N)/(ln 2), not the natural logarithm
这是一个for循环。其初始条件是 lgN 的值为零。只要 N 大于零,循环就会继续。在循环中的每次迭代中,它都会将 lgN 加 1 并将 N 除以 2。
这是循环所做的非常字面的翻译。它是如何运作的?好吧,第一次循环时,它会增加 lgN 并将 N 除以 2。因此,如果 N 是 1,那么 N 现在为零,我们就完成了。如果 N 大于 1,我们必须再次迭代。每当你能将 N 除以 2 而不会为零时,二进制表示中就需要另一个位。代码基本上会问:“在达到 0 之前,我可以将 N 除以 2 多少次?”这是大于 lg N 的最小整数。
It is a for loop. Its initial condition is that lgN has the value zero. The loop continues so long as N is greater than zero. At each iteration in the loop, it adds one to lgN and divides N by 2.
That's a very literal translation of what the loop does. How does it work? Well, the first time through the loop, it increments lgN and divides N by 2. So if N was 1, N is now zero, and we're done. If N was larger than one, we have to iterate again. Every time you can divide N by 2 without getting to zero is another required bit in the binary representation. The code basically asks, "how many times can I divide N by 2 before I reach 0?" and that is the smallest integer larger than lg N.