<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Anselm Bradford &#187; Programming</title>
	<atom:link href="http://blog.anselmbradford.com/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.anselmbradford.com</link>
	<description>wrangling the Internet's wildest</description>
	<lastBuildDate>Sun, 18 Jul 2010 08:55:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>2 invaluable Drupal development tips: list all available variables and backtrace a page</title>
		<link>http://blog.anselmbradford.com/2009/03/14/2-invaluable-drupal-development-tips-list-all-available-variables-and-backtrace-a-page/</link>
		<comments>http://blog.anselmbradford.com/2009/03/14/2-invaluable-drupal-development-tips-list-all-available-variables-and-backtrace-a-page/#comments</comments>
		<pubDate>Sun, 15 Mar 2009 03:59:49 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[Drupal]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=702</guid>
		<description><![CDATA[The Drupal Devel module includes some invaluable functions that make working with Drupal much much easier. A short list of these functions can be found at this post. One of these is the dpm() command (which I would guess stands for &#8220;Drupal Print Message,&#8221; or at least that&#8217;s how I remember it). Given an array [...]]]></description>
			<content:encoded><![CDATA[<p>The Drupal <a href="http://drupal.org/project/devel" onclick="pageTracker._trackPageview('/outgoing/drupal.org/project/devel?referer=');">Devel</a> module includes some invaluable functions that make working with Drupal much much easier. A short list of these functions can be found <a href="http://www.thingy-ma-jig.co.uk/blog/02-10-2007/hugely-useful-hugely-undocumented" onclick="pageTracker._trackPageview('/outgoing/www.thingy-ma-jig.co.uk/blog/02-10-2007/hugely-useful-hugely-undocumented?referer=');">at this post</a>. One of these is the <code>dpm()</code> command (which I would guess stands for &#8220;Drupal Print Message,&#8221; or at least that&#8217;s how I remember it). Given an array or object, this function will output a div structure at the top of your page that you can use to visually walk through the contents of either of these sets of data. Combine this with existing PHP debugging and instrospection commands and you have a very useful and powerful development tool at your disposal. For example, place this at the top of your Drupal theme&#8217;s <code>page.tpl.php</code> template:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>?php
	 dpm<span style="color: #66cc66;">&#40;</span> get_defined_vars<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
?<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p>Now when you navigate to your page you will have a clickable bar at the top that will list all variables available to the page when it loads, which you can access via the code in your theme page template if you want. </p>
<p><img src="http://blog.anselmbradford.com/wp-content/uploads/2009/03/dpm_get_defined_vars.png" alt="dpm_get_defined_vars" title="dpm_get_defined_vars" width="353" height="348" class="aligncenter size-full wp-image-703" /></p>
<p>Or maybe you would like to know what path your page took through Drupal&#8217;s architecture to its final incarnation, use <code>dpm()</code> with PHP&#8217;s <code>debug_backtrace()</code> function. This will output an array of each function your page contents went through before they were output to the browser, plus it shows the location of these functions. Try this in place of the code snippet above:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #66cc66;">&lt;</span>?php
	 dpm<span style="color: #66cc66;">&#40;</span> debug_backtrace<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;
?<span style="color: #66cc66;">&gt;</span></pre></div></div>

<p><img src="http://blog.anselmbradford.com/wp-content/uploads/2009/03/dpm_debug_backtrace.png" alt="dpm_debug_backtrace" title="dpm_debug_backtrace" width="531" height="374" class="aligncenter size-full wp-image-704" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2009/03/14/2-invaluable-drupal-development-tips-list-all-available-variables-and-backtrace-a-page/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Cairngorm versus PureMVC, the most basic example application!</title>
		<link>http://blog.anselmbradford.com/2008/12/05/cairngorm-versus-puremvc-the-most-basic-example/</link>
		<comments>http://blog.anselmbradford.com/2008/12/05/cairngorm-versus-puremvc-the-most-basic-example/#comments</comments>
		<pubDate>Sat, 06 Dec 2008 04:55:35 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=222</guid>
		<description><![CDATA[Awhile back when I was learning to use the Cairngorm framework, Nicolas Lierman had a very basic example of a Cairngorm application. At the time I was trying to slog through the Cairngorm Store example, racking my brains trying to figure out what was going on. I didn&#8217;t need such a complex example to start [...]]]></description>
			<content:encoded><![CDATA[<p>Awhile back when I was learning to use the <a href="http://opensource.adobe.com/wiki/display/cairngorm/Cairngorm" onclick="pageTracker._trackPageview('/outgoing/opensource.adobe.com/wiki/display/cairngorm/Cairngorm?referer=');">Cairngorm framework</a>, <a href="http://www.aboutnico.be" onclick="pageTracker._trackPageview('/outgoing/www.aboutnico.be?referer=');">Nicolas Lierman</a> had a very basic example of a Cairngorm application. At the time I was trying to slog through the Cairngorm Store example, racking my brains trying to figure out what was going on. I didn&#8217;t need such a complex example to start from, just something extremely simple that would compile, and I could build from there. Looking at Nicolas&#8217; example application was an &#8216;ah ha&#8217; moment when it all came together in my mind. Now I have been learning the <a href="http://puremvc.org" onclick="pageTracker._trackPageview('/outgoing/puremvc.org?referer=');">PureMVC framework</a> and would like to resurrect a slightly modified version of that Cairngorm example (Nicolas&#8217; used WebService, this uses HTTPService) side-by-side with the same example written in PureMVC. If you are trying to learn either of these frameworks, reference the <a href="http://www.cairngormdocs.org/cairngormDiagram/index.html" onclick="pageTracker._trackPageview('/outgoing/www.cairngormdocs.org/cairngormDiagram/index.html?referer=');">Cairngorm Explorer</a> and/or the <a href="http://puremvc.org/component/option,com_wrapper/Itemid,34/" onclick="pageTracker._trackPageview('/outgoing/puremvc.org/component/option_com_wrapper/Itemid_34/?referer=');">PureMVC Conceptual Diagram</a> and build on these examples. I hope they will help you as much as they helped me to learn the framework&#8217;s architecture.</p>
<p>Below is the example application written using the Cairngorm framework. Since the PureMVC example looks and functions exactly the same I will omit it. As you can see it is very simple. Type in your name and it sends it to the server and returns a message.</p>
<p><span style="color: #551a8b; text-decoration: underline;">
<object width="450" height="210">
<param name="movie" value="/wp-content/uploads/2008/12/cairngormexample.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="window"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#FFFFFF"></param>
<embed type="application/x-shockwave-flash" width="450" height="210" src="/wp-content/uploads/2008/12/cairngormexample.swf" quality="high" bgcolor="#FFFFFF" wmode="window" menu="false" ></embed>
</object>
<br />
</span></p>
<p>The php backend is extremely simple:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Hello &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$_REQUEST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;!&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><a href='http://blog.anselmbradford.com/wp-content/uploads/2008/12/as3_framework_examples.zip'>Download Source Here</a> (You will also need to download <a href="http://opensource.adobe.com/wiki/display/cairngorm/Downloads" onclick="pageTracker._trackPageview('/outgoing/opensource.adobe.com/wiki/display/cairngorm/Downloads?referer=');">Cairngorm</a> and <a href="http://trac.puremvc.org/PureMVC_AS3/" onclick="pageTracker._trackPageview('/outgoing/trac.puremvc.org/PureMVC_AS3/?referer=');">PureMVC</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2008/12/05/cairngorm-versus-puremvc-the-most-basic-example/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Coraldata merged into BirdEye</title>
		<link>http://blog.anselmbradford.com/2008/10/08/coraldata-merged-into-birdeye/</link>
		<comments>http://blog.anselmbradford.com/2008/10/08/coraldata-merged-into-birdeye/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 05:18:45 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[News]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=110</guid>
		<description><![CDATA[Thanks to the interest of Jason Bellone at the United Nations, Centre for Advanced Visual Analytics, I am happy to announce that my data structure library, coraldata, has a new home in the BirdEye Information Visualization and Visual Analytics Library. It may take a little time to get all the classes up there, but it [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.anselmbradford.com/wp-content/uploads/2008/10/cdmergebirdeye.png" alt="" title="cdmergebirdeye" width="372" height="178" class="aligncenter size-full wp-image-109" /><br />
Thanks to the interest of Jason Bellone at the <a href="http://cava.unog.ch" onclick="pageTracker._trackPageview('/outgoing/cava.unog.ch?referer=');">United Nations, Centre for Advanced Visual Analytics</a>, I am happy to announce that my data structure library, coraldata, has a new home in the <a href="http://code.google.com/p/birdeye/" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/birdeye/?referer=');">BirdEye Information Visualization and Visual Analytics Library</a>. It may take a little time to get all the classes up there, but it resides in the General Utilities package, called <a href="http://code.google.com/p/birdeye/wiki/GuVis" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/birdeye/wiki/GuVis?referer=');">GuVis</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2008/10/08/coraldata-merged-into-birdeye/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Recursively convert XML into multidimensional array (AS2)</title>
		<link>http://blog.anselmbradford.com/2008/09/04/recursively-convert-xml-into-multidimensional-array-as2/</link>
		<comments>http://blog.anselmbradford.com/2008/09/04/recursively-convert-xml-into-multidimensional-array-as2/#comments</comments>
		<pubDate>Thu, 04 Sep 2008 21:18:05 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS2]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=64</guid>
		<description><![CDATA[This is a code snippet I wrote awhile back that recursively works through an XML file and converts it from XML into a multidimensional array. This was from the days before E4X was a part of ActionScript, and as such this is an ActionScript 2.0 example, so it may not be as useful as it [...]]]></description>
			<content:encoded><![CDATA[<p>This is a code snippet I wrote awhile back that recursively works through an XML file and converts it from XML into a multidimensional array. This was from the days before <a href="http://en.wikipedia.org/wiki/E4X" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/E4X?referer=');">E4X</a> was a part of ActionScript, and as such this is an ActionScript 2.0 example, so it may not be as useful as it once was. But nonetheless, maybe someone will find it of use. Here it is (usage example is at the end):</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">//recursive function to convert an XML file to an array</span>
<span style="color: #000000; font-weight: bold;">function</span> XMLtoArray<span style="color: #66cc66;">&#40;</span>node:<span style="color: #0066CC;">XMLNode</span>, arr:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">var</span> nodelen:<span style="color: #0066CC;">Number</span> = node.<span style="color: #0066CC;">childNodes</span>.<span style="color: #0066CC;">length</span>;<span style="color: #808080; font-style: italic;">//number of children nodes within a particular node</span>
	<span style="color: #000000; font-weight: bold;">var</span> resultValue:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;<span style="color: #808080; font-style: italic;">//result of a recursive call to this function</span>
	<span style="color: #000000; font-weight: bold;">var</span> count:<span style="color: #0066CC;">Number</span> = <span style="color: #cc66cc;">0</span>;<span style="color: #808080; font-style: italic;">//index number of the current node relative to its siblings</span>
&nbsp;
	node = node.<span style="color: #0066CC;">firstChild</span>;
&nbsp;
	<span style="color: #808080; font-style: italic;">//loop through node children</span>
	<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i=<span style="color: #cc66cc;">0</span>; i <span style="color: #66cc66;">&lt;</span> nodelen; i++<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//if the first child of that node does not have children</span>
		<span style="color: #808080; font-style: italic;">//create an associative array with the node name equal to the node value</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>node.<span style="color: #0066CC;">firstChild</span>.<span style="color: #0066CC;">hasChildNodes</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			arr<span style="color: #66cc66;">&#91;</span>node.<span style="color: #0066CC;">nodeName</span><span style="color: #66cc66;">&#93;</span> = node.<span style="color: #0066CC;">firstChild</span>;
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #808080; font-style: italic;">//else node has more nodes inside, make a recursive call to walk down the XML tree</span>
			arr<span style="color: #66cc66;">&#91;</span>count<span style="color: #66cc66;">&#93;</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>;
			resultValue = XMLtoArray<span style="color: #66cc66;">&#40;</span>node, arr<span style="color: #66cc66;">&#91;</span>count<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>resultValue <span style="color: #66cc66;">!</span>= <span style="color: #0066CC;">undefined</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
				arr.<span style="color: #0066CC;">push</span><span style="color: #66cc66;">&#40;</span>resultValue<span style="color: #66cc66;">&#41;</span>;<span style="color: #808080; font-style: italic;">//push result into array</span>
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #808080; font-style: italic;">//when loop is on its last iteration return the compiled array</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>i == <span style="color: #66cc66;">&#40;</span>nodelen-<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> arr;
		<span style="color: #66cc66;">&#125;</span>
		count++;
		node = node.<span style="color: #0066CC;">nextSibling</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #0066CC;">undefined</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">var</span> imgXML:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;&lt;cat&gt;&lt;img&gt;&lt;filename&gt;1.jpg&lt;/filename&gt;&lt;caption&gt;cap1&lt;/caption&gt;&lt;/img&gt;&lt;img&gt;&lt;filename&gt;2.jpg&lt;/filename&gt;&lt;caption&gt;cap2&lt;/caption&gt;&lt;/img&gt;&lt;/cat&gt;&quot;</span>;
<span style="color: #000000; font-weight: bold;">var</span> xmlArray:<span style="color: #0066CC;">Array</span> = XMLtoArray<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">XML</span><span style="color: #66cc66;">&#40;</span>imgXML<span style="color: #66cc66;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #0066CC;">Array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>xmlArray<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;caption&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//xmlArray[0] goes into first node &lt;cat&gt;</span>
<span style="color: #808080; font-style: italic;">//xmlArray[0][0] goes into second node &lt;img&gt;</span>
<span style="color: #808080; font-style: italic;">//xmlArray[0][0][&quot;caption&quot;] retrieves contents of &lt;caption&gt;cap1&lt;/caption&gt;</span>
&nbsp;
<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>xmlArray<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;filename&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #808080; font-style: italic;">//outputs the filename of the second image</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2008/09/04/recursively-convert-xml-into-multidimensional-array-as2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Scaling a DisplayObject to fit to a container&#8217;s dimensions</title>
		<link>http://blog.anselmbradford.com/2008/06/30/scaling-a-displayobject-to-fit-to-a-containers-dimensions/</link>
		<comments>http://blog.anselmbradford.com/2008/06/30/scaling-a-displayobject-to-fit-to-a-containers-dimensions/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 23:16:52 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=27</guid>
		<description><![CDATA[I&#8217;ve often run into the problem of proportionally scaling an image (or other DisplayObject) to the dimensions of its parent container. I always end up having to scavenge around in my code bank for the line of code to do this—which really is a simple bit of code—but I prefer to copy-and-paste it than rewrite [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve often run into the problem of proportionally scaling an image (or other DisplayObject) to the dimensions of its parent container. I always end up having to scavenge around in my code bank for the line of code to do this—which really is a simple bit of code—but I prefer to copy-and-paste it than rewrite it. SO I am posting it here so I never have to search for it again:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> scale:<span style="color: #0066CC;">Number</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>container.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">/</span>container.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #66cc66;">&#40;</span>image.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">/</span>image.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #66cc66;">&#40;</span>container.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">/</span>image.<span style="color: #0066CC;">width</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #66cc66;">&#40;</span>container.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">/</span>image.<span style="color: #0066CC;">height</span><span style="color: #66cc66;">&#41;</span>;
image.<span style="color: #006600;">scaleX</span> = scale;
image.<span style="color: #006600;">scaleY</span> = scale;</pre></div></div>

<p><a href="/wp-content/uploads/2008/06/scaletocontainer.zip">Download an example here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2008/06/30/scaling-a-displayobject-to-fit-to-a-containers-dimensions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Spring &lt;br /&gt; 2008 Presentation</title>
		<link>http://blog.anselmbradford.com/2008/06/04/spring-br-2008-presentation/</link>
		<comments>http://blog.anselmbradford.com/2008/06/04/spring-br-2008-presentation/#comments</comments>
		<pubDate>Thu, 05 Jun 2008 06:48:02 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=23</guid>
		<description><![CDATA[This past Tuesday I spoke about Data Structures at the 2008 Spring &#60;br /&#62; Conference in Baker Center, at Ohio University in Athens, Ohio. Since I was involved in organizing the conference this year, it was a pleasure to have the big day finally arrive. I enjoyed some technical wizardry by Samuel Agesilas Pastel, a [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.anselmbradford.com/wp-content/uploads/2008/06/sbbadge.png" alt="Speaker - Spring Break 2008 Conference" title="Speaker - Spring Break 2008 Conference" width="180" height="120" class="alignleft size-full wp-image-25" />This past Tuesday I spoke about Data Structures at the 2008 <a href="http://www.sbconference.com" onclick="pageTracker._trackPageview('/outgoing/www.sbconference.com?referer=');">Spring &lt;br /&gt; Conference</a> in Baker Center, at Ohio University in Athens, Ohio. Since I was involved in organizing the conference this year, it was a pleasure to have the big day finally arrive. I enjoyed some technical wizardry by Samuel Agesilas Pastel, a dreamy walk down the Internet&#8217;s memory lane in Eric Meyer&#8217;s lunchtime presentation, open source wonders in Orson Bradford&#8217;s (yes we&#8217;re related) <em>Introduction to Drupal</em> and Eric Kramer&#8217;s <em>Using the Firebug &#038; WebDeveloper Extensions in Firefox</em>.</p>
<p>The slides from my presentation, <em>Data Structures in AS3</em>, are <a href="/wp-content/uploads/2008/06/springbreakprez2008.swf">available online.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2008/06/04/spring-br-2008-presentation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting private property values of a cloned ActionScript 3 object</title>
		<link>http://blog.anselmbradford.com/2008/05/24/preserving-private-property-values-of-a-cloned-actionscript-3-object/</link>
		<comments>http://blog.anselmbradford.com/2008/05/24/preserving-private-property-values-of-a-cloned-actionscript-3-object/#comments</comments>
		<pubDate>Sat, 24 May 2008 20:31:04 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=22</guid>
		<description><![CDATA[Sometimes when cloning an object it is desirable to set a private or read-only property on the clone. Since these properties aren&#8217;t writable from outside the class itself, how can their values be changed? Drawing from the ideas of Grant Skinner&#8217;s post on Singletons the same internal class technique can be used to set properties [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes when cloning an object it is desirable to set a private or read-only property on the clone. Since these properties aren&#8217;t writable from outside the class itself, how can their values be changed?</p>
<p>Drawing from the ideas of Grant Skinner&#8217;s post on <a href="http://www.gskinner.com/blog/archives/2006/07/as3_singletons.html" onclick="pageTracker._trackPageview('/outgoing/www.gskinner.com/blog/archives/2006/07/as3_singletons.html?referer=');">Singletons</a> the same internal class technique can be used to set properties on an object through its constructor, but only if it is instantiated within the class itself, such as through a clone() method.</p>
<p>The following is an example of a simple class that has a clone() method and the clone has a read-only property that is set when it is instantiated:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">package
<span style="color: #66cc66;">&#123;</span>
	<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Cloner
	<span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">/*----------------------------------------------------------------------
		* properties
		*---------------------------------------------------------------------*/</span>
		<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> _isClone_:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;
&nbsp;
		<span style="color: #808080; font-style: italic;">/*----------------------------------------------------------------------
		* constructor
		*---------------------------------------------------------------------*/</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Cloner<span style="color: #66cc66;">&#40;</span> allow:AllowConstructorArguments = <span style="color: #000000; font-weight: bold;">null</span> , prop:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #66cc66;">&#41;</span> 
		<span style="color: #66cc66;">&#123;</span>
		  	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> allow is AllowConstructorArguments <span style="color: #66cc66;">&#41;</span> 
		  	<span style="color: #66cc66;">&#123;</span>
		  		_isClone_ = prop;
		  	<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/*----------------------------------------------------------------------
		* public methods
		*---------------------------------------------------------------------*/</span>
		<span style="color: #808080; font-style: italic;">/**
		* Whether this object was created from the clone method.
		*/</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #0066CC;">get</span> isClone<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#123;</span> <span style="color: #b1b100;">return</span> _isClone_; <span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #808080; font-style: italic;">/**
		* Create a clone of this object
		*/</span>
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> clone<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> : Cloner
		<span style="color: #66cc66;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> Cloner<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> AllowConstructorArguments<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> , <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">/**
* Internal class for allowing arguments in the constructor if this class is 
* instantiated from within this class.
*/</span>
internal <span style="color: #000000; font-weight: bold;">class</span> AllowConstructorArguments<span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Alternatively, if you would not like the constructor to have a long list of parameters. The <a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/statements.html#..._(rest)_parameter" onclick="pageTracker._trackPageview('/outgoing/livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/statements.html_..._rest_parameter?referer=');">&#8230; (rest) parameter</a> could be used in the constructor, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">...
		<span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> Cloner<span style="color: #66cc66;">&#40;</span> ... <span style="color: #006600;">args</span>:<span style="color: #0066CC;">Array</span> <span style="color: #66cc66;">&#41;</span> 
		<span style="color: #66cc66;">&#123;</span>
		  	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span> args<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> is AllowConstructorArguments <span style="color: #66cc66;">&#41;</span> 
		  	_secretSwitch = <span style="color: #0066CC;">Boolean</span><span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
...</pre></div></div>

<p>The class can be tested with the following:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">			<span style="color: #000000; font-weight: bold;">var</span> c:Cloner = <span style="color: #000000; font-weight: bold;">new</span> Cloner<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">var</span> d:Cloner = c.<span style="color: #006600;">clone</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #808080; font-style: italic;">// outputs: false true</span>
			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span> c.<span style="color: #006600;">isClone</span> , d.<span style="color: #006600;">isClone</span> <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2008/05/24/preserving-private-property-values-of-a-cloned-actionscript-3-object/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Encapsulating interactive behavior in Flex applications for better code readability</title>
		<link>http://blog.anselmbradford.com/2008/05/07/encapsulating-interactive-behavior-in-flex-applications-for-better-code-readability/</link>
		<comments>http://blog.anselmbradford.com/2008/05/07/encapsulating-interactive-behavior-in-flex-applications-for-better-code-readability/#comments</comments>
		<pubDate>Thu, 08 May 2008 01:13:16 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[AS3]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=14</guid>
		<description><![CDATA[Consider for a moment dragging an element within an application. The steps required for this operation are as follows: Behavior Description Event to listen for START DRAGGING&#8230; When the mouse button is pressed down over the element. MouseEvent.MOUSE_DOWN DRAG&#8230; While the mouse is moving and the mouse button is pressed down over the element. MouseEvent.MOUSE_MOVE [...]]]></description>
			<content:encoded><![CDATA[<p>Consider for a moment dragging an element within an application. The steps required for this operation are as follows:</p>
<table border="1" cellpadding="10">
<tbody>
<tr style="font-weight:bold;">
<td>Behavior</td>
<td>Description</td>
<td>Event to listen for</td>
</tr>
<tr>
<td>START DRAGGING&#8230;</td>
<td>When the mouse button is pressed down over the element.</td>
<td>MouseEvent.MOUSE_DOWN</td>
</tr>
<tr>
<td>DRAG&#8230;</td>
<td>While the mouse is moving and the mouse button is pressed down over the element.</td>
<td>MouseEvent.MOUSE_MOVE</td>
</tr>
<tr>
<td rowspan="2">STOP DRAGGING&#8230;</td>
<td>When the mouse button is released over the element.</td>
<td>MouseEvent.MOUSE_UP</td>
</tr>
<tr>
<td>When the mouse is moved outside the stage (e.g. outside the application window). This is necessary because we cannot determine whether the mouse button has been released while the mouse is outside the application window.</td>
<td>Event.MOUSE_LEAVE</td>
</tr>
</tbody>
</table>
<p>Adobe Flex&#8217;s event model makes it easy to listen for interactive mouse events within an application. A simple operation like a mouse click only requires listening for a MouseEvent.CLICK event being dispatched from the element in question, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;">element.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> MouseEvent.<span style="color: #006600;">CLICK</span> , _element_clickHandler <span style="color: #66cc66;">&#41;</span>;
...
<span style="color: #006600;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _element_clickHandler<span style="color: #66cc66;">&#40;</span> event:MouseEvent <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #808080; font-style: italic;">// handle click on element</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>While handling a mouse click is simple enough to do, more complex interactive behavior such as dragging requires listening to multiple events from both the element in question as well as the stage. Implementing this behavior can clutter your code and not be as clear as it could be, since there is not a dragging event to listen for natively in Flex (Drag and Drop aside).</p>
<p>An alternative is to encapsulate the dragging behavior in a separate class that acts as a proxy for the element and issues its own events that the main application listens for. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">var</span> behavior:DraggableBehavior = <span style="color: #000000; font-weight: bold;">new</span> DraggableBehavior<span style="color: #66cc66;">&#40;</span>element<span style="color: #66cc66;">&#41;</span>;
element.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span> DraggableBehaviorEvent.<span style="color: #006600;">DRAGGING</span> , _element_draggingHandler <span style="color: #66cc66;">&#41;</span>;
...
<span style="color: #006600;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _element_draggingHandler<span style="color: #66cc66;">&#40;</span> event:DraggableBehaviorEvent <span style="color: #66cc66;">&#41;</span> : <span style="color: #0066CC;">void</span>
<span style="color: #66cc66;">&#123;</span>
<span style="color: #808080; font-style: italic;">// handle dragging of element</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This moves all the event handling methods and listeners to a separate class file, organizing your code better. It also creates an event that more accurately reflects what operation is going on.</p>
<p> </p>
<p><span style="color: #551a8b; text-decoration: underline;">
<object width="228" height="280">
<param name="movie" value="/wp-content/uploads/2008/05/draggablebehaviorexample.swf"></param>
<param name="quality" value="high"></param>
<param name="wmode" value="window"></param>
<param name="menu" value="false"></param>
<param name="bgcolor" value="#FFFFFF"></param>
<embed type="application/x-shockwave-flash" width="228" height="280" src="/wp-content/uploads/2008/05/draggablebehaviorexample.swf" quality="high" bgcolor="#FFFFFF" wmode="window" menu="false" ></embed>
</object>
<br />
</span></p>
<p><a href="/wp-content/uploads/2008/05/draggablebehaviorexample-src-code.zip">Download Source Here</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2008/05/07/encapsulating-interactive-behavior-in-flex-applications-for-better-code-readability/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Efficient storage and retrieval of boolean values using bitwise operations</title>
		<link>http://blog.anselmbradford.com/2008/05/01/efficient-storage-of-options-using-bitwise-operations/</link>
		<comments>http://blog.anselmbradford.com/2008/05/01/efficient-storage-of-options-using-bitwise-operations/#comments</comments>
		<pubDate>Fri, 02 May 2008 00:22:14 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=8</guid>
		<description><![CDATA[Since each digit in a binary number is only composed of one of two possible values, 0 or 1, a binary number can be thought of as a series of switches. Each of these switches can be thought of as a TRUE or FALSE value if it is 1 or 0. So a binary number [...]]]></description>
			<content:encoded><![CDATA[<p>Since each digit in a binary number is only composed of one of two possible values, 0 or 1, a binary number can be thought of as a series of switches. </p>
<p><img class="aligncenter size-full wp-image-5" title="switches" src="http://blog.anselmbradford.com/wp-content/uploads/2008/05/switches.jpg" alt="" width="253" height="164" /></p>
<p>Each of these switches can be thought of as a TRUE or FALSE value if it is 1 or 0. So a binary number can then be used to represent a list of boolean values. If we assigned a property to each bit in this binary number we would be able to store the value of a set of boolean properties (five properties in this case) within a single number. </p>
<p>For example, suppose you wanted to limit access to your computer for certain users, and you wanted to store what privileges each user had within a database. You could create a table for the users and create fields for CAN_READ, CAN_WRITE, CAN_EXECUTE. This would work fine, but using a binary number as the storage mechanism for the privileges can make this process much more elegant and efficient. </p>
<p>Take a three bit binary number, for instance, 111 (read that as one-one-one). Each bit could be assigned a boolean value. So the rightmost bit could represent the boolean value for CAN_READ, the middle bit could represent CAN_WRITE, and the leftmost bit could represent CAN_EXECUTE. Therefore switching any of these three values to 0 within the binary number would specify that that privilege was not granted for a particular user. For example, the binary number 011 (otherwise written as 3 in decimal) would specify that a user could read and write files on the computer but could not execute applications. </p>
<p>To create this kind of arrangement create a table to hold the privileges (a.k.a. permissions). Insert entries into this table for each bit in the whole binary number. Next create a users table that has a field for holding a number that represents what privileges a user has. In the permissions table each entry will only have one bit turned on, while the entries in the users table could have multiple bits turned on because a user could have more than one privilege on the computer.</p>
<p>So here is the SQL to create those two tables and fill them with data:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> PERMISSION
<span style="color: #66cc66;">&#40;</span>
	bit			BIGINT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">UNIQUE</span><span style="color: #66cc66;">,</span>
	name			VARCHAR<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">32</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">UNIQUE</span><span style="color: #66cc66;">,</span>
	description		VARCHAR<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">2048</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span> bit <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> USER
<span style="color: #66cc66;">&#40;</span>
	id			SERIAL<span style="color: #66cc66;">,</span>
	name			VARCHAR<span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">32</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">UNIQUE</span><span style="color: #66cc66;">,</span>
	bitpattern		BIGINT <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	<span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span> id <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> PERMISSION <span style="color: #66cc66;">&#40;</span> bit<span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">,</span> description <span style="color: #66cc66;">&#41;</span>
	 <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'CAN_READ'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'User can read files'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> PERMISSION <span style="color: #66cc66;">&#40;</span> bit<span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">,</span> description <span style="color: #66cc66;">&#41;</span>
	 <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'CAN_WRITE'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'User can write files'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> PERMISSION <span style="color: #66cc66;">&#40;</span> bit<span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">,</span> description <span style="color: #66cc66;">&#41;</span>
	 <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'CAN_EXECUTE'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'User can execute applications'</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> USER <span style="color: #66cc66;">&#40;</span> id<span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">,</span> bitpattern <span style="color: #66cc66;">&#41;</span>
	 <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'admin'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">7</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> USER <span style="color: #66cc66;">&#40;</span> id<span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">,</span> bitpattern <span style="color: #66cc66;">&#41;</span>
	 <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'manager'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> USER <span style="color: #66cc66;">&#40;</span> id<span style="color: #66cc66;">,</span> name<span style="color: #66cc66;">,</span> bitpattern <span style="color: #66cc66;">&#41;</span>
	 <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'enduser'</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Here&#8217;s what they look like in a more visual way: <br />
<img src="http://blog.anselmbradford.com/wp-content/uploads/2008/05/bitwise_schema.png" alt="" title="bitwise_schema" width="241" height="106" class="aligncenter size-full wp-image-12" /></p>
<p>&#8230;and filled with data:<br />
<img src="http://blog.anselmbradford.com/wp-content/uploads/2008/05/bitwise_schema_data.png" alt="" title="bitwise_schema_data" width="332" height="256" class="aligncenter size-full wp-image-13" /></p>
<p>Now to pull the permissions for a particular user out of the database we will use the bitwise AND operator (&#038;). This operator compares two numbers binary bit by binary bit. If the bit at a particular position in both numbers is 1, then the bit at that position in the resulting number is 1, otherwise it is 0. So for example if we took the numbers 011 (3 in decimal) and 100 (4 in decimal) and performed a bitwise AND operation on them it would look like this:</p>
<p>011<br />
100<br />
&#8212;-<br />
000</p>
<p>The result is 000, because none of the 1 bits overlapped. If the first number was the permissions a user had and the second number was the permission to execute applications, then performing a bit AND operation on these two numbers would determine that that particular user could not execute applications. If it had resulted in any other number besides 0 it means that they could execute applications. Therefore to query the database to find the permissions of a particular user we would perform the following:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> USER<span style="color: #66cc66;">,</span> PERMISSION 
	  <span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span>bitpattern &amp; bit !<span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> 
	  <span style="color: #993333; font-weight: bold;">AND</span> <span style="color: #66cc66;">&#40;</span>name <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'manager'</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Which would search through the PERMISSION table and only return those entries where a bitwise AND operation between the &#8216;manager&#8217; user&#8217;s permissions number and the bit of the privilege did not return 0. In this case it would return the entries for CAN_READ and CAN_WRITE.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2008/05/01/efficient-storage-of-options-using-bitwise-operations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Formatting decimal as binary</title>
		<link>http://blog.anselmbradford.com/2008/05/01/formatting-decimal-as-binary/</link>
		<comments>http://blog.anselmbradford.com/2008/05/01/formatting-decimal-as-binary/#comments</comments>
		<pubDate>Thu, 01 May 2008 23:39:12 +0000</pubDate>
		<dc:creator>Ans</dc:creator>
				<category><![CDATA[Math]]></category>

		<guid isPermaLink="false">http://blog.anselmbradford.com/?p=4</guid>
		<description><![CDATA[While most of the time you will see numbers in the decimal number system (using the digits 0 through 9), internally within your computer these numbers will be represented in binary. In the binary numeral system each number can only be composed of a sequence of two values: 0 or 1, ON or OFF, TRUE or [...]]]></description>
			<content:encoded><![CDATA[<p>While most of the time you will see numbers in the decimal number system (using the digits 0 through 9), internally within your computer these numbers will be represented in binary. In the binary numeral system each number can only be composed of a sequence of two values: 0 or 1, ON or OFF, TRUE or FALSE, and so on.</p>
<p>Binary numbers are sequences of 1&#8242;s and 0&#8242;s, moving from right to left, each occurrence of a 1 is equivalent to twice the decimal value of the previous digit&#8217;s decimal value. Take for example the binary number 11111111. The first digit has the decimal value of 1, the second has the decimal value of 2, the third has the decimal value of 4, and so on. Adding all these values together equals the final decimal value, which in this case is 255.</p>
<p><img class="aligncenter size-full wp-image-6" title="binary11111111" src="http://blog.anselmbradford.com/wp-content/uploads/2008/05/binary11111111-300x276.png" alt="" width="300" height="276" /></p>
<p>Each digit in a binary number is called a bit, in this case all bits are 1. If some of the bits were 0, they would not add to the resulting decimal number, but they would still represent a place within the sequence.</p>
<p><img class="aligncenter size-medium wp-image-7" title="binary01010010" src="http://blog.anselmbradford.com/wp-content/uploads/2008/05/binary01010010-300x276.png" alt="" width="300" height="276" /></p>
<p>To play around with converting numbers from one base to another, try my <a href="/?p=3">Base Converter widget</a>.</p>
<p>Treating decimal numbers as binary numbers is useful when dealing with bitwise programming, where individual bits within a binary number can be switched to their reverse value (either 1 or 0). This allows for extremely efficient division by 2 arithmetic and for a technique of representing binary numbers as an efficient storage mechanism for a bunch of boolean values.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.anselmbradford.com/2008/05/01/formatting-decimal-as-binary/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
