有点抨击 [英] bit bashing

查看:40
本文介绍了有点抨击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一小块程序(再次只有14行程序)

计算一个数字中设置的位数。


输入

输出


0

0(0000000)


5
2(0000101)

7

3(0000111)

int CountBits(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]);

返回x;

}


找出上述程序中使用的逻辑。

解决方案

< blockquote> Rohit kumar Chandel写道:


做你自己的作业,或者给我们你的

老师的地址。我们会直接给他回答


-

jacob navia

jacob at jacob point remcomp point fr

logiciels / informatique
http:// www。 cs.virginia.edu/~lcc-win32


Rohit kumar Chandel写道:


这是一小块程序(再次只有14行程序)



....


找出上述程序中使用的逻辑。



这对我来说闻起来像家庭作业......我还没有做过30多年的家庭作业 - 我为什么要做你的作业?




" jacob navia" < ja *** @ nospam.com写信息

新闻:fn ********** @ aioe.org ...


Rohit kumar Chandel写道:


做你自己的作业,或者给我们你的

老师的地址。我们会直接向他发送答案



这不是一个家庭作业问题。我试图理解逻辑,但是

无法得到它。

这是一段代码,从我的朋友那里传来作为一个谜题。我
一般计算这样设置的位数:


int CountBits(unsigned int x)

{

int count = 0;

while(x)

{

count ++;

x = x& ;(x-1);

}

返回计数;

}

更简单。


现在我可以得到一个答案,或者你还需要我老师的地址:-)


问候

Rohit


Here is a small piece of program(again just 14 lines of program) which
counts the number of bits set in a number.

Input
Output

0
0(0000000)

5
2(0000101)

7
3(0000111)
int CountBits (unsigned int x )
{
static unsigned int mask[] = { 0x55555555,
0x33333333,
0x0F0F0F0F,
0x00FF00FF,
0x0000FFFF

} ;

int i ;
int shift ; /* Number of positions to shift to right*/

for ( i =0, shift =1; i < 5; i ++, shift *= 2)
x = (x & mask[i ])+ ( ( x >shift) & mask[i]);
return x;
}

Find out the logic used in the above program.


解决方案

Rohit kumar Chandel wrote:

Do your own homework, or give us the address of your
teacher. We will send him the answer directly

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32


Rohit kumar Chandel wrote:

Here is a small piece of program(again just 14 lines of program)

....

Find out the logic used in the above program.

This smells like homework to me... I haven''t done homework in over 30
years - why should I do yours?



"jacob navia" <ja***@nospam.comwrote in message
news:fn**********@aioe.org...

Rohit kumar Chandel wrote:

Do your own homework, or give us the address of your
teacher. We will send him the answer directly

This is not a homework question. I have tried to understand the logic but
could not get it.
This is a piece of code which came to me from my friend as a puzzle. I
generally calculate number of bits set like this:

int CountBits(unsigned int x)
{
int count=0;
while(x)
{
count++;
x = x&(x-1);
}
return count;
}
which is much simpler.

Now can I get an answer, or still you need address for my teacher :-)

Regards
Rohit


这篇关于有点抨击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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