﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-雨来了，风知道</title><link>http://www.cnitblog.com/wuzi/</link><description>、、、、、、、、、◎◎◎◎◎◎◎◎◎
、、、、、、、、、◎◎◎◎◎◎◎◎◎
、、、、、、、、、◎◎◎◎◎◎◎◎◎


</description><language>zh-cn</language><lastBuildDate>Tue, 05 May 2026 00:03:18 GMT</lastBuildDate><pubDate>Tue, 05 May 2026 00:03:18 GMT</pubDate><ttl>60</ttl><item><title>c语言---一个数x中值为1的位数(3个版本)</title><link>http://www.cnitblog.com/wuzi/archive/2008/10/05/49851.html</link><dc:creator>五子</dc:creator><author>五子</author><pubDate>Sun, 05 Oct 2008 02:28:00 GMT</pubDate><guid>http://www.cnitblog.com/wuzi/archive/2008/10/05/49851.html</guid><wfw:comment>http://www.cnitblog.com/wuzi/comments/49851.html</wfw:comment><comments>http://www.cnitblog.com/wuzi/archive/2008/10/05/49851.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/wuzi/comments/commentRss/49851.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/wuzi/services/trackbacks/49851.html</trackback:ping><description><![CDATA[<p><span style="FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman"><img height=20 src="http://www.cnitblog.com/Emoticons/QQ/01.gif" width=20 border=0>/*版一*/<br>unsigned int bitcount(unsigned int x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int b;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for(b=0;x != 0;x &gt;&gt;= 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(x&amp;01)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b++;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return b;<br>}</span></p>
<br><span style="FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman"><img height=20 src="http://www.cnitblog.com/Emoticons/QQ/24.gif" width=20 border=0>/*版二*/<br>unsigned int bitcount(unsigned int x)<br>{<br>&nbsp;int b;<br>&nbsp;for(b=0;x != 0;)<br>&nbsp;{<br>&nbsp;&nbsp;x &amp;= (x-1);&nbsp;&nbsp;<span style="FONT-SIZE: 10pt">&nbsp;&nbsp;/* 这里的做用是每次都会在原的的数的基础上去掉一个1，贼酷 */</span><br>&nbsp;&nbsp;b++;<br>&nbsp;}<br>&nbsp;return b;<br>}<br></span><br><span style="FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman"><img height=20 src="http://www.cnitblog.com/Emoticons/QQ/icon18.gif" width=25 border=0>/*版三*/<br>unsigned&nbsp;&nbsp; int bitcount(register&nbsp;&nbsp; unsigned&nbsp;&nbsp; int&nbsp;&nbsp; x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x&nbsp;&nbsp; -=&nbsp;&nbsp; ((x&nbsp;&nbsp; &gt; &gt;&nbsp;&nbsp; 1)&nbsp;&nbsp; &amp;&nbsp;&nbsp; 0x55555555);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x&nbsp;&nbsp; =&nbsp;&nbsp;&nbsp; (((x&nbsp;&nbsp; &gt; &gt;&nbsp;&nbsp; 2)&nbsp;&nbsp; &amp;&nbsp;&nbsp; 0x33333333)&nbsp;&nbsp; +&nbsp;&nbsp; (x&nbsp;&nbsp; &amp;&nbsp;&nbsp; 0x33333333));<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;x&nbsp;&nbsp; =&nbsp;&nbsp;&nbsp; (((x&nbsp;&nbsp; &gt; &gt;&nbsp;&nbsp; 4)&nbsp;&nbsp; +&nbsp;&nbsp; x)&nbsp;&nbsp; &amp;&nbsp;&nbsp; 0x0f0f0f0f);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x&nbsp;&nbsp; +=&nbsp;&nbsp; (x&nbsp;&nbsp; &gt; &gt;&nbsp;&nbsp; 8);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x&nbsp;&nbsp; +=&nbsp;&nbsp; (x&nbsp;&nbsp; &gt; &gt;&nbsp;&nbsp; 16);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(x&nbsp;&nbsp; &amp;&nbsp;&nbsp; 0x0000003f);<br>}<br></span><br><span style="FONT-SIZE: 12pt; FONT-FAMILY: Times New Roman"><img height=20 src="http://www.cnitblog.com/Emoticons/QQ/shifty.gif" width=20 border=0>/*第3个版本的清晰版本*/<br>unsigned&nbsp;&nbsp; int bitcount(register&nbsp;&nbsp; unsigned&nbsp;&nbsp; int&nbsp;&nbsp; x)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = (x &amp; 0x55555555) + ((x &gt;&gt; 1) &amp; 0x55555555);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; x = (x &amp; 0x33333333) + ((x &gt;&gt; 2) &amp; 0x33333333);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = (x &amp; 0x0F0F0F0F) + ((x &gt;&gt; 4) &amp; 0x0F0F0F0F);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = (x &amp; 0x00FF00FF) + ((x &gt;&gt; 8) &amp; 0x00FF00FF);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = (x &amp; 0x0000FFFF) + ((x &gt;&gt;16) &amp; 0x0000FFFF);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(x&nbsp;&nbsp; &amp;&nbsp;&nbsp; 0x0000003f);<br>}<br><span style="FONT-SIZE: 10pt; FONT-FAMILY: Times New Roman">//进一步解释：看图吧，已经山穷水尽啦，如果还不明白请打120<br></span></span><span style="FONT-FAMILY: Arial">&nbsp;</span><br>
<pre>&nbsp; <img height=352 alt="" src="http://www.cnitblog.com/images/cnitblog_com/wuzi/bitcount.jpg" width=498 border=0></pre>
<pre><span style="FONT-FAMILY: Times New Roman"><br><br><br><br><br><br><br>/*其实这个算法很简单，就是把这个数中的所有的1都加起来就可以了*/<br><img height=20 src="http://www.cnitblog.com/Emoticons/QQ/14.gif" width=20 border=0><img height=20 src="http://www.cnitblog.com/Emoticons/QQ/14.gif" width=20 border=0><img height=20 src="http://www.cnitblog.com/Emoticons/QQ/14.gif" width=20 border=0><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></pre>
<p><font face="Times New Roman"></font>&nbsp;</p>
<p>&nbsp;</p>
<pre><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></span><br></pre>
<img src ="http://www.cnitblog.com/wuzi/aggbug/49851.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/wuzi/" target="_blank">五子</a> 2008-10-05 10:28 <a href="http://www.cnitblog.com/wuzi/archive/2008/10/05/49851.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>