<?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>Meseret Gebre &#187; Bitwise operators</title>
	<atom:link href="http://meseretgebre.com/archives/tag/bitwise-operators/feed/" rel="self" type="application/rss+xml" />
	<link>http://meseretgebre.com</link>
	<description></description>
	<lastBuildDate>Fri, 04 Jun 2010 07:15:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Bitwise Operators</title>
		<link>http://meseretgebre.com/archives/bitwise-operators/</link>
		<comments>http://meseretgebre.com/archives/bitwise-operators/#comments</comments>
		<pubDate>Fri, 20 Mar 2009 02:48:21 +0000</pubDate>
		<dc:creator>mez</dc:creator>
				<category><![CDATA[HPDC]]></category>
		<category><![CDATA[Bitwise operators]]></category>
		<category><![CDATA[Optimize]]></category>

		<guid isPermaLink="false">http://meseretgebre.com/?p=8</guid>
		<description><![CDATA[Bitwise operators let you manipulate individual bits. Normally I would not bother writing about this, but I believe knowing about bit wise operation is an important asset to have when trying to optimize code. Especially in memory consumption. Keep in mind that all bitwise operations  in C only work of singed and unsigned integer primitive [...]]]></description>
			<content:encoded><![CDATA[<p>Bitwise operators let you manipulate individual bits. Normally I would not bother writing about this, but I believe knowing about bit wise operation is an important asset to have when trying to optimize code. Especially in memory consumption. Keep in mind that all bitwise operations  in C only work of singed and unsigned integer primitive data types, mainly:</p>
<ol>
<li>char</li>
<li>short</li>
<li>int</li>
<li>long</li>
</ol>
<p><strong>Article outline</strong><br />
I&#8217;ll talk about each of the following operations and give some examples on how to use each.</p>
<ol>
<li>Bitwise NOT (~)</li>
<li>Bitwise AND (&amp;)</li>
<li>Bitwise OR (|)</li>
<li>Bitwise XOR (^)</li>
<li>Shift Left (&lt;&lt;)</li>
<li>Shift Right(&gt;&gt;)</li>
</ol>
<p><strong>Bitwise NOT (~)</strong><br />
This is an unary operator. It simply filps all &#8216;0&#8217;s to &#8216;1&#8217;s and &#8216;1&#8217;s to &#8216;0&#8217;s. Here is a simple example:</p>
<pre class="brush: c">
unsigned byte x = 10;
x = 00001010
~x = 11110101
</pre>
<p><strong>Bitwise AND (&amp;)</strong><br />
This is a binary operator. Just follow the truth table. Both bits have to be true to get a resulting true bit. Anything else is false. Here is a simple example:</p>
<pre class="brush: c">
unsigned byte x = (10&amp;8);
10  = 00001010
8   = 00001000
x   = 00001000
</pre>
<p><strong>Bitwise OR (|)</strong><br />
This is a binary operator. Just follow the truth table. Here, just one of the bits has to be true to get a resulting true. Here is a simple example:</p>
<pre class="brush: c">
unsigned byte x = (10|8);
10  = 00001010
8   = 00001000
x   = 00001010
</pre>
<p style="text-align: left;"><strong>Bitwise XOR (^)</strong><br />
This is a binary operator. Just follow the truth table. Basically it flags all bits that are different. Here is a simple example:</p>
<pre class="brush: c">
unsigned byte x = (10^15);
10  = 00001010
15  = 00001111
x   = 00000101
</pre>
<p><strong>Shift Left (&lt;&lt;)</strong><br />
Keep in mind that when you do one of the shift operators the size of the data type does not change.<br />
A left shift, moves the bits to the left inserting &#8216;0&#8217;s in the right most bit for every shift. The left most bit is discarded. This is effectively multiplying the number by 2. Note if you use too many shifts you could end up with a negative number. This is possible if the data type is interpreted as 2&#8217;s complement. Here is a simple example:</p>
<pre class="brush: c">
unsigned byte x = 4 &lt;&lt; 2;
4   = 00000100
x   = 00010000
x = 16
</pre>
<p><strong>Shift Right (&gt;&gt;)</strong><br />
A rightshift, moves the bits to the right inserting &#8216;0&#8217;s in the left most bit for every shift. The right most bit is discarded. This is effectively dividing the number by 2. Note if you use too many shifts you could end up with a zero. Here is a simple example:</p>
<pre class="brush: c">
unsigned byte x = 4 &gt;&gt; 2;
4   = 00000100
x   = 00000001
x = 1
</pre>
<p>That wraps up the bitwise operators. If there is anything I forgot, let me know and I&#8217;ll make the updates. I hope you learned something in this read. Remember these are operation which the processor can execute very fast. For example, if you every need to multiple or divide by 2, it best to use the shift operations. Not only will you code execute faster, but also save you processor some energy!</p>
]]></content:encoded>
			<wfw:commentRss>http://meseretgebre.com/archives/bitwise-operators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
