<?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; bytes</title>
	<atom:link href="http://codytaylor.org/tag/bytes/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>Get Integer Bit Parity In PHP</title>
		<link>http://codytaylor.org/2009/06/get-integer-bit-parity-in-php.html</link>
		<comments>http://codytaylor.org/2009/06/get-integer-bit-parity-in-php.html#comments</comments>
		<pubDate>Sat, 20 Jun 2009 19:20:50 +0000</pubDate>
		<dc:creator>Cody Taylor</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[bits]]></category>
		<category><![CDATA[bytes]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://codytaylor.org/?p=14144</guid>
		<description><![CDATA[I was forced to use php to generate a cyclic redundancy..]]></description>
			<content:encoded><![CDATA[<p>I was forced to use php to generate a cyclic redundancy check (CRC) byte earlier this week. When generating this CRC I needed to check the bit parity of a byte. At first I thought that there would be an example that I could just cut and paste into my code or a library including the function somewhere. I guess that most PHP users don&#8217;t really have to work with bits and bytes very often because I couldn&#8217;t find an example of doing this. Granted I didn&#8217;t really search that hard because writing it was pretty simple. But hopefully the next guy searching for this actually finds this example.</p>
<p>Most of the time we don&#8217;t really have to think about types in php. But if you&#8217;re dealing with this kind of thing then you usually want to know what you&#8217;re working with. This php function example will only work with integers and will return a value of -1 if the first parameter is not a number. The function returns 1 if the number of bits set in the number pased in is odd. Otherwise it will return 0. The second parameter defaults to 4 because that is the number of bytes in a normal php integer. In php if a number becomes larger than an integer can handle, which is called an overflow, it becomes a float. The default integer size is platform dependant but it&#8217;s usually 4 bytes. It can be set using the constant PHP_INT_SIZE if you need to change it.</p>
<p><pre><code>
&lt;?php

function odd_parity($something,$number_of_bytes_to_check=4,$double_check=0)
{
&nbsp;&nbsp;&nbsp;&nbsp;//test to make sure it&#039;s a number
&nbsp;&nbsp;&nbsp;&nbsp;if(!is_numeric($something)) {return -1;}&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;$setbits = 0;

&nbsp;&nbsp;&nbsp;&nbsp;//get the number of bits that are set
&nbsp;&nbsp;&nbsp;&nbsp;for($i=0;$i&lt;$number_of_bytes_to_check*8;$i++)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($something &amp; (1&lt;&lt;$i)) $setbits++;
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;//check if the number of bits set is even or odd&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;$result =&nbsp;&nbsp;$setbits % 2;
&nbsp;&nbsp; 
&nbsp;&nbsp;&nbsp;&nbsp;//spit out the results to double check it&#039;s working 
&nbsp;&nbsp;&nbsp;&nbsp;if($double_check)
&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;&nbsp; &quot;&lt;br&gt;Parity of : &quot;.$something.&quot; is &quot;.
&nbsp;&nbsp;&nbsp;&nbsp;$result.&quot; In Binary : &quot;.decbin((int)$something).
&nbsp;&nbsp;&nbsp;&nbsp;&quot; Checking the right &quot;.($number_of_bytes_to_check*8).
&nbsp;&nbsp;&nbsp;&nbsp;&quot; Bits&quot;;

&nbsp;&nbsp;&nbsp;&nbsp;return $result;
}

//will return 0 because it&#039;s defaulting to 4 bytes
echo odd_parity(0x0100010001);&nbsp;&nbsp;

?&gt;
</code></pre></p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fcodytaylor.org%2F2009%2F06%2Fget-integer-bit-parity-in-php.html&amp;linkname=Get%20Integer%20Bit%20Parity%20In%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/get-integer-bit-parity-in-php.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

