如何设计新的bitcount函数? [英] How to design the new bitcount function?

查看:124
本文介绍了如何设计新的bitcount函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我进行练习时,我遇到了一个问题:

在二进制补码系统中,x& =(x-1)删除最右边的

x中的1位。解释为什么。使用这个观察来写一个更快的

版本的bitcount。

旧版本的bitcount函数如下:

/ * bitcount:count 1位数x * /

int bitcount(无符号x)

{

int b;

for(b = 0; x!= 0; x>> = 1)

if(x& 01)

b ++;

返回b;

}

我已经考虑了很长时间仍然没法抓住这一点,

如何重新编写new& amp ;更快" bitcount函数?

有什么建议吗?

谢谢。

When I do the exercises I met a question:
In a two''s complement number system, x &= (x-1) deletes the rightmost
1-bit in x. Explain why. Use this observation to write a faster
version of bitcount.
The old version of bitcount function as follows:
/* bitcount: count 1 bits in x */
int bitcount(unsigned x)
{
int b;
for (b = 0; x != 0; x >>= 1)
if (x & 01)
b++;
return b;
}
I had consider for a long time still fetch the point,
how can I re-write the "new & faster" bitcount function?
Any suggestion?
Thanks.

推荐答案

11月7日,14日:47,jackl ... @ gmail.com < jackl ... @ gmail.comwrote:
On 7 Nov, 14:47, "jackl...@gmail.com" <jackl...@gmail.comwrote:

当我做练习时,我遇到了一个问题:

两个'' s补码系统,x& =(x-1)删除x中最右边的

1位。解释为什么。使用这个观察来写一个更快的

版本的bitcount。

旧版本的bitcount函数如下:

/ * bitcount:* count 1位x * /

* * int bitcount(无符号x)

* * {

* * * * int b;

* * * *表示(b = 0; x!= 0; x>> = 1)

* * * * * * if(x& 01)

* * * * * * * * b ++;

* * * *返回b;

* *}

我已经考虑了很长一段时间仍然取得了重点,

我怎样才能重写新&更快" bitcount函数?

有什么建议吗?
When I do the exercises I met a question:
In a two''s complement number system, x &= (x-1) deletes the rightmost
1-bit in x. Explain why. Use this observation to write a faster
version of bitcount.
The old version of bitcount function as follows:
/* bitcount: *count 1 bits in x */
* *int bitcount(unsigned x)
* *{
* * * *int b;
* * * *for (b = 0; x != 0; x >>= 1)
* * * * * *if (x & 01)
* * * * * * * *b++;
* * * *return b;
* *}
I had consider for a long time still fetch the point,
how can I re-write the "new & faster" bitcount function?
Any suggestion?



x& =(x-1)删除一点。在没有剩余比特之前你能申请多少次




-

Nick Keighley

x &= (x-1) deletes a bit. How many times could you apply
it before there were no bits left?

--
Nick Keighley


ja******@gmail.com 写道:

当我进行练习时,我遇到了一个问题:

在二进制补码系统中,x& =(x-1 )删除x中最右边的

1位。解释为什么。使用这个观察来写一个更快的

版本的bitcount。

旧版本的bitcount函数如下:

/ * bitcount:count 1位数x * /

int bitcount(无符号x)

{

int b;

for(b = 0; x!= 0; x>> = 1)

if(x& 01)

b ++;

返回b;

}

我已经考虑了很长时间仍然没法抓住这一点,

如何重新编写new& amp ;更快" bitcount函数?

有什么建议吗?
When I do the exercises I met a question:
In a two''s complement number system, x &= (x-1) deletes the rightmost
1-bit in x. Explain why. Use this observation to write a faster
version of bitcount.
The old version of bitcount function as follows:
/* bitcount: count 1 bits in x */
int bitcount(unsigned x)
{
int b;
for (b = 0; x != 0; x >>= 1)
if (x & 01)
b++;
return b;
}
I had consider for a long time still fetch the point,
how can I re-write the "new & faster" bitcount function?
Any suggestion?



如果墙上有九十九瓶啤酒,你可以多次拿下一瓶啤酒然后将它传递下去你好吗?b $ b发现自己正面对一堵空墙?


-
Er ********* @ sun.com


文章< 74 ***** *****************************@o4g2000pra.g ooglegroups.com> ;,
ja ****** @ gmail.com < ja ****** @ gmail.comwrote:
In article <74**********************************@o4g2000pra.g ooglegroups.com>,
ja******@gmail.com <ja******@gmail.comwrote:

>当我做练习时,我遇到了一个问题:
在二进制补码系统中,x& =(x-1)删除最右边的
1 -bit in x。解释为什么。使用这个观察来写一个更快的bitcount版本。
旧版本的bitcount函数如下:
/ * bitcount:计算1位x * /

int bitcount(unsigned x)

{

int b;

for(b = 0; x!= 0; x>> ; = 1)

如果(x& 01)

b ++;

返回b;

} <我已经考虑了很长一段时间仍然没法抓住这一点,
我怎样才能重写新&更快" bitcount函数?
有什么建议吗?
谢谢。
>When I do the exercises I met a question:
In a two''s complement number system, x &= (x-1) deletes the rightmost
1-bit in x. Explain why. Use this observation to write a faster
version of bitcount.
The old version of bitcount function as follows:
/* bitcount: count 1 bits in x */
int bitcount(unsigned x)
{
int b;
for (b = 0; x != 0; x >>= 1)
if (x & 01)
b++;
return b;
}
I had consider for a long time still fetch the point,
how can I re-write the "new & faster" bitcount function?
Any suggestion?
Thanks.



想想看,你有一个函数,当呈现非零值

值将*总是*删除*正好* 1来自一个数字的位,无论

在哪个位数。


对于上面的示例函数,它将循环32次为

值0x80000000,其中使用关闭数字中的最后一位只会

循环一次。

-

风险公司:Crisell(70 Gnome术士),Flolyna(64人类战士),

Alassaria(64德鲁伊),存在(64矮人猎人),Lory(64人类圣骑士),

Trinalie(64 Gnome Mage),Kincann(65 Dwarf Rogue),

Mysa(66 Draenei Priest),Kathelone(64 Shaman)

Think about it, you have a function that when presented with a non-zero
value will *always* remove *exactly* 1 bit from a number regardless of
where in the number that bit is.

For the sample function you have above, it will loop 32 times for the
value 0x80000000, where using the "turn off last bit in number" will only
loop once.
--
The Venture Co: Crisell (70 Gnome Warlock), Flolyna (64 Human Warrior),
Alassaria (64 Druid), Being (64 Dwarf Hunter), Lory (64 Human Paladin),
Trinalie (64 Gnome Mage), Kincann (65 Dwarf Rogue),
Mysa (66 Draenei Priest), Kathelone (64 Shaman)


这篇关于如何设计新的bitcount函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆