{"id":6,"date":"2007-06-05T15:12:09","date_gmt":"2007-06-05T19:12:09","guid":{"rendered":"http:\/\/www.ragestorm.net\/blogs\/?p=6"},"modified":"2007-06-05T15:14:55","modified_gmt":"2007-06-05T19:14:55","slug":"pop-count","status":"publish","type":"post","link":"https:\/\/www.ragestorm.net\/blogs\/?p=6","title":{"rendered":"Pop Count"},"content":{"rendered":"<p>Population Count is counting the number of bits set to one in a word (or any sized integer). I found pop counting as one of the most fascinating small algorithms in the bit twiddling category. There are so many uses for pop counting. Though, rarely, I find myself use them,\u00a0to make stuff compact, like bit-arrays, special bit-flags which represent collections, etc&#8230; There are also popular algorithms which use pop counting in error-correcting codes and sound\/image processing.<\/p>\n<p>Here&#8217;s my favorite implementation:<\/p>\n<p><code> int popcount(unsigned int x)<br \/>\n{<br \/>\n   int c = 0;<br \/>\n   for (; x &gt; 0; x &amp;= x -1) c++;<br \/>\n   return c;<br \/>\n}<\/code><\/p>\n<p>The whole concept is behind the mere formula: x &amp;= x -1. But I will leave it for you to figure it out.<\/p>\n<p>Speaking of pop count, <a target=\"_blank\" href=\"http:\/\/algorithm.co.il\/blogs\">Imri<\/a> and I even used this same implementation above in the <a target=\"_blank\" href=\"http:\/\/blogs.securiteam.com\/index.php\/archives\/378\">NULL Terminated Strip<\/a>. I even hinted for this algorithm. And here&#8217;s a real 32 bits bugfree implementation (that strip has the bugs on purpose):<\/p>\n<p><code>XCHG EBX, EAX<br \/>\nXOR EAX, EAX<br \/>\nOR EBX, EBX<br \/>\nJZ END<br \/>\nA:<br \/>\n   LEA ECX, [EBX-1]<br \/>\n   INC EAX<br \/>\n   AND EBX, ECX<br \/>\n   JNZ A<br \/>\nEND: <\/code><\/p>\n<p>Of course, most implementation are branch-free code, for the sake of speed. However\u00a0&#8211; Lately, I added the SSE4 support to diStorm, and among the new instructions I found out there is a single instruction for it all:\u00a0popcnt. :)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Population Count is counting the number of bits set to one in a word (or any sized integer). I found pop counting as one of the most fascinating small algorithms in the bit twiddling category. There are so many uses for pop counting. Though, rarely, I find myself use them,\u00a0to make stuff compact, like bit-arrays, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":""},"categories":[5,6],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pbWKd-6","_links":{"self":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/6"}],"collection":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6"}],"version-history":[{"count":0,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/6\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}