<?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: Binary Fun &#8211; Bits in Bed with Actionscript</title>
	<atom:link href="http://blog.betabong.com/2009/09/22/binary-fun-bits-in-bed-with-actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.betabong.com/2009/09/22/binary-fun-bits-in-bed-with-actionscript/</link>
	<description></description>
	<lastBuildDate>Tue, 20 Apr 2010 20:40:19 +0200</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Andy Stricker</title>
		<link>http://blog.betabong.com/2009/09/22/binary-fun-bits-in-bed-with-actionscript/comment-page-1/#comment-91</link>
		<dc:creator>Andy Stricker</dc:creator>
		<pubDate>Tue, 20 Apr 2010 20:40:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=297#comment-91</guid>
		<description>countBits is called population count or hamming weight in computer science. There is a good chapter about the optimization available for population count in the O&#039;Reilly book &quot;Beautiful Code&quot; in chapter Chapter 10: &quot;The Quest for an Accelerated Population Count&quot; (http://oreilly.com/catalog/9780596510046). I&#039;ll like to see how those improvements apply for high level languages.

Following an optimized function for 32 bit unsigned int population count in Javascript (derived from http://en.wikipedia.org/wiki/Hamming_weight):

&lt;pre lang=&quot;actionscript&quot;&gt;
function pop(x) {
   x &amp;= 0xFFFFFFFF;
   x -= (x &gt;&gt; 1) &amp; 0x55555555;
   x = (x &amp; 0x33333333) + ((x &gt;&gt; 2) &amp; 0x33333333);
   x = (x + (x &gt;&gt; 4)) &amp; 0x0f0f0f0f;
   return (x * 0x01010101) &gt;&gt; 24;
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>countBits is called population count or hamming weight in computer science. There is a good chapter about the optimization available for population count in the O&#8217;Reilly book &#8220;Beautiful Code&#8221; in chapter Chapter 10: &#8220;The Quest for an Accelerated Population Count&#8221; (<a href="http://oreilly.com/catalog/9780596510046)" rel="nofollow">http://oreilly.com/catalog/9780596510046)</a>. I&#8217;ll like to see how those improvements apply for high level languages.</p>
<p>Following an optimized function for 32 bit unsigned int population count in Javascript (derived from <a href="http://en.wikipedia.org/wiki/Hamming_weight)" rel="nofollow">http://en.wikipedia.org/wiki/Hamming_weight)</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">pop</span><span style="color: #66cc66;">&#40;</span>x<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
   x <span style="color: #66cc66;">&amp;</span>= 0xFFFFFFFF;
   x -= <span style="color: #66cc66;">&#40;</span>x <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;</span> 0x55555555;
   x = <span style="color: #66cc66;">&#40;</span>x <span style="color: #66cc66;">&amp;</span> 0x33333333<span style="color: #66cc66;">&#41;</span> + <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>x <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;</span> 0x33333333<span style="color: #66cc66;">&#41;</span>;
   x = <span style="color: #66cc66;">&#40;</span>x + <span style="color: #66cc66;">&#40;</span>x <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&amp;</span> 0x0f0f0f0f;
   <span style="color: #b1b100;">return</span> <span style="color: #66cc66;">&#40;</span>x <span style="color: #66cc66;">*</span> 0x01010101<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;&gt;</span> <span style="color: #cc66cc;">24</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: betabong</title>
		<link>http://blog.betabong.com/2009/09/22/binary-fun-bits-in-bed-with-actionscript/comment-page-1/#comment-73</link>
		<dc:creator>betabong</dc:creator>
		<pubDate>Thu, 01 Oct 2009 07:41:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=297#comment-73</guid>
		<description>Love that!! And it&#039;s about 40% faster too :) Allow me one tiny little correction though:

&lt;pre lang=&quot;Actionscript&quot;&gt;
function is_1_bit(value:uint): Boolean
{
	return value != 0 &amp;&amp; ( value == 0x80000000 &#124;&#124; (value &amp; -value) == value );
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Love that!! And it&#8217;s about 40% faster too :) Allow me one tiny little correction though:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> is_1_bit<span style="color: #66cc66;">&#40;</span>value:uint<span style="color: #66cc66;">&#41;</span>: <span style="color: #0066CC;">Boolean</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> value <span style="color: #66cc66;">!</span>= <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&amp;&amp;</span> <span style="color: #66cc66;">&#40;</span> value == 0x80000000 <span style="color: #66cc66;">||</span> <span style="color: #66cc66;">&#40;</span>value <span style="color: #66cc66;">&amp;</span> -value<span style="color: #66cc66;">&#41;</span> == value <span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: Jackson Dunstan</title>
		<link>http://blog.betabong.com/2009/09/22/binary-fun-bits-in-bed-with-actionscript/comment-page-1/#comment-70</link>
		<dc:creator>Jackson Dunstan</dc:creator>
		<pubDate>Thu, 01 Oct 2009 01:19:53 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=297#comment-70</guid>
		<description>Your function to check if an int has only a single one value is the same as a function that checks if an int is a power of two. You can do so without the loop for extra speed like this:

&lt;pre lang=&quot;Actionscript&quot;&gt;
function is_1_bit(value:uint): Boolean
{
	return value == 0x80000000 &#124;&#124; (value &amp; -value) == value;
}
&lt;/pre&gt;

The formula won&#039;t work for (1&lt;&lt;31), hence the need for the explicit check.</description>
		<content:encoded><![CDATA[<p>Your function to check if an int has only a single one value is the same as a function that checks if an int is a power of two. You can do so without the loop for extra speed like this:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> is_1_bit<span style="color: #66cc66;">&#40;</span>value:uint<span style="color: #66cc66;">&#41;</span>: <span style="color: #0066CC;">Boolean</span>
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> value == 0x80000000 <span style="color: #66cc66;">||</span> <span style="color: #66cc66;">&#40;</span>value <span style="color: #66cc66;">&amp;</span> -value<span style="color: #66cc66;">&#41;</span> == value;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The formula won&#8217;t work for (1&lt;&lt;31), hence the need for the explicit check.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: betabong</title>
		<link>http://blog.betabong.com/2009/09/22/binary-fun-bits-in-bed-with-actionscript/comment-page-1/#comment-68</link>
		<dc:creator>betabong</dc:creator>
		<pubDate>Tue, 22 Sep 2009 16:52:23 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=297#comment-68</guid>
		<description>XOR is deadly easy :-)
&lt;pre lang=&quot;actionscript&quot;&gt;
function xor( v1:uint , v2:uint ) : uint {
	return v1 ^ v2;
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>XOR is deadly easy :-)</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> xor<span style="color: #66cc66;">&#40;</span> v1:uint , v2:uint <span style="color: #66cc66;">&#41;</span> : uint <span style="color: #66cc66;">&#123;</span>
	<span style="color: #b1b100;">return</span> v1 ^ v2;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
	</item>
	<item>
		<title>By: peko</title>
		<link>http://blog.betabong.com/2009/09/22/binary-fun-bits-in-bed-with-actionscript/comment-page-1/#comment-67</link>
		<dc:creator>peko</dc:creator>
		<pubDate>Tue, 22 Sep 2009 14:50:49 +0000</pubDate>
		<guid isPermaLink="false">http://blog.betabong.com/?p=297#comment-67</guid>
		<description>What about binary xor?
AS3 don`t have an default operator :(</description>
		<content:encoded><![CDATA[<p>What about binary xor?<br />
AS3 don`t have an default operator :(</p>
]]></content:encoded>
	</item>
</channel>
</rss>
