<?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>My Workday &#187; Bitwise Operation</title>
	<atom:link href="http://www.goworkday.com/tag/bitwise-operation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.goworkday.com</link>
	<description>Let&#039;s talk TECH</description>
	<lastBuildDate>Tue, 23 Mar 2010 03:28:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Single-word Wang/Jenkins Hash in ConcurrentHashMap</title>
		<link>http://www.goworkday.com/2010/03/19/single-word-wangjenkins-hash-concurrenthashmap/</link>
		<comments>http://www.goworkday.com/2010/03/19/single-word-wangjenkins-hash-concurrenthashmap/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 00:23:16 +0000</pubDate>
		<dc:creator>Admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Bitwise Operation]]></category>
		<category><![CDATA[hash]]></category>

		<guid isPermaLink="false">http://www.goworkday.com/?p=244</guid>
		<description><![CDATA[<br/>ConcurrentHashMap is hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates. I recently came across this code during testing, and one part really got my attention. To generate the hash, ConcurrentHashMap uses an algorithm based on bitshifting and bitwise operations. ?View Code JAVA1 2 3 4 5 6 7 8 9 [...]]]></description>
			<content:encoded><![CDATA[<br/><p>ConcurrentHashMap is hash table supporting full concurrency of retrievals and adjustable expected concurrency for updates. I recently came across this code during testing, and one part really got my attention. To generate the hash, ConcurrentHashMap uses an algorithm based on bitshifting and bitwise operations.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p244code6'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2446"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code" id="p244code6"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">========================================</span>
Variant of single<span style="color: #339933;">-</span>word Wang<span style="color: #339933;">/</span>Jenkins hash
<span style="color: #339933;">========================================</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> hash<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> h<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Spread bits to regularize both segment and index locations,</span>
<span style="color: #666666; font-style: italic;">// using variant of single-word Wang/Jenkins hash.</span>
h <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&lt;&lt;</span>  <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">^</span> 0xffffcd7d<span style="color: #339933;">;</span>
h <span style="color: #339933;">^=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
h <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&lt;&lt;</span>   <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
h <span style="color: #339933;">^=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&gt;&gt;&gt;</span>  <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
h <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&lt;&lt;</span>   <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">14</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">return</span> h <span style="color: #339933;">^</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>According to the comment in the code, this method applies a supplemental hash function to a given hashCode, which defends against poor quality hash functions.  </p>
<p>Good hash functions are important as a hash table effectively turns from a map to a linked list, in the worst case, all keys in the same bucket. There are also other considerations that come into play such as the performance of hash calculation and the number of buckets. Dr. Heinz M. Kabutz explains the power of <a href="http://www.javaspecialists.co.za/archive/Issue054.html">&#8220;power-of-two number of buckets&#8221;</a> which gives us some good starting point to understand what is really going on here. </p>
<p>Let&#8217;s look at the code above and see how things change, line-by-line. To make things simple,  I use <strong>int 1</strong> to perform all the operations. </p>
<p>In Java, the <strong> int</strong> data type is a 32-bit signed two&#8217;s complement integer. To represent int 1 in binary code, we have the following:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p244code7'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2447"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p244code7"><pre class="java" style="font-family:monospace;">h<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&gt;</span> 0000<span style="color: #339933;">-</span>0000<span style="color: #339933;">-</span>0000<span style="color: #339933;">-</span>0000<span style="color: #339933;">-</span>0000<span style="color: #339933;">-</span>0000<span style="color: #339933;">-</span>0001</pre></td></tr></table></div>

<p>Now, let&#8217;s dissect the following line:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p244code8'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2448"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p244code8"><pre class="java" style="font-family:monospace;">h <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">^</span> 0xffffcd7d</pre></td></tr></table></div>

<p>First, let&#8217;s re-write this into an easier-to-read format.. at least for me <img src='http://www.goworkday.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' title=" Single word Wang/Jenkins Hash in ConcurrentHashMap" /> .</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p244code9'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2449"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p244code9"><pre class="java" style="font-family:monospace;">h1 <span style="color: #339933;">=</span> h <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">15</span>      <span style="color: #339933;">=</span>  0000<span style="color: #339933;">-</span>0000<span style="color: #339933;">-</span>0000<span style="color: #339933;">-</span>0000<span style="color: #339933;">-</span><span style="color: #cc66cc;">1000</span><span style="color: #339933;">-</span>0000<span style="color: #339933;">-</span>0000<span style="color: #339933;">-</span>0000
hex <span style="color: #339933;">=</span> 0xffffcd7d  <span style="color: #339933;">=</span>  <span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1100</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1101</span><span style="color: #339933;">-</span>0111<span style="color: #339933;">-</span><span style="color: #cc66cc;">1101</span>
h2 <span style="color: #339933;">=</span> h1 <span style="color: #339933;">^</span> hex     <span style="color: #339933;">=</span>  <span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span>0100<span style="color: #339933;">-</span><span style="color: #cc66cc;">1101</span><span style="color: #339933;">-</span>0111<span style="color: #339933;">-</span><span style="color: #cc66cc;">1101</span>
h2 <span style="color: #339933;">+</span> h            <span style="color: #339933;">=</span>  <span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span>0100<span style="color: #339933;">-</span><span style="color: #cc66cc;">1101</span><span style="color: #339933;">-</span>0111<span style="color: #339933;">-</span><span style="color: #cc66cc;">1110</span></pre></td></tr></table></div>

<p>Using the same thought processing and applying it to each line, we end-up with:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p244code10'); return false;">View Code</a> JAVA</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p24410"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p244code10"><pre class="java" style="font-family:monospace;">h <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">15</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">^</span> 0xffffcd7d <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span>0100<span style="color: #339933;">-</span><span style="color: #cc66cc;">1101</span><span style="color: #339933;">-</span>0111<span style="color: #339933;">-</span><span style="color: #cc66cc;">1110</span>
h <span style="color: #339933;">^=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span>	            <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1100</span><span style="color: #339933;">-</span>0000<span style="color: #339933;">-</span><span style="color: #cc66cc;">1011</span><span style="color: #339933;">-</span>0010<span style="color: #339933;">-</span><span style="color: #cc66cc;">1010</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1101</span>
h <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span>		    <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1101</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1100</span><span style="color: #339933;">-</span>0110<span style="color: #339933;">-</span>0100<span style="color: #339933;">-</span><span style="color: #cc66cc;">1000</span><span style="color: #339933;">-</span>0001<span style="color: #339933;">-</span>0101
h <span style="color: #339933;">^=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span>              <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1111</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1110</span><span style="color: #339933;">-</span>0011<span style="color: #339933;">-</span>0001<span style="color: #339933;">-</span>0101<span style="color: #339933;">-</span>0001<span style="color: #339933;">-</span>0011<span style="color: #339933;">-</span>0101
h <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&lt;&lt;</span> <span style="color: #cc66cc;">14</span><span style="color: #009900;">&#41;</span>   <span style="color: #339933;">=</span> 0100<span style="color: #339933;">-</span><span style="color: #cc66cc;">1011</span><span style="color: #339933;">-</span>0100<span style="color: #339933;">-</span>0011<span style="color: #339933;">-</span><span style="color: #cc66cc;">1101</span><span style="color: #339933;">-</span>0110<span style="color: #339933;">-</span>0000<span style="color: #339933;">-</span><span style="color: #cc66cc;">1001</span>
h <span style="color: #339933;">^=</span> <span style="color: #009900;">&#40;</span>h <span style="color: #339933;">&gt;&gt;&gt;</span> <span style="color: #cc66cc;">16</span><span style="color: #009900;">&#41;</span>             <span style="color: #339933;">=</span> 0100<span style="color: #339933;">-</span><span style="color: #cc66cc;">1011</span><span style="color: #339933;">-</span>0100<span style="color: #339933;">-</span>0011<span style="color: #339933;">-</span><span style="color: #cc66cc;">1001</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1101</span><span style="color: #339933;">-</span>0100<span style="color: #339933;">-</span><span style="color: #cc66cc;">1010</span></pre></td></tr></table></div>

<p>Result:<br />
Bin          = 0100-1011-0100-0011-1001-1101-0100-1010<br />
Decimal = 1,262,722,378</p>
]]></content:encoded>
			<wfw:commentRss>http://www.goworkday.com/2010/03/19/single-word-wangjenkins-hash-concurrenthashmap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bitwise Operations</title>
		<link>http://www.goworkday.com/2008/10/09/bitwise-operations/</link>
		<comments>http://www.goworkday.com/2008/10/09/bitwise-operations/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 04:55:34 +0000</pubDate>
		<dc:creator>Admin</dc:creator>
				<category><![CDATA[J2EE]]></category>
		<category><![CDATA[Bitwise Operation]]></category>

		<guid isPermaLink="false">http://www.goworkday.com/?p=30</guid>
		<description><![CDATA[<br/>AND A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. In each pair, the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise, the result is 0. For example: 0101 AND 0011 = 0001 XOR [...]]]></description>
			<content:encoded><![CDATA[<br/><h3><span class="mw-headline">AND</span></h3>
<p>A <strong>bitwise AND</strong> takes two binary representations of equal length and performs the logical <a title="Logical conjunction" href="http://en.wikipedia.org/wiki/Logical_conjunction">AND</a> operation on each pair of corresponding bits. In each pair, the result is 1 if the first bit is 1 <strong>AND</strong> the second bit is 1. Otherwise, the result is 0. For example:<br />
<code><br />
0101<br />
AND 0011<br />
= 0001<br />
</code></p>
<h3><span class="mw-headline">XOR</span></h3>
<p>A <strong>bitwise exclusive or</strong> takes two bit patterns of equal length and performs the logical <a class="mw-redirect" title="Exclusive disjunction" href="http://en.wikipedia.org/wiki/Exclusive_disjunction">XOR</a> operation on each pair of corresponding bits. The result in each position is 1 if the two bits are different, and 0 if they are the same. For example:<br />
<code><br />
0101<br />
XOR 0011<br />
= 0110</code></p>
<p>In <a title="Java (programming language)" href="http://en.wikipedia.org/wiki/Java_%28programming_language%29">Java</a>, all integer types are signed, and the &#8220;<code>&lt;&lt;</code>&#8221; and &#8220;<code>&gt;&gt;</code>&#8221; operators perform arithmetic shifts. Java adds the operator &#8220;<code>&gt;&gt;&gt;</code>&#8221; to perform logical right shifts, but since the logical and arithmetic left-shift operations are identical, there is no &#8220;<code>&lt;&lt;&lt;</code>&#8221; operator in Java. These general rules are affected in several ways by the default <a class="new" title="Type promotion (page does not exist)" href="http://en.wikipedia.org/w/index.php?title=Type_promotion&amp;action=edit&amp;redlink=1">type promotions</a>; for example, since the eight-bit type <code>byte </code>is promoted to <code>int</code> in shift-expressions,<sup id="cite_ref-1" class="reference"><a href="http://en.wikipedia.org/wiki/Bitwise_operation#cite_note-1">[2]</a></sup> the expression &#8220;<code>b &gt;&gt;&gt; 2</code>&#8221; effectively performs an arithmetic shift of the byte value <code>b</code> instead of a logical shift. Such effects can be mitigated by judicious use of <a title="Type conversion" href="http://en.wikipedia.org/wiki/Type_conversion">casts</a> or <a class="mw-redirect" title="Bitmask" href="http://en.wikipedia.org/wiki/Bitmask">bitmasks</a>; for example, &#8220;<code>(b &amp; 0xFF) &gt;&gt;&gt; 2</code>&#8221; effectively results in a logical shift.<a href="http://www.goworkday.com/wp-content/uploads/2008/10/bitwise.gif"><img class="alignnone size-medium wp-image-33" title="bitwise" src="http://www.goworkday.com/wp-content/uploads/2008/10/bitwise.gif" alt="bitwise Bitwise Operations" width="277" height="133" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.goworkday.com/2008/10/09/bitwise-operations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
