﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-内核开发空间-随笔分类-optimization</title><link>http://www.cnitblog.com/syq/category/5377.html</link><description /><language>zh-cn</language><lastBuildDate>Sat, 01 Oct 2011 04:16:43 GMT</lastBuildDate><pubDate>Sat, 01 Oct 2011 04:16:43 GMT</pubDate><ttl>60</ttl><item><title>BIOS Types, CHS Translation, LBA and Other Good Stuff</title><link>http://www.cnitblog.com/syq/archive/2007/05/18/27199.html</link><dc:creator>yongqing</dc:creator><author>yongqing</author><pubDate>Fri, 18 May 2007 07:34:00 GMT</pubDate><guid>http://www.cnitblog.com/syq/archive/2007/05/18/27199.html</guid><wfw:comment>http://www.cnitblog.com/syq/comments/27199.html</wfw:comment><comments>http://www.cnitblog.com/syq/archive/2007/05/18/27199.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/syq/comments/commentRss/27199.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/syq/services/trackbacks/27199.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: &nbsp;&nbsp;<a href='http://www.cnitblog.com/syq/archive/2007/05/18/27199.html'>阅读全文</a><img src ="http://www.cnitblog.com/syq/aggbug/27199.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/syq/" target="_blank">yongqing</a> 2007-05-18 15:34 <a href="http://www.cnitblog.com/syq/archive/2007/05/18/27199.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>bitwise operations</title><link>http://www.cnitblog.com/syq/archive/2007/05/18/27189.html</link><dc:creator>yongqing</dc:creator><author>yongqing</author><pubDate>Fri, 18 May 2007 01:29:00 GMT</pubDate><guid>http://www.cnitblog.com/syq/archive/2007/05/18/27189.html</guid><wfw:comment>http://www.cnitblog.com/syq/comments/27189.html</wfw:comment><comments>http://www.cnitblog.com/syq/archive/2007/05/18/27189.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/syq/comments/commentRss/27189.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/syq/services/trackbacks/27189.html</trackback:ping><description><![CDATA[<h3>Bitwise Operations</h3>
<p>It's fairly convoluted though very efficient to encode lots of
pieces of Boolean information into a single integer variable. Normal
arithmetic and logical operations on such integer variables become
unintuitive since we're not usually concerned with the value of the
variables (e.g., 17) but rather with the status of individual bits
(say, the first and fifth bits are set, all other bits are unset).</p>
<p>
This page presents some bitwise operations I found useful while writing
an extremely CPU-intensive ray-tracer. C syntax is used but the
techniques can be applied to other languages including Java, often with
only minor syntax changes.
</p>
<h4>Tests</h4>
<ul>
    <li>
    <p>Is a power of two:<br><br><code><font color="blue">&nbsp;&nbsp;&nbsp;&nbsp;v &amp; (v-1) == 0</font></code><br><br><br></p>
    </li>
    <li>
    <p>Has two or more set bits:<br><br><code><font color="blue">&nbsp;&nbsp;&nbsp;&nbsp;v &amp; (v-1) &gt; 0</font></code><br><br><br></p>
    </li>
    <li>
    <p>Has exactly one set bit:<br><br><code><font color="blue">&nbsp;&nbsp;&nbsp;&nbsp;v &amp;&amp; (v &amp; (v-1) == 0) == true</font></code><br><br><br></p>
    </li>
    <li>
    <p>Has all specified bits set:<br><br><code><font color="blue">&nbsp;&nbsp;&nbsp;&nbsp;v &amp; bits == bits</font></code><br><br><br></p>
    </li>
    <li>
    <p>Has at least one specified bit set:<br><br><code><font color="blue">&nbsp;&nbsp;&nbsp;&nbsp;v &amp; bits &gt; 0</font></code><br><br><br></p>
    </li>
</ul>
<h4>Operations</h4>
<ul>
    <li>
    <p>Toggle specific bits:<br><br><code><font color="blue">&nbsp;&nbsp;&nbsp;&nbsp;v ^= b;</font></code><br><br><br></p>
    </li>
    <li>
    <p>Set a bit:<br><br><code><font color="blue">&nbsp;&nbsp;&nbsp;&nbsp;v |= b;</font></code><br><br><br></p>
    </li>
    <li>
    <p>Unset a bit:<br><br><code><font color="blue">&nbsp;&nbsp;&nbsp;&nbsp;v &amp;= ~b;</font></code><br><br><br></p>
    </li>
    <li>
    <p>Unset all bits apart from lowest set bit:<br><br><code><font color="blue">&nbsp;&nbsp;&nbsp;&nbsp;((v ^ (v-1)) + 1) &gt;&gt; 1</font></code><br><br><br></p>
    </li>
    <li>
    <p>Unset the lowest set bit only:<br><br><code><font color="blue">&nbsp;&nbsp;&nbsp;&nbsp;v &amp;= (v-1)</font></code><br><br><br></p>
    </li>
</ul>
<h4>Other</h4>
<ul>
    <li>
    <p>Count number of set bits:<br><br><code><font color="blue">&nbsp;&nbsp;&nbsp;&nbsp;int n = 0; if (v) do { n++; } while (v &amp;= (v-1)); return(n);</font></code><br><br><br></p>
    </li>
    <li>
    <p>Compute log2 of a power of two:<br><br><code><font color="blue">&nbsp;&nbsp;&nbsp;&nbsp;float x=v; return (*(int*)&amp;x &gt;&gt; 23) - 127;</font></code><br><br><br></p>
    </li>
    <li>
    <p>Compute minimum of two signed 32-bit integers:<br><br><code><font color="blue">&nbsp;&nbsp;&nbsp;&nbsp;j + (((i-j) &gt;&gt; 31) &amp; (i-j))</font></code></p>
    </li>
</ul><img src ="http://www.cnitblog.com/syq/aggbug/27189.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/syq/" target="_blank">yongqing</a> 2007-05-18 09:29 <a href="http://www.cnitblog.com/syq/archive/2007/05/18/27189.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>