<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Interview Questions: Counting Bits</title>
	<atom:link href="http://20bits.com/articles/interview-questions-counting-bits/feed/" rel="self" type="application/rss+xml" />
	<link>http://20bits.com/articles/interview-questions-counting-bits/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=interview-questions-counting-bits</link>
	<description>Driven by Data</description>
	<lastBuildDate>Sat, 09 Jul 2011 18:30:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
	<item>
		<title>By: Anonymous</title>
		<link>http://20bits.com/articles/interview-questions-counting-bits/comment-page-1/#comment-5570</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 18 May 2011 01:34:00 +0000</pubDate>
		<guid isPermaLink="false">http://20bits.com/?p=108#comment-5570</guid>
		<description> Atpresent. on get older. goodness me form welcomes be only a little more low-key. I personally start to wearsophisticated basic color during. on some courtly pandora metallic bracelets .
From time to time. I’d would rather &lt;a href=&quot;http://www.pandoraukshop.com/pandora-charms.html&quot; rel=&quot;nofollow&quot;&gt;&lt;b&gt;pandora charms sale&lt;/b&gt;&lt;/a&gt;purchase an enormous red-colored hand-bag that i won`teconomic utilization most probably. as well as toss directly towards water closetbecause various years. purchase a manufacturer new neutral-colored ladies handbag &lt;a href=&quot;http://www.pandoraukshop.com/pandora-bracelets.html&quot; rel=&quot;nofollow&quot;&gt;&lt;b&gt;pandora bracelets on sale&lt;/b&gt;&lt;/a&gt;possessing pandora silver valuable metal charms beat would appear more from significance while have the ability&lt;a href=&quot;http://www.pandoraukshop.com/pandora-bracelets.html&quot; rel=&quot;nofollow&quot;&gt;&lt;b&gt;pandora bracelets outlet&lt;/b&gt;&lt;/a&gt; to pat it it does not create a difference throughout the evening or possibly overnight.</description>
		<content:encoded><![CDATA[<p> Atpresent. on get older. goodness me form welcomes be only a little more low-key. I personally start to wearsophisticated basic color during. on some courtly pandora metallic bracelets .<br />
From time to time. I’d would rather <a href="http://www.pandoraukshop.com/pandora-charms.html" rel="nofollow"><b>pandora charms sale</b></a>purchase an enormous red-colored hand-bag that i won`teconomic utilization most probably. as well as toss directly towards water closetbecause various years. purchase a manufacturer new neutral-colored ladies handbag <a href="http://www.pandoraukshop.com/pandora-bracelets.html" rel="nofollow"><b>pandora bracelets on sale</b></a>possessing pandora silver valuable metal charms beat would appear more from significance while have the ability<a href="http://www.pandoraukshop.com/pandora-bracelets.html" rel="nofollow"><b>pandora bracelets outlet</b></a> to pat it it does not create a difference throughout the evening or possibly overnight.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ScottM</title>
		<link>http://20bits.com/articles/interview-questions-counting-bits/comment-page-1/#comment-5384</link>
		<dc:creator>ScottM</dc:creator>
		<pubDate>Thu, 17 Mar 2011 15:51:17 +0000</pubDate>
		<guid isPermaLink="false">http://20bits.com/?p=108#comment-5384</guid>
		<description>I found this article after implementing a non-looping bit counter and I was googling to see if it&#039;s the best algorithm more out of curiosity than anything, since I figured it was a standard thing.&lt;br&gt;&lt;br&gt;Basically I converted the problem to the lowest level with a consistent pattern, which happen if you work on the problem in nibbles (4-bits).  Then breaking that down into quadrant and the 2 least significant bits, their counts follow the patten 0, 1, 1, 2 respectively, which can be treated as the if case &quot;return (v == 0) ? 0 : v == 2 ? 2 : 1&quot; which is quick.&lt;br&gt;&lt;br&gt;Then these &quot;quadrants&quot; which are really the 2 most significant bits, can be added separately through a similar set of if statements: &quot;return (v &lt; 4) ? 0 : v &gt;= 12 ? 2 : 1&quot;  which is actually the same pattern as the lower bits, but skipping the bit shifting by comparing different numbers.&lt;br&gt;&lt;br&gt;I think it&#039;s clear where to go from here... I just break out each nibble with another overloaded method of each bit level... 8bits, 16, 32, and 64 and I get the value I need for all types.&lt;br&gt;&lt;br&gt;So each nibble is taking 5 ifs, 1 and, 2 adds... which are all likely to be single opcode operations.  If we can optimize the nibble code further, then the rest of the algorithm with also be improved.&lt;br&gt;&lt;br&gt;So the 32 bit case is Count(v &amp; 0x0000FFFF) + Count(v &amp; 0xFFFF0000 &gt;&gt; 16) for example, so the 32 bit case calling each of the methods, you end up with 8 nibble calls, 4 char call, 2 short calls for that one int32 call (I&#039;m using c#) so I&#039;d bet the method calls are the thing taking the most time, which could also be rolled out for better performance, like the good old Apple II programming days :)&lt;br&gt;&lt;br&gt;So my algorithm is definitely O(1) since I don&#039;t loop, and since splitting the problem to each lower bit-level takes 2 ANDs, 1 SHIFT-RIGHT, and 1 ADD, I&#039;d venture to say the total number of opcodes if we don&#039;t consider the call stack and method calls would be 4 operations per split, and we&#039;re splitting 7 times and running 8 nibbles... &lt;br&gt;&lt;br&gt;--- 7*4 + 8*8 = 64 + 28 = 92&lt;br&gt;I think I&#039;m looking at a fixed set of 92 operations to achieve this.  This definitely takes less RAM than having an array of 4 billion elements.  Perhaps a lookup table on the nibbles or byte level would hit a better memory/cpu sweet spot.&lt;br&gt;&lt;br&gt;Also seems like these 92 very simple opcodes would be something the processor could pipeline fairly well and perhaps become one of those rare instances where you can get more than 1 opcode per cycle on average.  I don&#039;t know if this is really much more efficient, but I bet there&#039;s a way to implement this line of reasoning on super long arrays that could be computed using scalar/SSE instructions.&lt;br&gt;&lt;br&gt;</description>
		<content:encoded><![CDATA[<p>I found this article after implementing a non-looping bit counter and I was googling to see if it&#39;s the best algorithm more out of curiosity than anything, since I figured it was a standard thing.</p>
<p>Basically I converted the problem to the lowest level with a consistent pattern, which happen if you work on the problem in nibbles (4-bits).  Then breaking that down into quadrant and the 2 least significant bits, their counts follow the patten 0, 1, 1, 2 respectively, which can be treated as the if case &#8220;return (v == 0) ? 0 : v == 2 ? 2 : 1&#8243; which is quick.</p>
<p>Then these &#8220;quadrants&#8221; which are really the 2 most significant bits, can be added separately through a similar set of if statements: &#8220;return (v &lt; 4) ? 0 : v &gt;= 12 ? 2 : 1&#8243;  which is actually the same pattern as the lower bits, but skipping the bit shifting by comparing different numbers.</p>
<p>I think it&#39;s clear where to go from here&#8230; I just break out each nibble with another overloaded method of each bit level&#8230; 8bits, 16, 32, and 64 and I get the value I need for all types.</p>
<p>So each nibble is taking 5 ifs, 1 and, 2 adds&#8230; which are all likely to be single opcode operations.  If we can optimize the nibble code further, then the rest of the algorithm with also be improved.</p>
<p>So the 32 bit case is Count(v &amp; 0x0000FFFF) + Count(v &amp; 0xFFFF0000 &gt;&gt; 16) for example, so the 32 bit case calling each of the methods, you end up with 8 nibble calls, 4 char call, 2 short calls for that one int32 call (I&#39;m using c#) so I&#39;d bet the method calls are the thing taking the most time, which could also be rolled out for better performance, like the good old Apple II programming days :)</p>
<p>So my algorithm is definitely O(1) since I don&#39;t loop, and since splitting the problem to each lower bit-level takes 2 ANDs, 1 SHIFT-RIGHT, and 1 ADD, I&#39;d venture to say the total number of opcodes if we don&#39;t consider the call stack and method calls would be 4 operations per split, and we&#39;re splitting 7 times and running 8 nibbles&#8230; </p>
<p>&#8212; 7*4 + 8*8 = 64 + 28 = 92<br />I think I&#39;m looking at a fixed set of 92 operations to achieve this.  This definitely takes less RAM than having an array of 4 billion elements.  Perhaps a lookup table on the nibbles or byte level would hit a better memory/cpu sweet spot.</p>
<p>Also seems like these 92 very simple opcodes would be something the processor could pipeline fairly well and perhaps become one of those rare instances where you can get more than 1 opcode per cycle on average.  I don&#39;t know if this is really much more efficient, but I bet there&#39;s a way to implement this line of reasoning on super long arrays that could be computed using scalar/SSE instructions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sven.hedin</title>
		<link>http://20bits.com/articles/interview-questions-counting-bits/comment-page-1/#comment-5072</link>
		<dc:creator>sven.hedin</dc:creator>
		<pubDate>Mon, 28 Jun 2010 15:34:21 +0000</pubDate>
		<guid isPermaLink="false">http://20bits.com/?p=108#comment-5072</guid>
		<description>In all codes above you used 0x1u, why not only 1 instead? I think both do the same thing.</description>
		<content:encoded><![CDATA[<p>In all codes above you used 0x1u, why not only 1 instead? I think both do the same thing.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hrish</title>
		<link>http://20bits.com/articles/interview-questions-counting-bits/comment-page-1/#comment-5045</link>
		<dc:creator>Hrish</dc:creator>
		<pubDate>Wed, 16 Jun 2010 08:06:03 +0000</pubDate>
		<guid isPermaLink="false">http://20bits.com/?p=108#comment-5045</guid>
		<description>Just noticed that the link &lt;a href=&quot;http://20bits.com/tag/interview/&quot; rel=&quot;nofollow&quot;&gt;http://20bits.com/tag/interview/&lt;/a&gt; at the beginning of your article is broken.</description>
		<content:encoded><![CDATA[<p>Just noticed that the link <a href="http://20bits.com/tag/interview/" rel="nofollow">http://20bits.com/tag/interview/</a> at the beginning of your article is broken.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gournew</title>
		<link>http://20bits.com/articles/interview-questions-counting-bits/comment-page-1/#comment-4946</link>
		<dc:creator>gournew</dc:creator>
		<pubDate>Thu, 08 Apr 2010 23:36:10 +0000</pubDate>
		<guid isPermaLink="false">http://20bits.com/?p=108#comment-4946</guid>
		<description>Now in this case we can reduce the number of iteration..&lt;br&gt;as for example if input is 64 then the binary will be 100000&lt;br&gt;so if normal procedure used then we have to itrate 6v times in the loop&lt;br&gt;&lt;br&gt;simple steps&lt;br&gt;int count_bit(int num)&lt;br&gt;{&lt;br&gt;    count =0;&lt;br&gt;    while(num)&lt;br&gt;    {&lt;br&gt;          num=(num) &amp; (num-1);&lt;br&gt;          count++;&lt;br&gt;      }&lt;br&gt;return count;&lt;br&gt;// complete program will be  available in&lt;br&gt;//  &lt;a href=&quot;http://goursaha.freeoda.com&quot; rel=&quot;nofollow&quot;&gt;http://goursaha.freeoda.com&lt;/a&gt;&lt;br&gt;}</description>
		<content:encoded><![CDATA[<p>Now in this case we can reduce the number of iteration..<br />as for example if input is 64 then the binary will be 100000<br />so if normal procedure used then we have to itrate 6v times in the loop</p>
<p>simple steps<br />int count_bit(int num)<br />{<br />    count =0;<br />    while(num)<br />    {<br />          num=(num) &#038; (num-1);<br />          count++;<br />      }<br />return count;<br />// complete program will be  available in<br />//  <a href="http://goursaha.freeoda.com" rel="nofollow">http://goursaha.freeoda.com</a><br />}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: axvpast</title>
		<link>http://20bits.com/articles/interview-questions-counting-bits/comment-page-1/#comment-4862</link>
		<dc:creator>axvpast</dc:creator>
		<pubDate>Tue, 23 Feb 2010 03:27:57 +0000</pubDate>
		<guid isPermaLink="false">http://20bits.com/?p=108#comment-4862</guid>
		<description>it is not so good answers all, because best way to solve tasks like:&lt;br&gt; We choose the routing destination based on the number of on bits in the binary representation of the routing number.&lt;br&gt;&lt;br&gt;Using VHDL :) in hardware level (like CRC32 computation). All can be done in one instruction like it do real routers :)</description>
		<content:encoded><![CDATA[<p>it is not so good answers all, because best way to solve tasks like:<br /> We choose the routing destination based on the number of on bits in the binary representation of the routing number.</p>
<p>Using VHDL :) in hardware level (like CRC32 computation). All can be done in one instruction like it do real routers :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mohdhussain</title>
		<link>http://20bits.com/articles/interview-questions-counting-bits/comment-page-1/#comment-4833</link>
		<dc:creator>mohdhussain</dc:creator>
		<pubDate>Wed, 03 Feb 2010 03:47:20 +0000</pubDate>
		<guid isPermaLink="false">http://20bits.com/?p=108#comment-4833</guid>
		<description>bits of accounts questions answers</description>
		<content:encoded><![CDATA[<p>bits of accounts questions answers</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reuben</title>
		<link>http://20bits.com/articles/interview-questions-counting-bits/comment-page-1/#comment-2492</link>
		<dc:creator>Reuben</dc:creator>
		<pubDate>Thu, 08 May 2008 00:16:13 +0000</pubDate>
		<guid isPermaLink="false">http://20bits.com/?p=108#comment-2492</guid>
		<description>I&#039;m surprised the bit counting in parallel code snippet wasn&#039;t included in this post too -- I thought it was a classic.  Here&#039;s a link to the algorithm (in response to roi):

http://www.cs.utk.edu/~vose/c-stuff/bithacks.html#CountBitsSetParallel

This version doesn&#039;t use a for loop so that you can clearly see the c*log_2{n} == c*5 steps (for n=32).  Easily changed to accomodate counting bits set for 64 bit integers of course.

Another interesting solution, which is faster than the naive solution only for integers where the bits set are few, is here:

http://www.cs.utk.edu/~vose/c-stuff/bithacks.html#CountBitsSetKernighan</description>
		<content:encoded><![CDATA[<p>I&#8217;m surprised the bit counting in parallel code snippet wasn&#8217;t included in this post too &#8212; I thought it was a classic.  Here&#8217;s a link to the algorithm (in response to roi):</p>
<p><a href="http://www.cs.utk.edu/~vose/c-stuff/bithacks.html#CountBitsSetParallel" rel="nofollow">http://www.cs.utk.edu/~vose/c-stuff/bithacks.html#CountBitsSetParallel</a></p>
<p>This version doesn&#8217;t use a for loop so that you can clearly see the c*log_2{n} == c*5 steps (for n=32).  Easily changed to accomodate counting bits set for 64 bit integers of course.</p>
<p>Another interesting solution, which is faster than the naive solution only for integers where the bits set are few, is here:</p>
<p><a href="http://www.cs.utk.edu/~vose/c-stuff/bithacks.html#CountBitsSetKernighan" rel="nofollow">http://www.cs.utk.edu/~vose/c-stuff/bithacks.html#CountBitsSetKernighan</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: roi</title>
		<link>http://20bits.com/articles/interview-questions-counting-bits/comment-page-1/#comment-2012</link>
		<dc:creator>roi</dc:creator>
		<pubDate>Wed, 30 Apr 2008 20:30:55 +0000</pubDate>
		<guid isPermaLink="false">http://20bits.com/?p=108#comment-2012</guid>
		<description>I see that Wordpress didn&#039;t like it. Another attempt at the punchline:

&lt;pre&gt;
for ( i = 0; (1&lt;&lt;i) &lt; 32; i++) n = ((n &amp; ~masks[i]) &gt; &gt; (1&lt;&lt;i)) + (n &amp; masks[i]);
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I see that WordPress didn&#8217;t like it. Another attempt at the punchline:</p>
<pre>
for ( i = 0; (1&lt;&lt;i) &lt; 32; i++) n = ((n &amp; ~masks[i]) &gt; &gt; (1&lt;&lt;i)) + (n &amp; masks[i]);
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: roi</title>
		<link>http://20bits.com/articles/interview-questions-counting-bits/comment-page-1/#comment-2009</link>
		<dc:creator>roi</dc:creator>
		<pubDate>Wed, 30 Apr 2008 20:19:02 +0000</pubDate>
		<guid isPermaLink="false">http://20bits.com/?p=108#comment-2009</guid>
		<description>There&#039;s a cuter, log n algorithm:

&lt;pre&gt;
int bitcount(unsigned int n) {
    unsigned int i, masks[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF};
    for ( i = 0; (1&lt;&lt;i) &gt; (1&lt;&lt;i)) + (n &amp; masks[i]);
    return n;
}
&lt;/pre&gt;
(I hope it survives wordpress&#039; munging)

Creating the bitmasks programatically, to make it independent of int size, is left as a trivial exercise for the reader.</description>
		<content:encoded><![CDATA[<p>There&#8217;s a cuter, log n algorithm:</p>
<pre>
int bitcount(unsigned int n) {
    unsigned int i, masks[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF};
    for ( i = 0; (1&lt;&lt;i) &gt; (1&lt;&lt;i)) + (n &amp; masks[i]);
    return n;
}
</pre>
<p>(I hope it survives wordpress&#8217; munging)</p>
<p>Creating the bitmasks programatically, to make it independent of int size, is left as a trivial exercise for the reader.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

