如何计算unsigned int中的位? [英] How to count bits in a unsigned int?

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

问题描述

在32位平台上,

我正在努力在没有if循环的情况下获得等于1的位数。


-

问候。

Terrence Feldman

电子邮件: tf ********@gmail.com

On 32-bit platform,
I am working on getting how many bits equal to 1 without an if loop.

--
Regards.
Terrence Feldman
Email: tf********@gmail.com

推荐答案

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

On 32 -bit平台,

我正在努力在没有if循环的情况下获得多少位等于1。
On 32-bit platform,
I am working on getting how many bits equal to 1 without an if loop.



使用for循环或while循环,测试每个位,直到你没有要测试的
多个位。我不确定if循环是什么样的,除非

你正在使用goto。

Use a for loop or a while loop, testing each bit until you have no
more bits to test. I''m not sure what an if loop would be like, unless
you''re using goto.


< a href =mailto:tf ******** @ gmail.com> tf ******** @ gmail.com 写道:

在32位平台上,

我正在努力在没有if循环的情况下获得多少位等于1。
On 32-bit platform,
I am working on getting how many bits equal to 1 without an if loop.



由于没有

" if loop"这样你的任务变得更容易了,所以你避免它不会有任何麻烦。


解决任何问题最重要的一步是确切地定义问题是什么。你还没有这样做。


但是形式问题如何不使用Y功能我怎么办?

几乎总是如此家庭作业。我们不会为你做好你的作业(除非你愿意支付我们过高的咨询费用

*和*让我们直接将你的解决方案提交给你教师)。


我们非常愿意帮助您解决C代码中的任何问题,但是除非你,否则我们不能这样做写一些并发布。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *< http://用户。 sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

Your task is made easier by the fact that there''s no such thing as an
"if loop", so you won''t have any trouble avoiding it.

The most important step in solving any problem is defining exactly
what the problem is. You haven''t done that.

But questions of the form "How can I do X without using feature Y?"
are almost always homework assignments. We won''t do your homework for
you (unless you''re willing to pay our exhorbitant consulting fees
*and* let us submit our solutions directly to your instructor).

We''re quite willing to help you solve any problems in your C code, but
we can''t do that unless you write some and post it.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.




< tf ******** @ gmail.comwrote in message n

<tf********@gmail.comwrote in message n

在32位平台上,

我正在努力获得在没有if循环的情况下等于1的位数。
On 32-bit platform,
I am working on getting how many bits equal to 1 without an if loop.



最低位可以等于1。剩下的等于2,4,8 ......等等。

所以如果(x& 1)是你的答案。


Tell你的教授他的意思是设定位数。

On the lowest bit can equal one. The rest equal 2, 4, 8 ... etc.

So if (x & 1) is your answer.

Tell your professor he means the number of set bits.


这篇关于如何计算unsigned int中的位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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