l*8
2 楼
old problem. you need 5 ">>", 10 "&" and 5 "+" but no branching
l*8
4 楼
"看x&x-1多久到0" is in fact not so good because the loop has conditional
branches, and in the worst case the loop has 32 iterations.
Try this. Although it looks ugly, it's at least 4x faster than the "while(x){i++;x&=(x-1);}" method.
int countbit_fast(int x)
{
x = ((x >> 1) & 0x55555555) + (x & 0x55555555);
x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
x += (x >> 4);
x &= 0x0f0f0f0f;
x += (x >> 8);
x += (x >> 16);
x &= 0x3f;
return x;
}
branches, and in the worst case the loop has 32 iterations.
Try this. Although it looks ugly, it's at least 4x faster than the "while(x){i++;x&=(x-1);}" method.
int countbit_fast(int x)
{
x = ((x >> 1) & 0x55555555) + (x & 0x55555555);
x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
x += (x >> 4);
x &= 0x0f0f0f0f;
x += (x >> 8);
x += (x >> 16);
x &= 0x3f;
return x;
}
M*8
5 楼
some explanations here
http://everything2.com/e2node/Counting%25201%2520bits
http://everything2.com/e2node/Counting%25201%2520bits
X*r
6 楼
这个问题也太老了吧,N年前就流传开了,当时还在版上讨论过,在我被面试的时候居
然被问到了。
其实还有一种有效的方法:查表法,也可以查表和移位加相结合。
x){i++;x&=(x-1);}" method.
【在 l***8 的大作中提到】
: "看x&x-1多久到0" is in fact not so good because the loop has conditional
: branches, and in the worst case the loop has 32 iterations.
: Try this. Although it looks ugly, it's at least 4x faster than the "while(x){i++;x&=(x-1);}" method.
: int countbit_fast(int x)
: {
: x = ((x >> 1) & 0x55555555) + (x & 0x55555555);
: x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
: x += (x >> 4);
: x &= 0x0f0f0f0f;
: x += (x >> 8);
然被问到了。
其实还有一种有效的方法:查表法,也可以查表和移位加相结合。
x){i++;x&=(x-1);}" method.
【在 l***8 的大作中提到】
: "看x&x-1多久到0" is in fact not so good because the loop has conditional
: branches, and in the worst case the loop has 32 iterations.
: Try this. Although it looks ugly, it's at least 4x faster than the "while(x){i++;x&=(x-1);}" method.
: int countbit_fast(int x)
: {
: x = ((x >> 1) & 0x55555555) + (x & 0x55555555);
: x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
: x += (x >> 4);
: x &= 0x0f0f0f0f;
: x += (x >> 8);
t*t
7 楼
事实上本版精华区里就有....
http://www.mitbbs.com/bbsann2/computer.faq/Programming/algorithm/number_bit/%E5%AF%B9%E6%95%B0%E5%AD%97%E7%9A%84%E4%BD%8D%E6%93%8D%E4%BD%9C
【在 X****r 的大作中提到】
: 这个问题也太老了吧,N年前就流传开了,当时还在版上讨论过,在我被面试的时候居
: 然被问到了。
: 其实还有一种有效的方法:查表法,也可以查表和移位加相结合。
:
: x){i++;x&=(x-1);}" method.
http://www.mitbbs.com/bbsann2/computer.faq/Programming/algorithm/number_bit/%E5%AF%B9%E6%95%B0%E5%AD%97%E7%9A%84%E4%BD%8D%E6%93%8D%E4%BD%9C
【在 X****r 的大作中提到】
: 这个问题也太老了吧,N年前就流传开了,当时还在版上讨论过,在我被面试的时候居
: 然被问到了。
: 其实还有一种有效的方法:查表法,也可以查表和移位加相结合。
:
: x){i++;x&=(x-1);}" method.
l*8
8 楼
It wastes two unnecessary "&" operations. Compare that with mine and you'll
see.
【在 M**8 的大作中提到】
: some explanations here
: http://everything2.com/e2node/Counting%25201%2520bits
相关阅读
zookeeper还有公司在用吗Re: [BSSD] GTX1080是业余GPU (转载)幼稚问题:公司用python,怎么保护产权?重新捡起C++怎么上手?swift现在有GC了吗Index PDF和doc 是elasticsearch还是solr问下python spyder话说angular2现在公司里迁移的多吗fb为啥没有坚持用Cassandra ?要将数据同时生成JSON和XML, 应该先生成哪个,再转换成另一个有没有直接对pdf或者doc简历进行分析的开源软件?知道R为什么那么流行了哈哈湾区大公司搞AA的本质SQL要学到什么程度?要写sub procedure吗?如何让python dictionary sorting 的速度变得很快?大家都用啥gui diff工具store "" in c string经常有人说R是垃圾,其实是普通码工的智力水平不够有什么 open source 让做平台发 post, comments, votes?普丁说DNC被骇不是他干的, 谁干的不重要, 重要的是被揭发