Possible Duplicates:
Previous power of 2
Getting the Leftmost Bit
我想的是,在座标有5
,即101
。 我的回答应为<代码>100。 <代码>9 i.e1001
, 回答应为1000
。
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.) 算法的复杂性取决于投入中确定的参照数。
检查链接。 这是一种令人.慕和启发的读物,可能给你更多的想法。
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 ...
I have been searching for sample code creating iterator for my own container, but I haven t really found a good example. I know this been asked before (Creating my own Iterators) but didn t see any ...
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 ...
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?
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->...
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, ...
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 ...
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?