<?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>Microbits</title>
	<atom:link href="http://microbits.info/index.php/feed" rel="self" type="application/rss+xml" />
	<link>http://microbits.info</link>
	<description>Random thoughts about programming, gaming, and the world.</description>
	<lastBuildDate>Sat, 12 May 2012 10:09:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Cygwin .bash_profile</title>
		<link>http://microbits.info/index.php/2010/05/cygwin-bash_profile</link>
		<comments>http://microbits.info/index.php/2010/05/cygwin-bash_profile#comments</comments>
		<pubDate>Fri, 21 May 2010 21:53:56 +0000</pubDate>
		<dc:creator>recon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cygwin]]></category>

		<guid isPermaLink="false">http://microbits.info/?p=515</guid>
		<description><![CDATA[This file will put a more standard Linux prompt on your Cygwin login shell (file should be ~/.bash_profile): #!/bin/bash # Custom prompt PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h:\[\e[33m\]\w\[\e[0m\]$ ']]></description>
			<content:encoded><![CDATA[<p>This file will put a more standard Linux prompt on your Cygwin login shell (file should be ~/.bash_profile):</p>
<pre class="brush:bash">
#!/bin/bash

# Custom prompt
PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u@\h:\[\e[33m\]\w\[\e[0m\]$ '
</pre>
]]></content:encoded>
			<wfw:commentRss>http://microbits.info/index.php/2010/05/cygwin-bash_profile/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fixing box drawing in Midnight Commander</title>
		<link>http://microbits.info/index.php/2010/05/fixing-box-drawing-in-midnight-commander</link>
		<comments>http://microbits.info/index.php/2010/05/fixing-box-drawing-in-midnight-commander#comments</comments>
		<pubDate>Fri, 21 May 2010 21:38:08 +0000</pubDate>
		<dc:creator>recon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cygwin]]></category>

		<guid isPermaLink="false">http://microbits.info/?p=507</guid>
		<description><![CDATA[Launching MC in rxvt showed all the box drawing characters replaced with strange looking accented characters&#8230; What? Turns out none of the Windows fonts support box drawing characters (from what I&#8217;ve read, it seems that they don&#8217;t have the characters &#8230; <a href="http://microbits.info/index.php/2010/05/fixing-box-drawing-in-midnight-commander">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Launching MC in rxvt showed all the box drawing characters replaced with strange looking accented characters&#8230; What?</p>
<p>Turns out none of the Windows fonts support box drawing characters (from what I&#8217;ve read, it seems that they don&#8217;t have the characters in the expected positions in the character table)&#8230;</p>
<p>The easiest solution is to tell MC to use non box drawing characters to draw boxes with the -a switch. The other solution is to get the <a class="downloadlink" href="http://microbits.info/wp-content/plugins/download-monitor/download.php?id=11" title=" downloaded 307 times" >Lucida ConsoleP Font. (307)</a>, and set rxvt to use it (as suggested by <a href="http://blogs.linux.ie/fuzzbucket/2008/02/23/cygwin/" target="_blank">this guide</a>).</p>
<p>How do you set the fonts? You could set them with command line switches, or use a .XResources file in your Cygwin home directory (base file from <a href="http://c2.com/cgi/wiki?BetterCygwinTerminal" target="_blank">here</a>)):</p>
<pre class="brush:plain">rxvt.font:             Lucida Console-14
rxvt.boldFont:         Lucida Console-14
rxvt.scrollBar:        True
rxvt.visualBell:       True
rxvt.loginShell:       True
rxvt.background:       Black
rxvt.foreground:       White
rxvt.saveLines:        3000
rxvt.cursorColor:      Green
rxvt.scrollBar_right:  True</pre>
<p>If you want to use the box drawing font (you have to install it on the system first):</p>
<pre class="brush:plain">rxvt.font:             Lucida ConsoleP-14
rxvt.boldFont:         Lucida ConsoleP-14
rxvt.scrollBar:        True
rxvt.visualBell:       True
rxvt.loginShell:       True
rxvt.background:       Black
rxvt.foreground:       White
rxvt.saveLines:        3000
rxvt.cursorColor:      Green
rxvt.scrollBar_right:  True</pre>
<p>If you use the standard DOS terminal for Cygwin, you may want to change the code page setting in the batch file to:</p>
<pre class="brush:plain">set CYGWIN=codepage:oem</pre>
]]></content:encoded>
			<wfw:commentRss>http://microbits.info/index.php/2010/05/fixing-box-drawing-in-midnight-commander/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Source Engine Servers and Network Protocols &#8211; Security</title>
		<link>http://microbits.info/index.php/2010/01/source-engine-servers-and-network-protocols-security</link>
		<comments>http://microbits.info/index.php/2010/01/source-engine-servers-and-network-protocols-security#comments</comments>
		<pubDate>Fri, 22 Jan 2010 01:45:00 +0000</pubDate>
		<dc:creator>recon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[gaming]]></category>
		<category><![CDATA[source engine]]></category>
		<category><![CDATA[valve]]></category>

		<guid isPermaLink="false">http://microbits.info/?p=463</guid>
		<description><![CDATA[As an administrator of several Source based game servers, I&#8217;ve dealt with various known security vulnerabilities that Valve hasn&#8217;t bothered to fix. Some of the most common include client side console commands, that if run once, or spammed (which the &#8230; <a href="http://microbits.info/index.php/2010/01/source-engine-servers-and-network-protocols-security">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As an administrator of several Source based game servers, I&#8217;ve dealt with various known security vulnerabilities that Valve hasn&#8217;t bothered to fix. Some of the most common include client side console commands, that if run once, or spammed (which the server will allow), will cause a degradation (massive lag) or denial of service (you guessed it, the server will crash). These really aren&#8217;t all that bad, since they are well known, server plugins are available to block the commands.</p>
<p>The most troubling incident I&#8217;ve dealt with involved an attacker who exploited a security vulnerability in Valve&#8217;s server query protocol, which caused a serious degradation of service condition. I couldn&#8217;t simply block all the query packets, since legitimate clients needed to be able to query servers. To further complicate matters, the attacker was also spoofing IP headers, which ruled out the possibility of a simple firewall rule.</p>
<p>This particular vulnerability was not really bad design per say (although it was poorly designed), but more a really poor implementation. Here are the details:<br />
<span id="more-463"></span><br />
When a client wishes to find out basic information about a server (player counts, game type, game version, etc), they send a packet to the server requesting the information. The server then locates all this information, creates a packet and replies.</p>
<p>Normally, the query protocol works perfectly, without any performance issues. However, the code that generates the replies is so poorly written, that a large number of query packets will generate lag spikes on the server, thus making the server unplayable (degradation of service). Did Valve require a handshake / challenge before replying with this expensive to generate packet? Nope.</p>
<p>To resolve the attack, I wrote a server plugin that hooks the Winsock recvfrom call, and replies to query packets (it generates the replies itself) from a high performance cache (it returns SOCKET_ERROR to the server so it doesn&#8217;t try to reply).</p>
<p>With this cache installed, the server works flawlessly. This makes me wonder, how could Valve have written such inefficient code, and having written it, consider it ready for production.</p>
<p>I&#8217;d like to take a minute here and remind everyone this is just one of many known (and inexplicably, not yet patched) vulnerabilities (in game and network based) in the Source engine.</p>
]]></content:encoded>
			<wfw:commentRss>http://microbits.info/index.php/2010/01/source-engine-servers-and-network-protocols-security/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>MinGW</title>
		<link>http://microbits.info/index.php/2010/01/mingw</link>
		<comments>http://microbits.info/index.php/2010/01/mingw#comments</comments>
		<pubDate>Fri, 08 Jan 2010 06:47:59 +0000</pubDate>
		<dc:creator>recon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[mingw]]></category>

		<guid isPermaLink="false">http://microbits.info/?p=434</guid>
		<description><![CDATA[After needing a copy of MinGW to compile some C code, I was somewhat annoyed to find that their all in one installer is now considered &#8220;deprecated&#8221;. That meant I had to download and extract 15 packages manually (two of &#8230; <a href="http://microbits.info/index.php/2010/01/mingw">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After needing a copy of MinGW to compile some C code, I was somewhat annoyed to find that their all in one installer is now considered &#8220;deprecated&#8221;. That meant I had to download and extract 15 packages manually (two of them were even compressed as .lzma).</p>
<p>Why anyone would compress a package designed for Windows in .lzma format? The only way to extract it is with a command line sdk tool. Even on Linux, distributing archives in .lzma format is a little strange.</p>
<p>Anyhow, to save everyone the trouble, here&#8217;s a package (includes C, C++ and all necessary tools) in .7z format (which can be extracted by <a href="http://www.7-zip.org/" target="_blank">7-Zip</a>, the best file archiver for Windows) -&gt; <a class="downloadlink" href="http://microbits.info/wp-content/plugins/download-monitor/download.php?id=10" title="Version1.0.0.0 downloaded 238 times" >MinGW Compiler Package (238)</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://microbits.info/index.php/2010/01/mingw/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More on eAccelerator</title>
		<link>http://microbits.info/index.php/2009/12/more-on-eaccelerator</link>
		<comments>http://microbits.info/index.php/2009/12/more-on-eaccelerator#comments</comments>
		<pubDate>Sun, 27 Dec 2009 22:27:04 +0000</pubDate>
		<dc:creator>recon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[eaccelerator]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://microbits.info/?p=429</guid>
		<description><![CDATA[Now that I&#8217;m correctly loading the extension (zend_extension_ts), eAccelerator is running smoothly. I found something interesting in the event log yesterday: PHP Warning: [eAccelerator] This build of "eAccelerator" was compiled for PHP version 5.2.9. Rebuild it for your PHP version &#8230; <a href="http://microbits.info/index.php/2009/12/more-on-eaccelerator">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Now that I&#8217;m correctly loading the extension (zend_extension_ts), eAccelerator is running smoothly.</p>
<p>I found something interesting in the event log yesterday:</p>
<pre class="brush: plain">
PHP Warning:  [eAccelerator] This build of "eAccelerator" was compiled for PHP version 5.2.9. Rebuild it for your PHP version (5.2.9-2) or download precompiled binaries.
</pre>
<p>Now that error message makes perfect sense. The reason I didn&#8217;t see it the first time around is that PHP logged two messages separately at the same time. If I was reading a flat file log, I would&#8217;ve noticed it immediately.</p>
]]></content:encoded>
			<wfw:commentRss>http://microbits.info/index.php/2009/12/more-on-eaccelerator/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Thread safety and PHP</title>
		<link>http://microbits.info/index.php/2009/12/thread-safety-and-the-insanity-of-php-on-windows</link>
		<comments>http://microbits.info/index.php/2009/12/thread-safety-and-the-insanity-of-php-on-windows#comments</comments>
		<pubDate>Sun, 27 Dec 2009 04:06:30 +0000</pubDate>
		<dc:creator>recon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[eaccelerator]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://microbits.info/?p=419</guid>
		<description><![CDATA[UPDATED After a few seg-faults, I realized that eAccelerator isn&#8217;t thread safe (I was running it under a thread safe server)&#8230; It is thread safe&#8230; I just wasn&#8217;t loading it in thread safe mode (see the manual for details). Whoops.]]></description>
			<content:encoded><![CDATA[<p><span style="color: #ff0000;"><strong>UPDATED</strong></span></p>
<p>After a few seg-faults, I realized that eAccelerator <span style="text-decoration: line-through;">isn&#8217;t thread safe (I was running it under a thread safe server)&#8230; </span><span style="color: #ff0000;"><strong>It is thread safe</strong></span>&#8230; I just wasn&#8217;t loading it in thread safe mode (<a href="http://eaccelerator.net/wiki/InstallFromSource" target="_blank">see the manual for details</a>). Whoops.</p>
]]></content:encoded>
			<wfw:commentRss>http://microbits.info/index.php/2009/12/thread-safety-and-the-insanity-of-php-on-windows/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>eAccelerator</title>
		<link>http://microbits.info/index.php/2009/12/eaccelerator</link>
		<comments>http://microbits.info/index.php/2009/12/eaccelerator#comments</comments>
		<pubDate>Sun, 27 Dec 2009 00:59:46 +0000</pubDate>
		<dc:creator>recon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[eaccelerator]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://microbits.info/?p=411</guid>
		<description><![CDATA[After getting somewhat annoyed with the slowness of my blog, and having nothing better to do, I decided to setup eAccelerator. I&#8217;m pretty sure I tried setting it up a long time ago, but for some reason, didn&#8217;t finish. Following &#8230; <a href="http://microbits.info/index.php/2009/12/eaccelerator">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After getting somewhat annoyed with the slowness of my blog, and having nothing better to do, I decided to setup eAccelerator. I&#8217;m pretty sure I tried setting it up a long time ago, but for some reason, didn&#8217;t finish.</p>
<p>Following the path of logic, I went to the <a href="http://eaccelerator.net/" target="_blank">eAccelerator website</a> in search of the appropriate files. Their Windows section directed me to <a href="http://www.sitebuddy.com/PHP/Accelerators/eAccelerator_windows_binaries_builds" target="_blank">SiteBuddy</a>, which had a nice collection of binaries for various PHP versions. After determining my PHP version (5.2.9-2), I tried the closest match (5.2.9), and was greeted by the following error in the Windows event log:</p>
<pre class="brush: text">Unable to start eAccelerator module in Unknown on line 0.</pre>
<p>My initial reaction to the error: what the ****&#8230; (<a href="http://microbits.info/?p=429" target="_blank">UPDATE: See this post for more details</a>) After a quick Google search, I found that PHP modules must be built for the EXACT PHP version you plan to use them on.</p>
<p>Since I have just about every Microsoft C++ compiler, I figured compiling a new eAccellerator module for my version of PHP would be simple. I downloaded the archived PHP files I needed (now I understand why they keep archives), switched to the release configuration, and pressed build.<br />
<span id="more-411"></span></p>
<pre class="brush: text">error C2466: cannot allocate an array of constant size 0</pre>
<p>My reaction to this was about the same as the other one. After half an hour, I tracked down and fixed the problem, which was a <a href="http://bugs.php.net/bug.php?id=39130" target="_blank">broken Windows build configuration (which the PHP developers KNEW about, and marked as &#8220;Wont Fix&#8221;)</a>.</p>
<p>The solution was to make the changes suggested by sailormax.</p>
<p>In the end, everything compiled, and you&#8217;re reading my post through eAccelerator. I noticed that blog page loads are 2 &#8211; 3 times faster. <img src='http://microbits.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://microbits.info/index.php/2009/12/eaccelerator/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Best of open source (2009)</title>
		<link>http://microbits.info/index.php/2009/12/best-of-open-source-2009</link>
		<comments>http://microbits.info/index.php/2009/12/best-of-open-source-2009#comments</comments>
		<pubDate>Thu, 17 Dec 2009 02:09:34 +0000</pubDate>
		<dc:creator>recon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://microbits.info/?p=306</guid>
		<description><![CDATA[As 2009 starts to wrap up, here&#8217;s my list of the best open source software that I&#8217;ve used: The best of open source Software Comments WordPress The best blogging platform. Python One of the most modern and powerful programming languages. &#8230; <a href="http://microbits.info/index.php/2009/12/best-of-open-source-2009">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>As 2009 starts to wrap up, here&#8217;s my list of the best open source software that I&#8217;ve used:</p>
<table border="0">
<tbody>
<tr>
<th colspan="2">The best of open source</th>
</tr>
<tr>
<th>Software</th>
<th>Comments</th>
</tr>
<tr>
<td><a href="http://wordpress.org/" target="_blank">WordPress</a></td>
<td>The best blogging platform.</td>
</tr>
<tr>
<td><a href="http://python.org/" target="_blank">Python</a></td>
<td>One of the most modern and powerful programming languages. Incredible standard library.</td>
</tr>
<tr>
<td><a href="http://mumble.sourceforge.net/" target="_blank">Mumble</a></td>
<td>The best conference style VOIP. Incredible sound quality, the lowest latency and lowest bandwidth usage that I&#8217;ve ever seen in a VOIP system (open source or not). Version 1.2 seems to have fixed some of the major server administration issues.</td>
</tr>
<tr>
<td><a href="http://www.sagemath.org/" target="_blank">Sage (Open Source Mathematics)</a></td>
<td>An open source alternative to &#8220;Magma, Maple, Mathematica and Matlab&#8221;. Good documentation.</td>
</tr>
<tr>
<td><a href="http://www.ubuntu.com/" target="_blank">Ubuntu</a></td>
<td>My personal favorite Linux distribution. Version 9.10 adds cloud technology to both the desktop and server (yes, you can use it to make your own cloud).</td>
</tr>
<tr>
<td><a href="http://www.sourcemod.net/" target="_blank">SourceMod</a></td>
<td>The most modular and powerful server administration tool for Source based game servers.</td>
</tr>
<tr>
<td><a href="http://www.virtualbox.org/" target="_blank">VirtualBox</a></td>
<td>Open source (the RDP server, USB support, and USB over RDP are not) virtualization comparable to VMWare Workstation. Seamless windows for Linux and Windows guests. The closed source parts are available for free under the VirtualBox PUEL.</td>
</tr>
<tr>
<td><a href="http://www.mozilla.com/en-US/firefox/personal.html" target="_blank">Firefox</a></td>
<td>The best web browser.</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://microbits.info/index.php/2009/12/best-of-open-source-2009/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Browsing the web without ads</title>
		<link>http://microbits.info/index.php/2009/12/browsing-the-web-without-ads</link>
		<comments>http://microbits.info/index.php/2009/12/browsing-the-web-without-ads#comments</comments>
		<pubDate>Thu, 17 Dec 2009 00:23:36 +0000</pubDate>
		<dc:creator>recon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[web browsing]]></category>

		<guid isPermaLink="false">http://microbits.info/?p=395</guid>
		<description><![CDATA[Amusingly, I didn&#8217;t use an ad blocker until a few months ago. Now, I enjoy viewing sites without half a million ads cluttering up my screen. Ad blockers like ABP (Ad Block Plus) scan web pages for ads, and remove &#8230; <a href="http://microbits.info/index.php/2009/12/browsing-the-web-without-ads">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Amusingly, I didn&#8217;t use an ad blocker until a few months ago. Now, I enjoy viewing sites without half a million ads cluttering up my screen.</p>
<p>Ad blockers like <a href="https://addons.mozilla.org/en-US/firefox/addon/1865">ABP (Ad Block Plus)</a> scan web pages for ads, and remove them. As simple as they are, ad blockers are incredible.</p>
<p>I installed ABP when I was trying to print an article from a coding website. There were so many ads on their main and print views of the article that printing it would have barely been worth the effort. I remembered that a friend of mine had talked about ABP, so I looked it up and installed it. After enabling it and returning to the website, the ads were gone, and I could actually print the article.</p>
<p>ABP also lets you block new ads that it doesn&#8217;t already detect with a few simple clicks. Some websites use JavaScript to display ads, and if ABP can&#8217;t block them, <a href="https://addons.mozilla.org/en-US/firefox/addon/722">NoScript</a> can.</p>
<p>Web masters should realize that intrusive ads will just cause more and more people to start using ad blockers, and start being reasonable.</p>
]]></content:encoded>
			<wfw:commentRss>http://microbits.info/index.php/2009/12/browsing-the-web-without-ads/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Bitwise operations</title>
		<link>http://microbits.info/index.php/2009/09/bitwise-operations</link>
		<comments>http://microbits.info/index.php/2009/09/bitwise-operations#comments</comments>
		<pubDate>Sun, 13 Sep 2009 20:57:47 +0000</pubDate>
		<dc:creator>recon</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[bitwise]]></category>

		<guid isPermaLink="false">http://microbits.info/?p=326</guid>
		<description><![CDATA[While you don&#8217;t see them every day, bitwise operations are important. Everything at the hardware level is represented in binary (base 2, 1 and 0). Bitwise operations allow programmers to operate directly on the bits used by variables. This is &#8230; <a href="http://microbits.info/index.php/2009/09/bitwise-operations">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>While you don&#8217;t see them every day, bitwise operations are important. Everything at the hardware level is represented in binary (base 2, 1 and 0). Bitwise operations allow programmers to operate directly on the bits used by variables. This is very useful with enums, since you can have one variable be multiple values.</p>
<p>To understand how one enum variable can be multiple values, you need to understand the bitwise AND and OR operations.</p>
<p>An OR operation compares two sets of bits. If either bit is 1, the result is 1, else, result is 0. In the below example, set 1 is the value of enum option 1, and set 2 is the value of enum option 2.<br />
<span id="more-326"></span></p>
<pre class="brush: text">
Set 1:  11001000
Set 2:  00110110
Result: 11111110
</pre>
<p>An AND operation compares two sets of bits. If both bits are 1, the result is 1, else, result is 0. In the below example, set 1 is enum option 1, and set 2 is the result from the OR above.</p>
<pre class="brush: text">
Set 1:  11001000
Set 2:  11111110
Result: 11001000
</pre>
<p>Notice what the result is? enum option 1. Using this logic, we can create the following snippet:</p>
<pre class="brush: cpp">
enum Status {STOP, START, RUN};

// Bitwise OR of START and RUN
Status s = START | RUN;

// If the result of a bitwise AND of s and START is START
// (if condition is true)
if ((s &#038; START) == START))
   cout << "s is Start." << endl;

// If the result of a bitwise AND of s and RUN is RUN
// (if condition is true)
if ((s &#038; RUN) == RUN))
   cout << "s is Run." << endl;
</pre>
<p>Pretty neat right? Well, there's one more use that I learned yesterday.</p>
<p>Suppose you wanted to find out if an integer was a power of 2. One person volunteered a somewhat complex algorithm using logarithms, and someone else said, it's faster to count the bits. After reading the bit counting algorithm several times, I realized how amazingly simple it was. </p>
<p>The binary number system, base 2, has two digits, 1 and 0. Every place value is a power of two (in the decimal / base 10 system, there are ten digits, 0 - 9, and every place value is a power of 10). What this means is, if only one bit is 1 in the byte (a byte is 8 bits) storing the number, it must be a power of two.</p>
<p>For example:</p>
<pre class="brush: text">
Binary Place value: From right to left, 2 ^ 0, 2 ^ 1, 2 ^ 2, etc.

128 64 32 16 8 4 2 1
1   0  0  0  0 0 0 0

The binary byte, 10000000, is 128 in decimal, and 128 is 2 ^ 7.
</pre>
]]></content:encoded>
			<wfw:commentRss>http://microbits.info/index.php/2009/09/bitwise-operations/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.616 seconds -->

