简体   繁体   English

截断的二进制对数

[英]truncated binary logarithm

I have a question about this problem, and any help would be great! 我对这个问题有疑问,任何帮助都将非常有用!

Write a program that takes one integer N as an argument and prints out its truncated binary logarithm [log2 N]. 编写一个程序,该程序将一个整数N作为参数,并打印出其截断的二进制对数[log2 N]。 Hint: [log2 N] = l is the largest integer ` such that 2^l <= N. 提示:[log2 N] = 1是最大整数`,使得2 ^ l <=N。

I got this much down: 我很失望:

int N = Integer.parseInt(args[0]);  
    double l = Math.log(N) / Math.log(2);
    double a = Math.pow(2, l);

But I can't figure out how to truncate l while keeping 2^l <= N 但是我不知道如何在保持2 ^ l <= N的同时截断l

Thanks 谢谢


This is what i have now: 这就是我现在所拥有的:

int N = Integer.parseInt(args[0]); int N = Integer.parseInt(args [0]);

    int i = 0; // loop control counter
    int v = 1; // current power of two
    while (Math.pow(2 , i) <= N) {


    i = i + 1;
    v = 2 * v; 
    }

    System.out.println(Integer.highestOneBit(N)); 

This prints out the integer that is equal to 2^i which would be less than N. My test still comes out false and i think that is because the question is asking to print the i that is the largest rather than the N. So when i do 这将打印出等于2 ^ i的整数,该整数将小于N。我的测试仍然失败,我认为这是因为问题是要打印最大的i而不是N。所以当我做

Integer.highestOneBit(i) Integer.highestOneBit(i)

the correct i does not print out. 正确的我没有打印出来。 For example if i do: N = 38 then the highest i should be 5, but instead it prints out 4. 例如,如果我这样做:N = 38,那么最高的i应该是5,但是它将打印出4。

Then i tried this: 然后我尝试了这个:

            int N = Integer.parseInt(args[0]);

    int i; // loop control counter
    for (i= 0; Math.pow(2 , i) == N; i++) {

    }
            System.out.println(Integer.highestOneBit(i)); 

Where if i make N = 2 i should print out to be 1, but instead it is printing out 0. 如果我使N = 2,我应该将输出打印为1,而是将其打印为0。

I've tried a bunch of things on top of that, but cant get what i am doing wrong. 最重要的是,我尝试了很多事情,但是我做错了。 Help would be greatly appreciated. 帮助将不胜感激。 Thanks 谢谢

I believe the answer you're looking for here is based on the underlying notion of how a number is actually stored in a computer, and how that can be used to your advantage in a problem such as this. 我相信您在这里要寻找的答案是基于一个基本概念,即数字实际上是如何存储在计算机中的,以及如何在诸如此类的问题中将其用于您的利益

Numbers in a computer are stored in binary - a series of ones and zeros where each column represents a power of 2: 计算机中的数字以二进制形式存储-一系列的一和零,其中每一列代表2的幂:

在此处输入图片说明

(Above image from http://www.mathincomputers.com/binary.html - see for more info on binary) (上图来自http://www.mathincomputers.com/binary.html-有关二进制的更多信息,请参见)

The zeroth power of 2 is over on the right. 2的零次方在右边。 So, 01001, for example, represents the decimal value 2^0 + 2^3; 因此,例如01001代表十进制值2 ^ 0 + 2 ^ 3; 9. 9。

This storage format, interestingly, gives us some additional information about the number. 有趣的是,这种存储格式为我们提供了有关数字的其他信息。 We can see that 2^3 is the highest power of 2 that 9 is made up of. 我们可以看到2 ^ 3是9组成的2最高幂 Let's imagine it's the only power of two it contains, by chopping off all the other 1's except the highest . 让我们想象一下,通过斩除除最高值以外的所有其他1 ,它是其中包含的唯一2的幂。 This is a truncation , and results in this: 这是一个截断 ,结果是:

01000 01000

You'll now notice this value represents 8, or 2^3. 您现在会注意到该值表示8,即2 ^ 3。 Taking it down to basics, lets now look at what log base 2 really represents. 归根到底,现在让我们看一下log base 2 真正代表什么。 It's the number that you raise 2 to the power of to get the thing your finding the log of. 这是您将2提升为获得所需结果的次数。 log2(8) is 3. Can you see the pattern emerging here? log2(8)是3。您能看到这里出现的模式吗?

  • The position of the highest bit can be used as an approximation to it's log base 2 value. 的最高位的位置可以被用作近似它的记录基体2的值。

2^3 is the 3rd bit over in our example, so a truncated approximation to log base 2(9) is 3. 2 ^ 3是本例中的第3位,因此对数基数2(9)的截断近似值为3。

So the truncated binary logarithm of 9 is 3. 2^3 is less than 9; 因此9的截断二进制对数为3。2 ^ 3小于9; This is where the less than comes from, and the algorithm to find it's value simply involves finding the position of the highest bit that makes up the number. 这是小于值的来源,找到其值的算法仅涉及找到组成该数字的最高位的位置。

Some more examples: 其他示例:

12 = 1100. Position of the highest bit = 3 (starting from zero on the right). 12 =1100。最高位的位置= 3(从右侧的零开始)。 Therefore the truncated binary logarithm of 12 = 3. 2^3 is <= 12. 因此,截断后的二进制对数12 =3。2 ^ 3 <= 12。

38 = 100110. Position of the highest bit = 5. Therefore the truncated binary logarithm of 38 = 5. 2^5 is <= 38. 38 =100110。最高位的位置=5。因此,截断后的二进制对数38 =5。2^ 5 <= 38。

This level of pushing bits around is known as bitwise operations in Java. 这种将位推入的水平在Java中称为按位操作。

Integer.highestOneBit(n) returns essentially the truncated value. Integer.highestOneBit(n)本质上返回截断的值。 So if n was 9 (1001), highestOneBit(9) returns 8 (1000), which may be of use. 因此,如果n为9(1001),则mostOneBit(9)返回8(1000),这可能有用。

A simple way of finding the position of that highest bit of a number involves doing a bitshift until the value is zero. 发现的一定数量的最高位的位置的简单方法包括做一个位位移 ,直到值为零。 Something a little like this: 有点像这样:

// Input number - 1001:
int n=9;
int position=0;
// Cache the input number - the loop destroys it.
int originalN=n;

while( n!=0 ){
    position++; // Also position = position + 1;
    n = n>>1; // Shift the bits over one spot (Overwriting n).
    // 1001 becomes 0100, then 0010, then 0001, then 0000 on each iteration. 
    // Hopefully you can then see that n is zero when we've 
    // pushed all the bits off.
}

// Position is now the point at which n became zero.
// In your case, this is also the value of your truncated binary log.
System.out.println("Binary log of "+originalN+" is "+position);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM