<?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>Ramunas Geciauskas &#187; bitwise</title>
	<atom:link href="http://geciauskas.com/tag/bitwise/feed" rel="self" type="application/rss+xml" />
	<link>http://geciauskas.com</link>
	<description>Imagination is more important than knowledge</description>
	<lastBuildDate>Sun, 13 Jun 2010 06:02:13 +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>Efficiency of Bitwise Operations in C#</title>
		<link>http://geciauskas.com/2010/programming/efficiency-of-bitwise-operations-in-c</link>
		<comments>http://geciauskas.com/2010/programming/efficiency-of-bitwise-operations-in-c#comments</comments>
		<pubDate>Mon, 01 Feb 2010 13:44:22 +0000</pubDate>
		<dc:creator>ramas</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[bitwise]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://geciauskas.com/?p=29</guid>
		<description><![CDATA[Today I found great article in Polygonal.de blog about useful bitwise operations and attempted to determine how effective those &#8220;tricks&#8221; are in C# .NET. Note: - All test were performed 1000000000 times with same values of all variables - Difference was calculated using [(Method1_Time/Method2_Time)*100%] equation - >100% difference indicates that bitwise operation was faster Absolute [...]]]></description>
			<content:encoded><![CDATA[<p>Today I found great article in <a href="http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/">Polygonal.de</a> blog about useful bitwise operations and attempted to determine how effective those &#8220;tricks&#8221; are in C# .NET.</p>
<p><i>Note:<br />
- All test were performed 1000000000 times with same values of all variables</p>
<p>- Difference was calculated using [(Method1_Time/Method2_Time)*100%] equation<br />
- >100% difference indicates that bitwise operation was faster<br />
</i></p>
<p><strong>Absolute value of an integer</strong></p>
<pre>
x = i + (i >> 31) ^ (i >> 31);
versus
x = Math.Abs(x);

<i>
Basic method:   6.51467910027671
Bitwise method: 2.73749624603127

Difference:     238%
</i>
</pre>
<p>Note: This particular example only Works with 32 bit integers<br />
Universal code would look like this: [i + (i >> INT_SIZE-1) ^ (i >> INT_SIZE-1]</p>
<p><strong>Multiplication by any power of two</strong></p>
<pre>
x = x * 256;
versus
x = x << 8;

<i>
Basic method:   3.54274747209492
Bitwise method: 2.95297894006082

Difference:     120%
</i>
</pre>
<p><strong>Division by any power of two</strong></p>
<pre>
x = x / 256;
versus
x = x >> 8;

<i>
Basic method:   3.05552884514652
Bitwise method: 3.53027912765449

Difference:     87%
</i>
</pre>
<p>To my surprise, bitwise multiplication was faster, while division was slower (almost the same amount).</p>
<p><strong>Swapping integers without additional temporary variable</strong></p>
<pre>
int a = x;
x = y;
y = a;

versus

x ^= y;
y ^= x;
x ^= y;

<i>
Basic method:   4.45572132771064
Bitwise method: 4.04139101477981

Difference:     110%
</i>
</pre>
<p>My favorite bitwise operation, which I use quite often. Although it&#8217;s only 10% faster, but it doesn&#8217;t require creation of a new temporary integer, uses less memory therefore there&#8217;s no need calling .NET garbage collector at the end.</p>
<p><strong>Increase/Decrease integer by one</strong></p>
<pre>
x++;
x--;

versus

x = -~x;
x = ~-x;

<i>
Basic method:   3.44019505272318
Bitwise method: 3.70579836264106

Difference:     93%
</i>
</pre>
<p>Both x++ and x&#8211; are very frequent operations, so no wonder that compilers use all their potential to optimize them as good as they can. There&#8217;s no point using bitwise operations here.</p>
<p><strong>Flipping sign of an integer</strong></p>
<pre>
x = -x;
versus
x = ~x + 1;

<i>
Basic method:   3.07309755848858
Bitwise method: 3.05791937243421

Difference:     100%
</i>
</pre>
<p>Bitwise operation provides almost identical speed, so I don&#8217;t see why to complicate your code with bitwise analog of simple [x = -x] code.</p>
<p><strong>Modulo operation with divisors of any power by two</strong></p>
<pre>
x = x % 32;
versus
x = x &#038; (32 - 1);

<i>
Basic method:   2.65090814614707
Bitwise method: 3.55706940407231

Difference:     75%
</i>
</pre>
<p>The only bitwise operation that was a lot slower then simple [x % c] way.</p>
<p>Further reading:<br />
&bull; <a href="http://cmschill.net/stringtheory/2008/04/bitwise-operators/">cmschill.net</a><br />
&bull; <a href="http://avim.veneroida.com/?p=29">veneroida.com</a><br />
&bull; <a href="http://en.wikipedia.org/wiki/Bitwise_operation">wikipedia.org</a></p>
]]></content:encoded>
			<wfw:commentRss>http://geciauskas.com/2010/programming/efficiency-of-bitwise-operations-in-c/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
