大于 lg N 的最小整数

发布于 2024-09-27 03:59:57 字数 303 浏览 0 评论 0原文

我在某处读到:

大于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

黄昏下泛黄的笔记 2024-10-04 03:59:57

如果重写为 while 循环可能会更清楚:

lgN = 0
while( N > 0 ) {
    lgN++;
    N = N/2;
}

这可以被认为是“在移走所有 1 之前我们必须右移多少次”(留下零)

it might be clearer if rewritten as a while loop:

lgN = 0
while( N > 0 ) {
    lgN++;
    N = N/2;
}

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)

古镇旧梦 2024-10-04 03:59:57

把它写在纸上。例如,N = 1750

   lgN   N      N > 0?
1   0    1750   y
2   1    875    y
3   2    437    y
4   3    218    y
5   4    109    y
6   5    54     y
7   6    27     y
8   7    13     y
9   8    6      y  
10  9    3      y
11  10   1      y
12  11   0      n  stop; lgN = 11

Write it out on paper. Example, for N = 1750

   lgN   N      N > 0?
1   0    1750   y
2   1    875    y
3   2    437    y
4   3    218    y
5   4    109    y
6   5    54     y
7   6    27     y
8   7    13     y
9   8    6      y  
10  9    3      y
11  10   1      y
12  11   0      n  stop; lgN = 11
我喜欢麦丽素 2024-10-04 03:59:57

这是您应该问的问题的答案,即如何最好地计算它:

不要用循环来执行此操作。直接计算:

int bits = (int) Math.floor(Math.log(n)/Math.log(2)) + 1;

注意不要让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:

int bits = (int) Math.floor(Math.log(n)/Math.log(2)) + 1;

Be careful not to let n == 0.

不可一世的女人 2024-10-04 03:59:57

仅在示例输入上追踪此内容有任何问题吗?

step 1) N = 10, lgN = 0
step 2) N = 5,  lgN = 1
step 3) N = 2,  lgN = 2
step 4) N = 1,  lgN = 3
step 5) N = 0,  lgN = 4

lgN最后是4。这是大于 log(2, 10) 的最小整数

Have any problem just tracing this on sample input?

step 1) N = 10, lgN = 0
step 2) N = 5,  lgN = 1
step 3) N = 2,  lgN = 2
step 4) N = 1,  lgN = 3
step 5) N = 0,  lgN = 4

lgN is 4 in the end. That's the smallest integer larger than log(2, 10)

坦然微笑 2024-10-04 03:59:57

看起来这是在计算 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.

︶葆Ⅱㄣ 2024-10-04 03:59:57

对于那些偏离正题并试图通过为未提出的问题提供更有效的解决方案来“停止循环疯狂”的人,我建议这同时更高效和可读:

 public int bitsNeeded(int n) {
     return 32 - Integer.numberOfLeadingZeros(n);
 }

作为 Javadoc Integer.numberOfLeadingZeros()< /code>说:

请注意,此方法与以 2 为底的对数密切相关。对于所有正 int 值 x:

  • floor(log2(x)) = 31 - numberOfLeadingZeros(x)
  • ceil(log2(x)) = 32 - numberOfLeadingZeros(x - 1)

我之前没有发布此内容,因为正如我所说,它是切线的。 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:

 public int bitsNeeded(int n) {
     return 32 - Integer.numberOfLeadingZeros(n);
 }

As the Javadoc for Integer.numberOfLeadingZeros() says:

Note that this method is closely related to the logarithm base 2. For all positive int values x:

  • floor(log2(x)) = 31 - numberOfLeadingZeros(x)
  • ceil(log2(x)) = 32 - numberOfLeadingZeros(x - 1)

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.

栀子花开つ 2024-10-04 03:59:57

考虑二进制形式的 N,左侧没有所有零。

例如,对于 19,则为 10011。

每个 N/=2 向右移动一位:

  • 10011
  • 1001
  • 100
  • 10
  • 1
  • 0

当 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:

  • 10011
  • 1001
  • 100
  • 10
  • 1
  • 0

When N is equal to 0 then lgN (which is the number of step) is equal to log(N).

_畞蕅 2024-10-04 03:59:57

lg N 小于 k:

  • 当且仅当 N 小于 2^k
  • 当且仅当将 N 除以 2, k 次时,结果为 0(请记住,整数除法向下舍入)。

因此,大于 lg N 的最小整数是除以 2 所需的次数才能达到 0,这就是循环的作用。

lg N is smaller than k:

  • if and only if N is smaller than 2^k
  • if and only if dividing N by 2, k times, results in 0 (remember that integer division rounds down).

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.

山色无中 2024-10-04 03:59:57

假设其他一切都正确,代码将使用循环外部存在的“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.

音盲 2024-10-04 03:59:57

是的,对于以 2 为底的对数... (ln N)/(ln 2),不是自然对数

yes, for base 2 logarithm... (ln N)/(ln 2), not the natural logarithm

燕归巢 2024-10-04 03:59:57

这是一个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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文