整数的位数 [英] Bit count of an integer

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

问题描述

我读了一些旧帖子,他们以非常不同的方式完成了这项任务。怎么

是下一个?


感谢您的时间。

/

* ************************************************* ****************************

*计算整数设置的位数。例如。整数:0x01101011,bitcount:

5


*********************** *************************** *********************** ***** /


int bitcount(int i)

{

unsigned int cnt = 0,MASK;


for(MASK = 0x1; MASK!= 0; MASK<< = 1)

if((MASK& i)== MASK)

cnt ++;

返回cnt;

}

解决方案
" lovecreatesbea ... @ gmail.com" < lo *************** @ gmail.comwrites:


我读了一些旧帖子,他们做了这个任务非常不同的方式。怎么

是下一个?


感谢您的时间。


/

********************************************* *** *****************************

*计算整数设置的位数。例如。整数:0x01101011,bitcount:

5


*********************** *************************** *********************** ***** /


int bitcount(int i)

{

unsigned int cnt = 0,MASK;


for(MASK = 0x1; MASK!= 0; MASK<< = 1)

if((MASK& i)== MASK)

cnt ++;

返回cnt;

}



别人会说出来你左边的范围超出了范围(我估计它可能很好用无符号的int)但是我会删除

与MASK的比较并将MASK放在小写字母以上case倾向于

表示常量/ #define。


lovecreatesbea ... @ gmail.com写道:


我读了一些旧帖子,他们以非常不同的方式完成了这项任务。

如何以下?

/ *

*计算整数设置的位数。例如。整数:0x01101011,bitcount:5

* /


int bitcount(int i)

{

unsigned int cnt = 0,MASK;


for(MASK = 0x1; MASK!= 0; MASK<< = 1)

if ((MASK& i)== MASK)

cnt ++;

返回cnt;

}



不要理会大声读取此代码的问题(我曾经是一名

培训师,我知道一个名为cnt的变量的缺陷)...... />

为什么我们需要这么多班次?你需要(sizeof int)* CHAR_BITS转移

,即使i中没有设置位。


至少,这可能不那么浪费了.. 。(未经测试)


int bitcount(unsigned int i)/ *你不想要它未签名吗? * /

{

unsigned int cnt = 0;

while(i){

cnt + = i &安培; 1;

i>> = 1;

}

返回cnt;

}


似乎相当于(但比更详细)
http://graphics.stanford.edu/~seande...ntBitsSetNaive


然后该网页会查看其他技术...


Richard< rg **** @ gmail.comwrote:


" lovecreatesbea ... @ gmail .COM" < lo *************** @ gmail.comwrites:


int bitcount(int i)

{

unsigned int cnt = 0,MASK;


for(MASK = 0x1; MASK!= 0; MASK<< = 1 )

if((MASK& i)== MASK)

cnt ++;

返回cnt;

}



别人会告诉你左移超出范围(我认为
它可能很好用一个无符号的int)



如果涉及按位操作,请务必确认。他们是一个讨厌的,令人惊讶的混蛋,迟早会咬你,如果你不是b $ b。不过,在这种情况下,我确定你算是正确的。


但是我会将比较移除到MASK



是;这是一个优化,我甚至不希望一个好的优化器

自动获得。


并将MASK置于更低的位置因为大写的情况往往表示

常数/ #define。



我同意。


Richard


I read some old posts, they did this task in very different ways. How
is the following one?

Thank you for your time.
/
************************************************** *****************************
* Count the bit set in an integer. eg. integer: 0x01101011, bitcount:
5

************************************************** ****************************/

int bitcount(int i)
{
unsigned int cnt = 0, MASK;

for (MASK = 0x1; MASK != 0; MASK <<= 1)
if ((MASK & i) == MASK)
cnt++;
return cnt;
}

解决方案

"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:

I read some old posts, they did this task in very different ways. How
is the following one?

Thank you for your time.
/
************************************************** *****************************
* Count the bit set in an integer. eg. integer: 0x01101011, bitcount:
5

************************************************** ****************************/

int bitcount(int i)
{
unsigned int cnt = 0, MASK;

for (MASK = 0x1; MASK != 0; MASK <<= 1)
if ((MASK & i) == MASK)
cnt++;
return cnt;
}

Someone else will tell you about left shifting out of range ( I reckon
its probably fine with an unsigned int) but I would remove the
comparison to MASK and put MASK in lower case since upper case tends to
indicate a constant/#define.


lovecreatesbea...@gmail.com wrote:

I read some old posts, they did this task in very different ways. How
is the following one?
/*
* Count the bit set in an integer. eg. integer: 0x01101011, bitcount: 5
*/

int bitcount(int i)
{
unsigned int cnt = 0, MASK;

for (MASK = 0x1; MASK != 0; MASK <<= 1)
if ((MASK & i) == MASK)
cnt++;
return cnt;
}

Leaving aside the problems of reading this code aloud (I used to be a
trainer and I know the pitfalls of a variable called "cnt")...

Why do we need so many shifts? You need (sizeof int) * CHAR_BITS shifts
even if there are no bits set in i.

At the least, this may be less wasteful... (untested)

int bitcount(unsigned int i) /* don''t you want it unsigned? */
{
unsigned int cnt = 0;
while(i) {
cnt += i & 1;
i >>= 1;
}
return cnt;
}

Seems equivalent to (but more verbose than)
http://graphics.stanford.edu/~seande...ntBitsSetNaive

That webpage then looks at other techniques...


Richard <rg****@gmail.comwrote:

"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrites:

int bitcount(int i)
{
unsigned int cnt = 0, MASK;

for (MASK = 0x1; MASK != 0; MASK <<= 1)
if ((MASK & i) == MASK)
cnt++;
return cnt;
}


Someone else will tell you about left shifting out of range ( I reckon
its probably fine with an unsigned int)

Where bitwise operations are concerned, never reckon, make sure. They''re
nasty, surprising bastards that will bite you sooner or later if you
don''t. In this case, though, I''m sure you reckoned correctly.

but I would remove the comparison to MASK

Yes; it''s an optimisation that I wouldn''t expect even a good optimiser
to get automatically.

and put MASK in lower case since upper case tends to indicate a
constant/#define.

I agree.

Richard


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

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