<?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>philihp.com &#187; Bitwise</title>
	<atom:link href="http://www.philihp.com/blog/tag/bitwise/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.philihp.com/blog</link>
	<description>I do things, and then I tell the internet about them.</description>
	<lastBuildDate>Mon, 06 Feb 2012 05:40:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Reverse the Bits in a Byte in 3 operations</title>
		<link>http://www.philihp.com/blog/2009/reverse-the-bits-in-a-byte-in-3-operations/</link>
		<comments>http://www.philihp.com/blog/2009/reverse-the-bits-in-a-byte-in-3-operations/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 15:39:44 +0000</pubDate>
		<dc:creator>philihp</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Bitwise]]></category>
		<category><![CDATA[Optimization]]></category>

		<guid isPermaLink="false">http://www.philihp.com/blog/?p=404</guid>
		<description><![CDATA[This is among many brilliant hacks from this page. b = (b * 0x0202020202ULL &#038; 0x010884422010ULL) % 1023; The multiply operation creates five separate copies of the 8-bit byte pattern to fan-out into a 64-bit value. The AND operation selects the bits that are in the correct (reversed) positions, relative to each 10-bit groups of [...]]]></description>
			<content:encoded><![CDATA[<p>This is among many brilliant hacks from <a href="http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith64BitsDiv">this</a> page.</p>
<pre>b = (b * 0x0202020202ULL &#038; 0x010884422010ULL) % 1023;</pre>
<blockquote><p>The multiply operation creates five separate copies of the 8-bit byte pattern to fan-out into a 64-bit value. The AND operation selects the bits that are in the correct (reversed) positions, relative to each 10-bit groups of bits. The multiply and the AND operations copy the bits from the original byte so they each appear in only one of the 10-bit sets. The reversed positions of the bits from the original byte coincide with their relative positions within any 10-bit set. The last step, which involves modulus division by 2^10 &#8211; 1, has the effect of merging together each set of 10 bits (from positions 0-9, 10-19, 20-29, &#8230;) in the 64-bit value. They do not overlap, so the addition steps underlying the modulus division behave like or operations. </p></blockquote>
<p>The genius here, I think, is the modulus division to compress eight specific bits from a 64-bit number down into an 8-bit byte.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.philihp.com/blog/2009/reverse-the-bits-in-a-byte-in-3-operations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

