nscboy:用移位算法不是更简洁吗?
int func(int x)
{
int countx = 0;
for(int i=0;i<32;i++){
if((x>>i)&1)
countx ++;
}
return countx;
}
(发表于2005-1-12 13:35:00)
sagas:还是原来算法效率高啊。
(发表于2005-1-14 8:35:00)
sagas:也可以切成四个8bit用查找表算法,估计统计效率更高:
const byte table[256]={...};
int func(int x)
{
byte *p=(byte*)&x;
return table[p[0]]+table[p[1]]+table[p[2]]+table[p[3]];
;
}
(发表于2005-1-14 8:41:00)
du51:int bits(unsigned int x )
{
static unsigned int mask[] = { 0x55555555,
0x33333333,
0x0F0F0F0F,
0x00FF00FF,
0x0000FFFF };
int i ;
int shift ;
for ( i=0, shift=1; i<5; i++, shift*=2 )
{
x = (x&mask[i])+((x>>shift)&mask[i]);
}
return x;
}
(发表于2005-4-28 2:37:00)
鬼龙之舞:calc1 proc n
mov edx,n
xor eax,eax
.while edx
shr edx,1
adc eax,0
.endw
ret
calc1 endp
(发表于2005-5-3 23:02:00)
ccsz_ch:楼主的效率最高.........
(发表于2006-1-13 10:36:00)
..........................................................................
--------------------next---------------------
阅读(106) | 评论(0) | 转发(0) |