计算数字中设置的位数 [英] Count number of bits set in a number
问题描述
大家好,
我想编写一个程序来计算一个数字中设置的位数。
条件是我们不应该遍历每一位来查找是否
设定与否。
提前致谢,
-Mukesh
Hi all,
I want to write a program to count number of bits set in a number.
The condition is we should not loop through each bit to find whether
its set or not.
Thanks in advance,
-Mukesh
推荐答案
" Mack" < Ma ********** @ gmail.comschrieb im Newsbeitrag
news:11 ******************** **@d55g2000hsg.googlegr oups.com ...
"Mack" <Ma**********@gmail.comschrieb im Newsbeitrag
news:11**********************@d55g2000hsg.googlegr oups.com...
大家好,
我想编写一个程序来计算位数设置一个数字。
条件是我们不应该遍历每一位来查找
是否设置。
Hi all,
I want to write a program to count number of bits set in a number.
The condition is we should not loop through each bit to find whether
its set or not.
你最好自己做作业。
再见,Jojo
You''d better do your homework yourself.
Bye, Jojo
Mack写道:
Mack wrote:
大家好,
我想编写一个程序来计算a中设置的位数数字。
条件是我们不应该遍历每一位来查找
是否设置。
Hi all,
I want to write a program to count number of bits set in a number.
The condition is we should not loop through each bit to find whether
its set or not.
首先写下几个数字以及
他们的位数,使用你喜欢的任何方法计算
比特:
数字位
0 0
1 1
2 1
3 2
现在找到直线,给出最佳的
最小二乘拟合观测数据(任何电子表格<如果你不记得数学,那么
会为你做这件事。
取合适的系数并编写你的函数:
double bit_count(int number){
返回0.6 *数字+ 0.1;
}
使用更多数据点,如果你愿意,可以获得更准确的统计数据。在那里,那不是很难,是吗?
-
Eric Sosman
es ***** @ ieee-dot-org.inva 盖子
9月27,下午5:48,Eric Sosman< esos ... @ ieee-dot-org.invalidwrote:
On Sep 27, 5:48 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
Mack写道:
Mack wrote:
大家好,
我想编写一个程序来计算一个数字中设置的位数。
条件是我们不应该循环遍历每一位以查找
是否设置。
Hi all,
I want to write a program to count number of bits set in a number.
The condition is we should not loop through each bit to find whether
its set or not.
首先写下几个数字以及
他们的位数,使用你喜欢的任何方法来计算
比特:
数字位
0 0
1 1
2 1
3 2
现在找到直线,给出最佳的
最小二乘拟合观测数据(任何电子表格<如果你不记得数学,那么
会为你做这件事。
取合适的系数并编写你的函数:
double bit_count(int number){
返回0.6 *数字+ 0.1;
}
使用更多数据点,如果你愿意,可以获得更准确的统计数据。在那里,那不是很难,是吗?
-
Eric Sosman
esos ... @ ieee-dot-org.invalid
Start by writing down a few numbers along with
their bit counts, using any method you like to count
the bits:
Number Bits
0 0
1 1
2 1
3 2
Now find the straight line that gives the best
least-squares fit to the observed data (any spreadsheet
will do this for you if you can''t recall the math).
Take the fitted coefficients and write your function:
double bit_count(int number) {
return 0.6 * number + 0.1;
}
Use more data points, if you like, to get a statistically
more accurate fit. There, that wasn''t so hard, was it?
--
Eric Sosman
esos...@ieee-dot-org.invalid
一个粗略的想法就是这样......只是一个非常高级的想法。
Not确切的算法。
取你想要计算的位数,
第一位,检查它是否已设定,将它添加到
你的bitset计数器是否已设置。移动到下一位
然后下一位,检查它是否已设置,如果已设置,则将其添加到
您的bitset计数器。转到下一位。
使用按位运算符(lshift或rshift,&)实现并享受乐趣。
Karthik Balaguru >
这篇关于计算数字中设置的位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!