English 中文(简体)
计算一个只有比值组数的数位数的最快方法是什么? [复制]
原标题:What is fastest method to calculate a number having only bit set which is the most significant digit set in another number? [duplicate]
  • 时间:2010-08-04 15:24:38
  •  标签:
  • c++
  • c
This question already has answers here:
Closed 12 years ago.

Possible Duplicates:
Previous power of 2
Getting the Leftmost Bit

我想的是,在座标有5,即101。 我的回答应为<代码>100。 <代码>9 i.e1001, 回答应为1000

问题回答

您可以不限制的机器。 例如,一些机器支持一项称为“零点”的指令,或能够迅速加以仿效。 如果你能够接受这一指示(例如用gcc),你可以写:

#include <limits.h>
#include <stdint.h>
uint32_t f(uint32_t x) 
{
    return ((uint64_t)1)<<(32-__builtin_clz(x)-1);
}
int main()
{
    printf("=>%d
",f(5));
    printf("=>%d
",f(9));
}

f(x) 退还你想要的物品(最少和有x>=y和=2**n)。 汇编者现在将产生目标机器的最佳代码序列。 例如,在为违约x86_64建筑进行汇编时,f(a)认为:

    bsrl    %edi, %edi
    movl    $31, %ecx
    movl    $1, %eax
    xorl    $31, %edi
    subl    %edi, %ecx
    salq    %cl, %rax
    ret

你们看不见,这里没有 lo! 7 指示,没有分支机构。

但是,如果我告诉我的编辑(gcc-4.5),以优化使用现在使用的Im机(AMD Phenom-II),那么就是为了 f():

    bsrl    %edi, %ecx
    movl    $1, %eax
    salq    %cl, %rax
    ret

这也许是这一机器走得最快的道路。

EDIT: f(0)本可产生UB, I ve set that (and the Assembly)。 而且,uint32_t 意味着我可以写32,而不必感到有罪。

http://my.safaribooksonline.com/0-201-91465-4/38“rel=“nofollow noreferer” 7. Hacker s Delight, a nice Branchless Solutions:

uint32_t flp2 (uint32_t x)
{
    x = x | (x >> 1);
    x = x | (x >> 2);
    x = x | (x >> 4);
    x = x | (x >> 8);
    x = x | (x >> 16);
    return x - (x >> 1);
}

这通常有12项指示。 如果万国邮联的指令是“零点”的话,你可以做的更少。

int input = 5;
std::size_t numBits = 0;
while(input)
{
    input >>= 1;
    numBits++;
}
int output = 1 << (numBits-1);

这是一项与计票有关的任务:。 参见

采用2a(这是我对算法的偏好;不是最快)的方法,可以做到:

int highest_bit_mask (unsigned int n)  {
   while (n)  {
      if (n & (n-1)) {
         n &= (n-1) ;
      } else {
         return n;
      }
   }
   return 0;
}

<代码>n &=(n-1); 是指从n中删除最不重要的轨道。 (Corollary: n & (n-1) is false only when n ~ one bit set.) 算法的复杂性取决于投入中确定的参照数。

检查链接。 这是一种令人.慕和启发的读物,可能给你更多的想法。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?