<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460</link>
    <title>MATLAB Central Newsreader - count number of &quot;on&quot; bits?</title>
    <description>Feed for thread: count number of &quot;on&quot; bits?</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 30 Jul 2008 03:57:12 -0400</pubDate>
      <title>count number of &quot;on&quot; bits?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460#446176</link>
      <author>bogfrog</author>
      <description>How would I do a bitcount in matlab?&lt;br&gt;
&lt;br&gt;
For example, say I have the unsigned 8 bit integer 170, which is '10101010'.  Is there a function to count that it has 4 &quot;on&quot; (1) bits?&lt;br&gt;
&lt;br&gt;
Thanks.</description>
    </item>
    <item>
      <pubDate>Wed, 30 Jul 2008 04:22:01 -0400</pubDate>
      <title>Re: count number of</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460#446177</link>
      <author>matt dash</author>
      <description>d=17465765&lt;br&gt;
[crap,e]=log2(max(d));&lt;br&gt;
n=sum(rem(floor(d*pow2(1-max(1,e):0)),2));</description>
    </item>
    <item>
      <pubDate>Wed, 30 Jul 2008 04:52:57 -0400</pubDate>
      <title>Re: count number of</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460#446179</link>
      <author>bogfrog</author>
      <description>&amp;gt; d=17465765&lt;br&gt;
&amp;gt; [crap,e]=log2(max(d));&lt;br&gt;
&amp;gt; n=sum(rem(floor(d*pow2(1-max(1,e):0)),2));&lt;br&gt;
&lt;br&gt;
How in the world you can come up with that, I'll never know.  But thanks! :)</description>
    </item>
    <item>
      <pubDate>Wed, 30 Jul 2008 04:56:13 -0400</pubDate>
      <title>Re: count number of &quot;on&quot; bits?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460#446181</link>
      <author>Yumnam Kirani Singh</author>
      <description>You can use length and find fucntions to solve your problem.</description>
    </item>
    <item>
      <pubDate>Wed, 30 Jul 2008 06:10:11 -0400</pubDate>
      <title>Re: count number of</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460#446188</link>
      <author>bogfrog</author>
      <description>&amp;gt; d=17465765&lt;br&gt;
&amp;gt; [crap,e]=log2(max(d));&lt;br&gt;
&amp;gt; n=sum(rem(floor(d*pow2(1-max(1,e):0)),2));&lt;br&gt;
&lt;br&gt;
quick question.&lt;br&gt;
&lt;br&gt;
Why the max(d) in the log2 statement?  Isn't max(number) always just the number?&lt;br&gt;
&lt;br&gt;
I took the max(d) out and it still seems to count ok.  Was there a good reason for it?&lt;br&gt;
&lt;br&gt;
Thanks.</description>
    </item>
    <item>
      <pubDate>Wed, 30 Jul 2008 09:05:26 -0400</pubDate>
      <title>Re: count number of &quot;on&quot; bits?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460#446234</link>
      <author>Igor</author>
      <description>On Jul 30, 5:57=A0am, bogfrog &amp;lt;jmcg...@rcn.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; How would I do a bitcount in matlab?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; For example, say I have the unsigned 8 bit integer 170, which is '1010101=&lt;br&gt;
0'. =A0Is there a function to count that it has 4 &quot;on&quot; (1) bits?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Thanks.&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt;help bitget</description>
    </item>
    <item>
      <pubDate>Wed, 30 Jul 2008 09:30:05 -0400</pubDate>
      <title>Re: count number of</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460#446237</link>
      <author>Steve Amphlett</author>
      <description>bogfrog &amp;lt;jmcgraw@rcn.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;3817980.1217390262927.JavaMail.jakarta@nitrogen.mathforum.o&lt;br&gt;
rg&amp;gt;...&lt;br&gt;
&amp;gt; How would I do a bitcount in matlab?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; For example, say I have the unsigned 8 bit integer 170, &lt;br&gt;
which is '10101010'.  Is there a function to count that it &lt;br&gt;
has 4 &quot;on&quot; (1) bits?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks.&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; sum(dec2bin(170)-'0')&lt;br&gt;
&lt;br&gt;
ans =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4</description>
    </item>
    <item>
      <pubDate>Wed, 30 Jul 2008 09:38:02 -0400</pubDate>
      <title>Re: count number of</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460#446239</link>
      <author>us</author>
      <description>bogfrog:&lt;br&gt;
&amp;lt;SNIP counting bits...&lt;br&gt;
&lt;br&gt;
one of the solutions&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;n=170;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;r=sum(bitget(n,1:8));&lt;br&gt;
% r = 4&lt;br&gt;
&lt;br&gt;
us</description>
    </item>
    <item>
      <pubDate>Wed, 30 Jul 2008 14:26:01 -0400</pubDate>
      <title>Re: count number of</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460#446284</link>
      <author>matt dash</author>
      <description>bogfrog &amp;lt;jmcgraw@rcn.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;2156845.1217398241891.JavaMail.jakarta@nitrogen.mathforum.org&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; d=17465765&lt;br&gt;
&amp;gt; &amp;gt; [crap,e]=log2(max(d));&lt;br&gt;
&amp;gt; &amp;gt; n=sum(rem(floor(d*pow2(1-max(1,e):0)),2));&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; quick question.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Why the max(d) in the log2 statement?  Isn't max(number)&lt;br&gt;
always just the number?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I took the max(d) out and it still seems to count ok.  Was&lt;br&gt;
there a good reason for it?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Seems like there's no shortage of solutions here, but for&lt;br&gt;
the record i was lazy and just copied that line out of&lt;br&gt;
dec2bin. I believe the max(d) is there for cases where d is&lt;br&gt;
a vector, so it uses e of the largest d.&lt;br&gt;
&lt;br&gt;
If you're doing this many times in a loop you should try of&lt;br&gt;
the solutions here and see which one is fastest. I had come&lt;br&gt;
up with an alternate method of &lt;br&gt;
&lt;br&gt;
s=sum(dec2bin(d)=='1') but on my computer the method i&lt;br&gt;
posted above is about twice as fast.</description>
    </item>
    <item>
      <pubDate>Wed, 30 Jul 2008 17:34:44 -0400</pubDate>
      <title>Re: count number of</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460#446345</link>
      <author>bogfrog</author>
      <description>&amp;gt; If you're doing this many times in a loop you should&lt;br&gt;
&amp;gt; try of&lt;br&gt;
&amp;gt; the solutions here and see which one is fastest. I&lt;br&gt;
&amp;gt; had come&lt;br&gt;
&amp;gt; up with an alternate method of &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; s=sum(dec2bin(d)=='1') but on my computer the method&lt;br&gt;
&amp;gt; i&lt;br&gt;
&amp;gt; posted above is about twice as fast.&lt;br&gt;
&lt;br&gt;
I ended up writing it in C and using a mex file.  Everything in matlab was just too slow.&lt;br&gt;
&lt;br&gt;
Thanks, though.</description>
    </item>
    <item>
      <pubDate>Wed, 30 Jul 2008 17:50:18 -0400</pubDate>
      <title>Re: count number of</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460#446349</link>
      <author>Steve Amphlett</author>
      <description>bogfrog &amp;lt;jmcgraw@rcn.com&amp;gt; wrote in message &lt;br&gt;
&amp;lt;2748437.1217439315576.JavaMail.jakarta@nitrogen.mathforum.o&lt;br&gt;
rg&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; If you're doing this many times in a loop you should&lt;br&gt;
&amp;gt; &amp;gt; try of&lt;br&gt;
&amp;gt; &amp;gt; the solutions here and see which one is fastest. I&lt;br&gt;
&amp;gt; &amp;gt; had come&lt;br&gt;
&amp;gt; &amp;gt; up with an alternate method of &lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; s=sum(dec2bin(d)=='1') but on my computer the method&lt;br&gt;
&amp;gt; &amp;gt; i&lt;br&gt;
&amp;gt; &amp;gt; posted above is about twice as fast.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I ended up writing it in C and using a mex file.  &lt;br&gt;
Everything in matlab was just too slow.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks, though.&lt;br&gt;
&lt;br&gt;
Gonna share??</description>
    </item>
    <item>
      <pubDate>Wed, 30 Jul 2008 18:36:50 -0400</pubDate>
      <title>Re: count number of</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460#446366</link>
      <author>bogfrog</author>
      <description>&amp;gt; Gonna share??&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Sure.  But it's optimized for a sparse number of 1's.  It's just based on some c-code I found online.  It's still not as fast as I'd _really_ like, but here is the source:&lt;br&gt;
&lt;br&gt;
---------------------------------&lt;br&gt;
#include &quot;mex.h&quot;&lt;br&gt;
&lt;br&gt;
void mexFunction(int nlhs, mxArray *plhs[],&lt;br&gt;
int nrhs, const mxArray *prhs[]) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;unsigned int n, c;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
/* check: only one input and one output argument */&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (nrhs !=1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexErrMsgTxt(&quot;Must have one input argument&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (nlhs !=1)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;mexErrMsgTxt(&quot;Must have one output argument&quot;);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;n = mxGetScalar(prhs[0]);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c = 0;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;while (n)   {&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c++ ;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;n &amp;= (n - 1) ;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;plhs[0]=mxCreateDoubleMatrix(1,1,mxREAL);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*mxGetPr(plhs[0])=c; &lt;br&gt;
}&lt;br&gt;
--------------------------------------&lt;br&gt;
&lt;br&gt;
If you name it bitcount_c.c, just do &quot;mex bitcount_c.c&quot; at the command line and use it as so:&lt;br&gt;
&lt;br&gt;
numbits = bitcount_c(number)</description>
    </item>
    <item>
      <pubDate>Wed, 30 Jul 2008 18:41:37 -0400</pubDate>
      <title>Re: count number of</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460#446370</link>
      <author>bogfrog</author>
      <description>&amp;gt; Gonna share??&lt;br&gt;
&lt;br&gt;
Oh, and if you are interested, here is the link with a bunch of different c code bitcount functions:&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://infolab.stanford.edu/~manku/bitcount/bitcount.c&quot;&gt;http://infolab.stanford.edu/~manku/bitcount/bitcount.c&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
You can modify the matlab .c file I posted with any of those.</description>
    </item>
    <item>
      <pubDate>Sun, 03 Aug 2008 11:32:28 -0400</pubDate>
      <title>Re: count number of</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460#446984</link>
      <author>Andy Robb</author>
      <description>I prefer the following (which I didn't spot):&lt;br&gt;
&lt;br&gt;
int bitcount(unsigned n) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;n = ((n &amp;gt;&amp;gt; 1) &amp; 0x55555555) + (n &amp; 0x55555555);&lt;br&gt;
&amp;nbsp;&amp;nbsp;n = ((n &amp;gt;&amp;gt; 2) &amp; 0x33333333) + (n &amp; 0x33333333);&lt;br&gt;
&amp;nbsp;&amp;nbsp;n = ((n &amp;gt;&amp;gt; 4) &amp; 0x0f0f0f0f) + (n &amp; 0x0f0f0f0f);&lt;br&gt;
&amp;nbsp;&amp;nbsp;n += n &amp;gt;&amp;gt; 8;&lt;br&gt;
&amp;nbsp;&amp;nbsp;n += n &amp;gt;&amp;gt; 16;&lt;br&gt;
&amp;nbsp;&amp;nbsp;return n &amp; 0xff;&lt;br&gt;
}</description>
    </item>
    <item>
      <pubDate>Sun, 14 Sep 2008 19:51:02 -0400</pubDate>
      <title>Re: count number of</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173460#600287</link>
      <author>Andy Robb</author>
      <description>The following times are those to count the bits in all 4 billion 32-bit integers on 64-bit ubuntu linux.&lt;br&gt;
&amp;nbsp;&lt;br&gt;
With a fast multiply, the following can be significantly faster than my previous post (25s v 32s):&lt;br&gt;
&lt;br&gt;
int bitcount(unsigned n) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;n -= (n &amp;gt;&amp;gt; 1) &amp; 0x55555555;&lt;br&gt;
&amp;nbsp;&amp;nbsp;n = ((n &amp;gt;&amp;gt; 2) &amp; 0x33333333) + (n &amp; 0x33333333);&lt;br&gt;
&amp;nbsp;&amp;nbsp;return (((n + (n &amp;gt;&amp;gt; 4)) &amp; 0x0f0f0f0f) * 0x01010101) &amp;gt;&amp;gt; 24;&lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
see page 180 of&lt;br&gt;
&lt;a href=&quot;http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/25112.PDF&quot;&gt;http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/25112.PDF&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
However, if you are counting bits of lots of integers at once, the look-up method can be even faster (once the look-up table is loaded into cache - 19s)&lt;br&gt;
&lt;br&gt;
int count(unsigned v) {&lt;br&gt;
&amp;nbsp;&amp;nbsp;static const int BitsSetTable256[] = &lt;br&gt;
&amp;nbsp;&amp;nbsp;{ 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4&lt;br&gt;
&amp;nbsp;&amp;nbsp;, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5&lt;br&gt;
&amp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, 7, 5, 6, 6, 7, 6, 7, 7, 8&lt;br&gt;
&amp;nbsp;&amp;nbsp;};&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;return BitsSetTable256[v &amp; 0xff] + &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BitsSetTable256[(v &amp;gt;&amp;gt; 8) &amp; 0xff] + &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BitsSetTable256[(v &amp;gt;&amp;gt; 16) &amp; 0xff] + &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;BitsSetTable256[v &amp;gt;&amp;gt; 24]; &lt;br&gt;
}&lt;br&gt;
&lt;br&gt;
This is a modified version of a routine in &lt;a href=&quot;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel&quot;&gt;http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
I changed the table from (unsigned char) to (int) as it is faster to add directly from the table than to convert unsigned char to int before adding.&lt;br&gt;
&lt;br&gt;
If the routine is inlined, the time drops to 15s. With a bit of assembler hacking, the time can drop below 12s.</description>
    </item>
  </channel>
</rss>

