<?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>tech stuff &#187; low level</title>
	<atom:link href="http://codytaylor.org/tag/low-level/feed" rel="self" type="application/rss+xml" />
	<link>http://codytaylor.org</link>
	<description>From Cody Taylor.</description>
	<lastBuildDate>Sun, 30 Oct 2011 04:15:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Checking Bits With PHP</title>
		<link>http://codytaylor.org/2009/06/checking-bits-with-php.html</link>
		<comments>http://codytaylor.org/2009/06/checking-bits-with-php.html#comments</comments>
		<pubDate>Tue, 16 Jun 2009 02:15:51 +0000</pubDate>
		<dc:creator>Cody Taylor</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[reference]]></category>
		<category><![CDATA[bits and bytes]]></category>
		<category><![CDATA[bitwise]]></category>
		<category><![CDATA[check bits]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[function example]]></category>
		<category><![CDATA[low level]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://codytaylor.org/?p=14122</guid>
		<description><![CDATA[PHP makes life a lot easier for quick or dirty maintenance scripts....]]></description>
			<content:encoded><![CDATA[<p>PHP makes life a lot easier for quick or dirty maintenance scripts, cron jobs or web applications but how does it do for older, not so straight forward problems dealing with bits and bytes? I was surprised how easy it was to manipulate bits in a byte with php. Here is an function that made my life a fair amount easier when having to check for a specific bit in a byte. </p>
<p>This function checks whether a certain bit is set or not given a byte and an index. It returns true if the chosen bit is set. It casts the $value argument to a integer just in case. The index $n goes from left to right so the most significant bit is bit one and the least significant is bit eight. This function will only work for integers between 0 and 255 because that was all I needed at the time. It would be trivial to write either a function to separate bytes in an integer or to increase the amount of bits that this function checks. I originally had a different function here but the Internet quickly told me that there was an easier way to do this.</p>
<p><pre><code>
&lt;?php

function check_bit($value,$n=8)
{
&nbsp;&nbsp;&nbsp;&nbsp;$value = (int)$value;
&nbsp;&nbsp;&nbsp;&nbsp;if($value &amp; (1&lt;&lt;(8-$n))) { return true; }
&nbsp;&nbsp;&nbsp;&nbsp;else { return false; }
}

//Check Bit Usage Example
$test_byte1 = &quot;4&quot;;

if( check_bit($test_byte1,2))
&nbsp;&nbsp;echo &quot;Bit 2 is set in &quot;.decbin($test_byte1);
else
&nbsp;&nbsp;echo &quot;Bit 2 is not set in &quot;.decbin($test_byte1);

?&gt;
</code></pre></p>
<p>Here are the php bitwise operator definitions from the php documentation. Look like C much?</p>
<table cellpadding=4 cellspacing=0 border='0'>
<tr style="border-bottom:1px solid grey;">
<td align='center' width='20%'>$a &#038; $b</td>
<td align='left'>And</td>
<td align='left'>Bits that are set in both $a and $b are set.</td>
</tr>
<tr style="border-bottom:1px solid grey;">
<td align='center'>$a | $b</td>
<td align='left'>Or</td>
<td align='left'>Bits that are set in either $a or $b are set.</td>
</tr>
<tr style="border-bottom:1px solid grey;">
<td align='center'>$a ^ $b</td>
<td align='left'>Xor</td>
<td align='left'>Bits that are set in $a or $b but not both are set.</td>
</tr>
<tr style="border-bottom:1px solid grey;">
<td align='center'>~ $a</td>
<td align='left'>Not</td>
<td align='left'>Bits that are set in $a are not set, and vice versa.</td>
</tr>
<tr style="border-bottom:1px solid grey;">
<td align='center'>$a << $b</td>
<td align='left'>Shift left</td>
<td align='left'>Shift the bits of $a $b steps to the left (each step means &#8220;multiply by two&#8221;)</td>
</tr>
<tr style="border-bottom:1px solid grey;">
<td align='center'>$a >> $b</td>
<td align='left'>Shift right</td>
<td align='left'>Shift the bits of $a $b steps to the right (each step means &#8220;divide by two&#8221;) </td>
</tr>
</table>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodytaylor.org%2F2009%2F06%2Fchecking-bits-with-php.html&amp;linkname=Checking%20Bits%20With%20PHP"><img src="http://codytaylor.org/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://codytaylor.org/2009/06/checking-bits-with-php.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

