<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title type="text">博客园_真 OO无双 之 真乱舞书</title><subtitle type="text">寫程式是很快樂的一件事 Since Sep.15,2006</subtitle><id>http://feed.cnblogs.com/blog/u/21379/rss</id><updated>2012-04-15T14:29:42Z</updated><author><name>真 OO无双</name><uri>http://www.cnblogs.com/oomusou/</uri></author><generator>feed.cnblogs.com</generator><link rel="alternate" type="text/html" href="http://www.cnblogs.com/oomusou/"/><link rel="self" type="application/atom+xml" href="http://feed.cnblogs.com/blog/u/21379/rss"/><entry><id>http://www.cnblogs.com/oomusou/archive/2012/03/01/bit_hacks.html</id><title type="text">(轉貼) Bit Twiddling Hacks (SOC) (Verilog) (C)</title><summary type="text">無論寫RTL或者寫firmware，巧妙的Bit運算均非常有效率。轉貼一篇Standford大學Sean Eron Anderson所整理的一些Bit運算Hacks。</summary><published>2012-03-01T14:42:00Z</published><updated>2012-03-01T14:42:00Z</updated><author><name>真 OO无双</name><uri>http://www.cnblogs.com/oomusou/</uri></author><link rel="alternate" href="http://www.cnblogs.com/oomusou/archive/2012/03/01/bit_hacks.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/oomusou/archive/2012/03/01/bit_hacks.html"/><content type="html">&lt;p&gt;&lt;strong&gt;Abstract&lt;/strong&gt;&lt;br /&gt;無論寫RTL或者寫firmware，巧妙的Bit運算均非常有效率。轉貼一篇Standford大學Sean Eron Anderson所整理的一些Bit運算Hacks。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br /&gt;轉貼自Sean Eron Anderson的&lt;a href="http://graphics.stanford.edu/~seander/bithacks.html" target="_blank"&gt;Bit Twiddling Hacks&lt;/a&gt;，僅對HTML做一些排版整理。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Bit Twiddling Hacks&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;By Sean Eron Anderson&lt;br /&gt;seander@cs.&lt;!-- NO SPAM! --&gt;stanford.edu&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;small&gt;Individually, the &lt;strong&gt;code snippets here are in the public domain&lt;/strong&gt; (unless otherwise noted) &amp;mdash; feel free to use them however you please. The aggregate collection and descriptions are &amp;copy; 1997-2005 Sean Eron Anderson. The code and descriptions are distributed in the hope that they will be useful, but &lt;strong&gt;WITHOUT ANY WARRANTY&lt;/strong&gt; and without even the implied warranty of merchantability or fitness for a particular purpose. As of May 5, 2005, all the code has been tested thoroughly. Thousands of people have read it. Moreover, &lt;a href="http://www-2.cs.cmu.edu/%7Ebryant/"&gt;Professor Randal Bryant&lt;/a&gt;, the Dean of Computer Science at Carnegie Mellon University, has personally tested almost everything with his &lt;a href="http://www-2.cs.cmu.edu/%7Euclid/"&gt;Uclid code verification system&lt;/a&gt;. What he hasn't tested, I have checked against all possible inputs on a 32-bit machine. &lt;strong&gt;To the first person to inform me of a legitimate bug in the code, I'll pay a bounty of US$10 (by check or Paypal)&lt;/strong&gt;. If directed to a charity, I'll pay US$20. &lt;/small&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Contents&lt;/strong&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="#OperationCounting"&gt;About the operation counting methodology&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#CopyIntegerSign"&gt;Compute the sign of an integer&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#DetectOppositeSigns"&gt;Detect if two integers have opposite signs &lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#IntegerAbs"&gt;Compute the integer absolute value (abs) without branching&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#IntegerMinOrMax"&gt;Compute the minimum (min) or maximum (max) of two integers without branching&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#DetermineIfPowerOf2"&gt;Determining if an integer is a power of 2&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Sign extending&lt;ul&gt;&lt;li&gt;&lt;a href="#FixedSignExtend"&gt;Sign extending from a constant bit-width&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#VariableSignExtend"&gt;Sign extending from a variable bit-width&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#VariableSignExtendRisky"&gt;Sign extending from a variable bit-width in 3 operations&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ConditionalSetOrClearBitsWithoutBranching"&gt;Conditionally set or clear bits without branching&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ConditionalNegate"&gt;Conditionally negate a value without branching&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#MaskedMerge"&gt;Merge bits from two values according to a mask&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Counting bits set&lt;ul&gt;&lt;li&gt;&lt;a href="#CountBitsSetNaive"&gt;Counting bits set, naive way&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#CountBitsSetTable"&gt;Counting bits set by lookup table&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#CountBitsSetKernighan"&gt;Counting bits set, Brian Kernighan's way&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#CountBitsSet64"&gt;Counting bits set in 12, 24, or 32-bit words using 64-bit instructions&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#CountBitsSetParallel"&gt;Counting bits set, in parallel&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#CountBitsFromMSBToPos"&gt;Count bits set (rank) from the most-significant bit upto a given position&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#SelectPosFromMSBRank"&gt;Select the bit position (from the most-significant bit) with the given count (rank)&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Computing parity (1 if an odd number of bits set, 0 otherwise)&lt;ul&gt;&lt;li&gt;&lt;a href="#ParityNaive"&gt;Compute parity of a word the naive way&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ParityLookupTable"&gt;Compute parity by lookup table&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ParityWith64Bits"&gt;Compute parity of a byte using 64-bit multiply and modulus division&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ParityMultiply"&gt;Compute parity of word with a multiply&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ParityParallel"&gt;Compute parity in parallel&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Swapping Values&lt;ul&gt;&lt;li&gt;&lt;a href="#SwappingValuesSubAdd"&gt;Swapping values with subtraction and addition&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#SwappingValuesXOR"&gt;Swapping values with XOR&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#SwappingBitsXOR"&gt;Swapping individual bits with XOR&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Reversing bit sequences&lt;ul&gt;&lt;li&gt;&lt;a href="#BitReverseObvious"&gt;Reverse bits the obvious way&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#BitReverseTable"&gt;Reverse bits in word by lookup table&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ReverseByteWith64BitsDiv"&gt;Reverse the bits in a byte with 3 operations (64-bit multiply and modulus division)&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ReverseByteWith64Bits"&gt;Reverse the bits in a byte with 4 operations (64-bit multiply, no division)&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ReverseByteWith32Bits"&gt;Reverse the bits in a byte with 7 operations (no 64-bit, only 32)&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ReverseParallel"&gt;Reverse an N-bit quantity in parallel with 5 * lg(N) operations&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Modulus division (aka computing &lt;em&gt;remainders&lt;/em&gt;)&lt;ul&gt;&lt;li&gt;&lt;a href="#ModulusDivisionEasy"&gt;Computing modulus division by 1 &amp;lt;&amp;lt; s without a division operation (obvious)&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ModulusDivision"&gt;Computing modulus division by (1 &amp;lt;&amp;lt; s) - 1 without a division operation&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ModulusDivisionParallel"&gt;Computing modulus division by (1 &amp;lt;&amp;lt; s) - 1 in parallel without a division operation&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Finding integer log base 2 of an integer (aka the position of the highest bit set)&lt;ul&gt;&lt;li&gt;&lt;a href="#IntegerLogObvious"&gt;Find the log base 2 of an integer with the MSB N set in O(N) operations (the obvious way)&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#IntegerLogIEEE64Float"&gt;Find the integer log base 2 of an integer with an 64-bit IEEE float&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#IntegerLogLookup"&gt;Find the log base 2 of an integer with a lookup table&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#IntegerLog"&gt;Find the log base 2 of an N-bit integer in O(lg(N)) operations&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#IntegerLogDeBruijn"&gt;Find the log base 2 of an N-bit integer in O(lg(N)) operations with multiply and lookup&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#IntegerLog10"&gt;Find integer log base 10 of an integer&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#IntegerLog10Obvious"&gt;Find integer log base 10 of an integer the obvious way&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#IntegerLogFloat"&gt;Find integer log base 2 of a 32-bit IEEE float&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#IntegerLogRootFloat"&gt;Find integer log base 2 of the pow(2, r)-root of a 32-bit IEEE float (for unsigned integer r)&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Counting consecutive trailing zero bits (or finding bit indices)&lt;ul&gt;&lt;li&gt;&lt;a href="#ZerosOnRightLinear"&gt;Count the consecutive zero bits (trailing) on the right linearly&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ZerosOnRightParallel"&gt;Count the consecutive zero bits (trailing) on the right in parallel&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ZerosOnRightBinSearch"&gt;Count the consecutive zero bits (trailing) on the right by binary search&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ZerosOnRightFloatCast"&gt;Count the consecutive zero bits (trailing) on the right by casting to a float&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ZerosOnRightModLookup"&gt;Count the consecutive zero bits (trailing) on the right with modulus division and lookup&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ZerosOnRightMultLookup"&gt;Count the consecutive zero bits (trailing) on the right with multiply and lookup&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#RoundUpPowerOf2Float"&gt;Round up to the next highest power of 2 by float casting&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#RoundUpPowerOf2"&gt;Round up to the next highest power of 2&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Interleaving bits (aka computing &lt;em&gt;Morton Numbers&lt;/em&gt;)&lt;ul&gt;&lt;li&gt;&lt;a href="#InterleaveTableObvious"&gt;Interleave bits the obvious way&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#InterleaveTableLookup"&gt;Interleave bits by table lookup&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#Interleave64bitOps"&gt;Interleave bits with 64-bit multiply&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#InterleaveBMN"&gt;Interleave bits by Binary Magic Numbers&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;Testing for ranges of bytes in a word (and counting occurances found)&lt;ul&gt;&lt;li&gt;&lt;a href="#ZeroInWord"&gt;Determine if a word has a zero byte&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#ValueInWord"&gt;Determine if a word has a byte equal to n&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#HasLessInWord"&gt;Determine if a word has byte less than n&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#HasMoreInWord"&gt;Determine if a word has a byte greater than n&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#HasBetweenInWord"&gt;Determine if a word has a byte between m and n&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="#NextBitPermutation"&gt;Compute the lexicographically next bit permutation&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="OperationCounting"&gt;&lt;/a&gt;About the operation counting methodology&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;When totaling the number of operations for algorithms here, any C operator is counted as one operation. Intermediate assignments, which need not be written to RAM, are not counted. Of course, this operation counting approach only serves as an approximation of the actual number of machine instructions and CPU time. All operations are assumed to take the same amount of time, which is not true in reality, but CPUs have been heading increasingly in this direction over time. There are many nuances that determine how fast a system will run a given sample of code, such as cache sizes, memory bandwidths, instruction sets, etc. In the end, benchmarking is the best way to determine whether one method is really faster than another, so consider the techniques below as possibilities to test on your target architecture.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="CopyIntegerSign"&gt;&lt;/a&gt;Compute the sign of an integer&lt;/strong&gt;&lt;/p&gt;int v;      // we want to find the sign of v&lt;br /&gt;int sign;   // the result goes here &lt;br /&gt;&lt;br /&gt;// CHAR_BIT is the number of bits per byte (normally 8).&lt;br /&gt;sign = -(v &amp;lt; 0);  // if v &amp;lt; 0 then -1, else 0. &lt;br /&gt;// or, to avoid branching on CPUs with flag registers (IA32):&lt;br /&gt;sign = -(int)((unsigned int)((int)v) &amp;gt;&amp;gt; (sizeof(int) * CHAR_BIT - 1));&lt;br /&gt;// or, for one less instruction (but not portable):&lt;br /&gt;sign = v &amp;gt;&amp;gt; (sizeof(int) * CHAR_BIT - 1); &lt;p&gt;The last expression above evaluates to sign = v &amp;gt;&amp;gt; 31 for 32-bit integers. This is one operation faster than the obvious way, sign = -(v &amp;lt; 0). This trick works because when signed integers are shifted right, the value of the far left bit is copied to the other bits. The far left bit is 1 when the value is negative and 0 otherwise; all 1 bits gives -1. Unfortunately, this behavior is architecture-specific.&lt;/p&gt;&lt;p&gt;Alternatively, if you prefer the result be either -1 or +1, then use:&lt;/p&gt;sign = +1 | (v &amp;gt;&amp;gt; (sizeof(int) * CHAR_BIT - 1));  // if v &amp;lt; 0 then -1, else +1&lt;p&gt;On the other hand, if you prefer the result be either -1, 0, or +1, then use:&lt;/p&gt;sign = (v != 0) | -(int)((unsigned int)((int)v) &amp;gt;&amp;gt; (sizeof(int) * CHAR_BIT - 1));&lt;br /&gt;// Or, for more speed but less portability:&lt;br /&gt;sign = (v != 0) | (v &amp;gt;&amp;gt; (sizeof(int) * CHAR_BIT - 1));  // -1, 0, or +1&lt;br /&gt;// Or, for portability, brevity, and (perhaps) speed:&lt;br /&gt;sign = (v &amp;gt; 0) - (v &amp;lt; 0); // -1, 0, or +1&lt;p&gt;If instead you want to know if something is non-negative, resulting in +1 or else 0, then use:&lt;/p&gt;sign = 1 ^ ((unsigned int)v &amp;gt;&amp;gt; (sizeof(int) * CHAR_BIT - 1)); // if v &amp;lt; 0 then 0, else 1&lt;p&gt;Caveat: On March 7, 2003, Angus Duggan pointed out that the 1989 ANSI C specification leaves the result of signed right-shift implementation-defined, so on some systems this hack might not work. For greater portability, Toby Speight suggested on September 28, 2005 that CHAR_BIT be used here and throughout rather than assuming bytes were 8 bits long. Angus recommended the more portable versions above, involving casting on March 4, 2006. &lt;a href="http://rpg-314.blogspot.com/"&gt;Rohit Garg&lt;/a&gt;suggested the version for non-negative integers on September 12, 2009.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="DetectOppositeSigns"&gt;&lt;/a&gt;Detect if two integers have opposite signs&lt;/strong&gt;&lt;/p&gt;int x, y;               // input values to compare signs&lt;br /&gt;&lt;br /&gt;bool f = ((x ^ y) &amp;lt; 0); // true iff x and y have opposite signs&lt;p&gt;Manfred Weis suggested I add this entry on November 26, 2009.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="IntegerAbs"&gt;&lt;/a&gt;Compute the integer absolute value (abs) without branching&lt;/strong&gt;&lt;/p&gt;int v;           // we want to find the absolute value of v&lt;br /&gt;unsigned int r;  // the result goes here &lt;br /&gt;int const mask = v &amp;gt;&amp;gt; sizeof(int) * CHAR_BIT - 1;&lt;br /&gt;&lt;br /&gt;r = (v + mask) ^ mask;&lt;p&gt;Patented variation:&lt;/p&gt;r = (v ^ mask) - mask;&lt;p&gt;Some CPUs don't have an integer absolute value instruction (or the compiler fails to use them). On machines where branching is expensive, the above expression can be faster than the obvious approach, r = (v &amp;lt; 0) ? -(unsigned)v : v, even though the number of operations is the same.&lt;/p&gt;&lt;p&gt;On March 7, 2003, Angus Duggan pointed out that the 1989 ANSI C specification leaves the result of signed right-shift implementation-defined, so on some systems this hack might not work. I've read that ANSI C does not require values to be represented as two's complement, so it may not work for that reason as well (on a diminishingly small number of old machines that still use one's complement). On March 14, 2004, Keith H. Duggar sent me the patented variation above; it is superior to the one I initially came up with, &lt;code&gt;r=(+1|(v&amp;gt;&amp;gt;(sizeof(int)*CHAR_BIT-1)))*v&lt;/code&gt;, because a multiply is not used. Unfortunately, this method has been &lt;a href="http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&amp;amp;Sect2=HITOFF&amp;amp;p=1&amp;amp;u=/netahtml/search-adv.htm&amp;amp;r=1&amp;amp;f=G&amp;amp;l=50&amp;amp;d=ptxt&amp;amp;S1=6073150&amp;amp;OS=6073150&amp;amp;RS=6073150"&gt;patented&lt;/a&gt; in the USA on June 6, 2000 by Vladimir Yu Volkonsky and assigned to &lt;a href="http://www.sun.com/"&gt;Sun Microsystems&lt;/a&gt;. On August 13, 2006, Yuriy Kaminskiy told me that the patent is likely invalid because the method was published well before the patent was even filed, such as in &lt;a href="http://www.goof.com/pcg/doc/pentopt.txt"&gt;How to Optimize for the Pentium Processor&lt;/a&gt; by Agner Fog, dated November, 9, 1996. Yuriy also mentioned that this document was translated to Russian in 1997, which Vladimir could have read. Moreover, the Internet Archive also has an old &lt;a href="http://web.archive.org/web/19961201174141/www.x86.org/ftp/articles/pentopt/PENTOPT.TXT"&gt;link&lt;/a&gt; to it. On January 30, 2007, Peter Kankowski shared with me an &lt;a href="http://smallcode.weblogs.us/2007/01/31/microsoft-probably-uses-the-abs-function-patented-by-sun/"&gt;abs version&lt;/a&gt; he discovered that was inspired by Microsoft's Visual C++ compiler output. It is featured here as the primary solution. On December 6, 2007, Hai Jin complained that the result was signed, so when computing the abs of the most negative value, it was still negative. On April 15, 2008 Andrew Shapira pointed out that the obvious approach could overflow, as it lacked an (unsigned) cast then; for maximum portability he suggested &lt;code&gt;(v &amp;lt; 0) ? (1 + ((unsigned)(-1-v))) : (unsigned)v&lt;/code&gt;. But citing the ISO C99 spec on July 9, 2008, Vincent Lef&amp;egrave;vre convinced me to remove it becasue even on non-2s-complement machines -(unsigned)v will do the right thing. The evaluation of -(unsigned)v first converts the negative value of v to an unsigned by adding 2**N, yielding a 2s complement representation of v's value that I'll call U. Then, U is negated, giving the desired result, -U = 0 - U = 2**N - U = 2**N - (v+2**N) = -v = abs(v).&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="IntegerMinOrMax"&gt;&lt;/a&gt;Compute the minimum (min) or maximum (max) of two integers without branching&lt;/strong&gt;&lt;/p&gt;int x;  // we want to find the minimum of x and y&lt;br /&gt;int y;   &lt;br /&gt;int r;  // the result goes here &lt;br /&gt;&lt;br /&gt;r = y ^ ((x ^ y) &amp;amp; -(x &amp;lt; y)); // min(x, y)&lt;p&gt;On some rare machines where branching is very expensive and no condition move instructions exist, the above expression might be faster than the obvious approach, r = (x &amp;lt; y) ? x : y, even though it involves two more instructions. (Typically, the obvious approach is best, though.) It works because if x &amp;lt; y, then -(x &amp;lt; y) will be all ones, so r = y ^ (x ^ y) &amp;amp; ~0 = y ^ x ^ y = x. Otherwise, if x &amp;gt;= y, then -(x &amp;lt; y) will be all zeros, so r = y ^ ((x ^ y) &amp;amp; 0) = y. On some machines, evaluating (x &amp;lt; y) as 0 or 1 requires a branch instruction, so there may be no advantage.&lt;/p&gt;&lt;p&gt;To find the maximum, use:&lt;/p&gt;r = x ^ ((x ^ y) &amp;amp; -(x &amp;lt; y)); // max(x, y)&lt;p&gt;&lt;strong&gt;Quick and dirty versions:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;If you know that INT_MIN &amp;lt;= x - y &amp;lt;= INT_MAX, then you can use the following, which are faster because (x - y) only needs to be evaluated once.&lt;/p&gt;r = y + ((x - y) &amp;amp; ((x - y) &amp;gt;&amp;gt; (sizeof(int) * CHAR_BIT - 1))); // min(x, y)&lt;br /&gt;r = x - ((x - y) &amp;amp; ((x - y) &amp;gt;&amp;gt; (sizeof(int) * CHAR_BIT - 1))); // max(x, y)&lt;p&gt;Note that the 1989 ANSI C specification doesn't specify the result of signed right-shift, so these aren't portable. If exceptions are thrown on overflows, then the values of x and y should be unsigned or cast to unsigned for the subtractions to avoid unnecessarily throwing an exception, however the right-shift needs a signed operand to produce all one bits when negative, so cast to signed there.&lt;/p&gt;&lt;p&gt;On March 7, 2003, Angus Duggan pointed out the right-shift portability issue. On May 3, 2005, Randal E. Bryant alerted me to the need for the precondition, INT_MIN &amp;lt;= x - y &amp;lt;= INT_MAX, and suggested the non-quick and dirty version as a fix. Both of these issues concern only the quick and dirty version. Nigel Horspoon observed on July 6, 2005 that gcc produced the same code on a Pentium as the obvious solution because of how it evaluates (x &amp;lt; y). On July 9, 2008 Vincent Lef&amp;egrave;vre pointed out the potential for overflow exceptions with subtractions in r = y + ((x - y) &amp;amp; -(x &amp;lt; y)), which was the previous version. Timothy B. Terriberry suggested using xor rather than add and subract to avoid casting and the risk of overflows on June 2, 2009.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="DetermineIfPowerOf2"&gt;&lt;/a&gt;Determining if an integer is a power of 2&lt;/strong&gt;&lt;/p&gt;unsigned int v; // we want to see if v is a power of 2&lt;br /&gt;bool f;         // the result goes here &lt;br /&gt;&lt;br /&gt;f = (v &amp;amp; (v - 1)) == 0;&lt;p&gt;Note that 0 is incorrectly considered a power of 2 here. To remedy this, use:&lt;/p&gt;f = v &amp;amp;&amp;amp; !(v &amp;amp; (v - 1));&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="FixedSignExtend"&gt;&lt;/a&gt;Sign extending from a constant bit-width&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Sign extension is automatic for built-in types, such as chars and ints. But suppose you have a signed two's complement number, x, that is stored using only b bits. Moreover, suppose you want to convert x to an int, which has more than b bits. A simple copy will work if x is positive, but if negative, the sign must be extended. For example, if we have only 4 bits to store a number, then -3 is represented as 1101 in binary. If we have 8 bits, then -3 is 11111101. The most-significant bit of the 4-bit representation is replicated sinistrally to fill in the destination when we convert to a representation with more bits; this is sign extending. In C, sign extension from a constant bit-width is trivial, since bit fields may be specified in structs or unions. For example, to convert from 5 bits to an full integer:&lt;/p&gt;int x; // convert this from using 5 bits to a full int&lt;br /&gt;int r; // resulting sign extended number goes here&lt;br /&gt;struct {signed int x:5;} s;&lt;br /&gt;r = s.x = x;&lt;p&gt;The following is a C++ template function that uses the same language feature to convert from B bits in one operation (though the compiler is generating more, of course).&lt;/p&gt;template &amp;lt;typename T, unsigned B&amp;gt;&lt;br /&gt;inline T signextend(const T x)&lt;br /&gt;{&lt;br /&gt;  struct {T x:B;} s;&lt;br /&gt;  return s.x = x;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int r = signextend&amp;lt;signed int,5&amp;gt;(x);  // sign extend 5 bit number x to r&lt;p&gt;John Byrd caught a typo in the code (attributed to html formatting) on May 2, 2005. On March 4, 2006, Pat Wood pointed out that the ANSI C standard requires that the bitfield have the keyword "signed" to be signed; otherwise, the sign is undefined.&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="VariableSignExtend"&gt;&lt;/a&gt;Sign extending from a variable bit-width&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Sometimes we need to extend the sign of a number but we don't know a priori the number of bits, b, in which it is represented. (Or we could be programming in a language like Java, which lacks bitfields.)&lt;/p&gt;unsigned b; // number of bits representing the number in x&lt;br /&gt;int x;      // sign extend this b-bit number to r&lt;br /&gt;int r;      // resulting sign-extended number&lt;br /&gt;int const m = 1U &amp;lt;&amp;lt; (b - 1); // mask can be pre-computed if b is fixed&lt;br /&gt;&lt;br /&gt;x = x &amp;amp; ((1U &amp;lt;&amp;lt; b) - 1);  // (Skip this if bits in x above position b are already zero.)&lt;br /&gt;r = (x ^ m) - m;&lt;p&gt;The code above requires four operations, but when the bitwidth is a constant rather than variable, it requires only two fast operations, assuming the upper bits are already zeroes.&lt;/p&gt;&lt;p&gt;A slightly faster but less portable method that doesn't depend on the bits in x above position b being zero is:&lt;/p&gt;int const m = CHAR_BIT * sizeof(x) - b;&lt;br /&gt;r = (x &amp;lt;&amp;lt; m) &amp;gt;&amp;gt; m;&lt;p&gt;Sean A. Irvine suggested that I add sign extension methods to this page on June 13, 2004, and he provided &lt;code&gt;m = (1 &amp;lt;&amp;lt; (b - 1)) - 1; r = -(x &amp;amp; ~m) | x;&lt;/code&gt; as a starting point from which I optimized to get m = 1U &amp;lt;&amp;lt; (b - 1); r = -(x &amp;amp; m) | x. But then on May 11, 2007, Shay Green suggested the version above, which requires one less operation than mine. Vipin Sharma suggested I add a step to deal with situations where x had possible ones in bits other than the b bits we wanted to sign-extend on Oct. 15, 2008. On December 31, 2009 Chris Pirazzi suggested I add the faster version, which requires two operations for constant bit-widths and three for variable widths.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="VariableSignExtendRisky"&gt;&lt;/a&gt;Sign extending from a variable bit-width in 3 operations&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;The following may be slow on some machines, due to the effort required for multiplication and division. This version is 4 operations. If you know that your initial bit-width, b, is greater than 1, you might do this type of sign extension in 3 operations by using r = (x * multipliers[b]) / multipliers[b], which requires only one array lookup.&lt;/p&gt;unsigned b; // number of bits representing the number in x&lt;br /&gt;int x;      // sign extend this b-bit number to r&lt;br /&gt;int r;      // resulting sign-extended number&lt;br /&gt;#define M(B) (1U &amp;lt;&amp;lt; ((sizeof(x) * CHAR_BIT) - B)) // CHAR_BIT=bits/byte&lt;br /&gt;static int const multipliers[] = &lt;br /&gt;{&lt;br /&gt;  0,     M(1),  M(2),  M(3),  M(4),  M(5),  M(6),  M(7),&lt;br /&gt;  M(8),  M(9),  M(10), M(11), M(12), M(13), M(14), M(15),&lt;br /&gt;  M(16), M(17), M(18), M(19), M(20), M(21), M(22), M(23),&lt;br /&gt;  M(24), M(25), M(26), M(27), M(28), M(29), M(30), M(31),&lt;br /&gt;  M(32)&lt;br /&gt;}; // (add more if using more than 64 bits)&lt;br /&gt;static int const divisors[] = &lt;br /&gt;{&lt;br /&gt;  1,    ~M(1),  M(2),  M(3),  M(4),  M(5),  M(6),  M(7),&lt;br /&gt;  M(8),  M(9),  M(10), M(11), M(12), M(13), M(14), M(15),&lt;br /&gt;  M(16), M(17), M(18), M(19), M(20), M(21), M(22), M(23),&lt;br /&gt;  M(24), M(25), M(26), M(27), M(28), M(29), M(30), M(31),&lt;br /&gt;  M(32)&lt;br /&gt;}; // (add more for 64 bits)&lt;br /&gt;#undef M&lt;br /&gt;r = (x * multipliers[b]) / divisors[b];&lt;p&gt;The following variation is not portable, but on architectures that employ an arithmetic right-shift, maintaining the sign, it should be fast.&lt;/p&gt;const int s = -b; // OR:  sizeof(x) * CHAR_BIT - b;&lt;br /&gt;r = (x &amp;lt;&amp;lt; s) &amp;gt;&amp;gt; s;&lt;p&gt;Randal E. Bryant pointed out a bug on May 3, 2005 in an earlier version (that used multipliers[] for divisors[]), where it failed on the case of x=1 and b=1.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ConditionalSetOrClearBitsWithoutBranching"&gt;&lt;/a&gt;Conditionally set or clear bits without branching&lt;/strong&gt;&lt;/p&gt;bool f;         // conditional flag&lt;br /&gt;unsigned int m; // the bit mask&lt;br /&gt;unsigned int w; // the word to modify:  if (f) w |= m; else w &amp;amp;= ~m; &lt;br /&gt;&lt;br /&gt;w ^= (-f ^ w) &amp;amp; m;&lt;br /&gt;&lt;br /&gt;// OR, for superscalar CPUs:&lt;br /&gt;w = (w &amp;amp; ~m) | (-f &amp;amp; m);&lt;p&gt;On some architectures, the lack of branching can more than make up for what appears to be twice as many operations. For instance, informal speed tests on an AMD Athlon&amp;trade; XP 2100+ indicated it was 5-10% faster. An Intel Core 2 Duo ran the superscalar version about 16% faster than the first. Glenn Slayden informed me of the first expression on December 11, 2003. Marco Yu shared the superscalar version with me on April 3, 2007 and alerted me to a typo 2 days later.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ConditionalNegate"&gt;&lt;/a&gt;Conditionally negate a value without branching&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;If you need to negate only when a flag is false, then use the following to avoid branching:&lt;/p&gt;bool fDontNegate;  // Flag indicating we should not negate v.&lt;br /&gt;int v;             // Input value to negate if fDontNegate is false.&lt;br /&gt;int r;             // result = fDontNegate ? v : -v;&lt;br /&gt;&lt;br /&gt;r = (fDontNegate ^ (fDontNegate - 1)) * v;&lt;p&gt;If you need to negate only when a flag is true, then use this:&lt;/p&gt;bool fNegate;  // Flag indicating if we should negate v.&lt;br /&gt;int v;         // Input value to negate if fNegate is true.&lt;br /&gt;int r;         // result = fNegate ? -v : v;&lt;br /&gt;&lt;br /&gt;r = (v ^ -fNegate) + fNegate;&lt;p&gt;Avraham Plotnitzky suggested I add the first version on June 2, 2009. Motivated to avoid the multiply, I came up with the second version on June 8, 2009. Alfonso De Gregorio pointed out that some parens were missing on November 26, 2009, and received a bug bounty.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="MaskedMerge"&gt;&lt;/a&gt;Merge bits from two values according to a mask&lt;/strong&gt;&lt;/p&gt;unsigned int a;    // value to merge in non-masked bits&lt;br /&gt;unsigned int b;    // value to merge in masked bits&lt;br /&gt;unsigned int mask; // 1 where bits from b should be selected; 0 where from a.&lt;br /&gt;unsigned int r;    // result of (a &amp;amp; ~mask) | (b &amp;amp; mask) goes here&lt;br /&gt;&lt;br /&gt;r = a ^ ((a ^ b) &amp;amp; mask); &lt;p&gt;This shaves one operation from the obvious way of combining two sets of bits according to a bit mask. If the mask is a constant, then there may be no advantage.&lt;/p&gt;&lt;p&gt;Ron Jeffery sent this to me on February 9, 2006.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="CountBitsSetNaive"&gt;&lt;/a&gt;Counting bits set (naive way)&lt;/strong&gt;&lt;/p&gt;unsigned int v; // count the number of bits set in v&lt;br /&gt;unsigned int c; // c accumulates the total bits set in v&lt;br /&gt;&lt;br /&gt;for (c = 0; v; v &amp;gt;&amp;gt;= 1)&lt;br /&gt;{&lt;br /&gt;  c += v &amp;amp; 1;&lt;br /&gt;}&lt;p&gt;The naive approach requires one iteration per bit, until no more bits are set. So on a 32-bit word with only the high set, it will go through 32 iterations.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="CountBitsSetTable"&gt;&lt;/a&gt;Counting bits set by lookup table&lt;/strong&gt;&lt;/p&gt;static const unsigned char BitsSetTable256[256] = &lt;br /&gt;{&lt;br /&gt;#   define B2(n) n,     n+1,     n+1,     n+2&lt;br /&gt;#   define B4(n) B2(n), B2(n+1), B2(n+1), B2(n+2)&lt;br /&gt;#   define B6(n) B4(n), B4(n+1), B4(n+1), B4(n+2)&lt;br /&gt;    B6(0), B6(1), B6(1), B6(2)&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;unsigned int v; // count the number of bits set in 32-bit value v&lt;br /&gt;unsigned int c; // c is the total bits set in v&lt;br /&gt;&lt;br /&gt;// Option 1:&lt;br /&gt;c = BitsSetTable256[v &amp;amp; 0xff] + &lt;br /&gt;    BitsSetTable256[(v &amp;gt;&amp;gt; 8) &amp;amp; 0xff] + &lt;br /&gt;    BitsSetTable256[(v &amp;gt;&amp;gt; 16) &amp;amp; 0xff] + &lt;br /&gt;    BitsSetTable256[v &amp;gt;&amp;gt; 24]; &lt;br /&gt;&lt;br /&gt;// Option 2:&lt;br /&gt;unsigned char * p = (unsigned char *) &amp;amp;v;&lt;br /&gt;c = BitsSetTable256[p[0]] + &lt;br /&gt;    BitsSetTable256[p[1]] + &lt;br /&gt;    BitsSetTable256[p[2]] +&lt;br /&gt;    BitsSetTable256[p[3]];&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// To initially generate the table algorithmically:&lt;br /&gt;BitsSetTable256[0] = 0;&lt;br /&gt;for (int i = 0; i &amp;lt; 256; i++)&lt;br /&gt;{&lt;br /&gt;  BitsSetTable256[i] = (i &amp;amp; 1) + BitsSetTable256[i / 2];&lt;br /&gt;}&lt;p&gt;On July 14, 2009 Hallvard Furuseth suggested the macro compacted table.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="CountBitsSetKernighan"&gt;&lt;/a&gt;Counting bits set, Brian Kernighan's way&lt;/strong&gt;&lt;/p&gt;unsigned int v; // count the number of bits set in v&lt;br /&gt;unsigned int c; // c accumulates the total bits set in v&lt;br /&gt;for (c = 0; v; c++)&lt;br /&gt;{&lt;br /&gt;  v &amp;amp;= v - 1; // clear the least significant bit set&lt;br /&gt;}&lt;p&gt;Brian Kernighan's method goes through as many iterations as there are set bits. So if we have a 32-bit word with only the high bit set, then it will only go once through the loop.&lt;/p&gt;&lt;p&gt;Published in 1988, the C Programming Language 2nd Ed. (by Brian W. Kernighan and Dennis M. Ritchie) mentions this in exercise 2-9. On April 19, 2006 Don Knuth pointed out to me that this method "was first published by Peter Wegner in CACM 3 (1960), 322. (Also discovered independently by Derrick Lehmer and published in 1964 in a book edited by Beckenbach.)"&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="CountBitsSet64"&gt;&lt;/a&gt;Counting bits set in 14, 24, or 32-bit words using 64-bit instructions&lt;/strong&gt;&lt;/p&gt;unsigned int v; // count the number of bits set in v&lt;br /&gt;unsigned int c; // c accumulates the total bits set in v&lt;br /&gt;&lt;br /&gt;// option 1, for at most 14-bit values in v:&lt;br /&gt;c = (v * 0x200040008001ULL &amp;amp; 0x111111111111111ULL) % 0xf;&lt;br /&gt;&lt;br /&gt;// option 2, for at most 24-bit values in v:&lt;br /&gt;c =  ((v &amp;amp; 0xfff) * 0x1001001001001ULL &amp;amp; 0x84210842108421ULL) % 0x1f;&lt;br /&gt;c += (((v &amp;amp; 0xfff000) &amp;gt;&amp;gt; 12) * 0x1001001001001ULL &amp;amp; 0x84210842108421ULL) &lt;br /&gt;     % 0x1f;&lt;br /&gt;&lt;br /&gt;// option 3, for at most 32-bit values in v:&lt;br /&gt;c =  ((v &amp;amp; 0xfff) * 0x1001001001001ULL &amp;amp; 0x84210842108421ULL) % 0x1f;&lt;br /&gt;c += (((v &amp;amp; 0xfff000) &amp;gt;&amp;gt; 12) * 0x1001001001001ULL &amp;amp; 0x84210842108421ULL) % &lt;br /&gt;     0x1f;&lt;br /&gt;c += ((v &amp;gt;&amp;gt; 24) * 0x1001001001001ULL &amp;amp; 0x84210842108421ULL) % 0x1f;&lt;p&gt;This method requires a 64-bit CPU with fast modulus division to be efficient. The first option takes only 3 operations; the second option takes 10; and the third option takes 15.&lt;/p&gt;&lt;p&gt;Rich Schroeppel originally created a 9-bit version, similiar to option 1; see the Programming Hacks section of &lt;a href="http://www.inwap.com/pdp10/hbaker/hakmem/hakmem.html"&gt;Beeler, M., Gosper, R. W., and Schroeppel, R. HAKMEM. MIT AI Memo 239, Feb. 29, 1972.&lt;/a&gt; His method was the inspiration for the variants above, devised by Sean Anderson. Randal E. Bryant offered a couple bug fixes on May 3, 2005. Bruce Dawson tweaked what had been a 12-bit version and made it suitable for 14 bits using the same number of operations on Feburary 1, 2007.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="CountBitsSetParallel"&gt;&lt;/a&gt;Counting bits set, in parallel&lt;/strong&gt;&lt;/p&gt;unsigned int v; // count bits set in this (32-bit value)&lt;br /&gt;unsigned int c; // store the total here&lt;br /&gt;static const int S[] = {1, 2, 4, 8, 16}; // Magic Binary Numbers&lt;br /&gt;static const int B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF, 0x0000FFFF};&lt;br /&gt;&lt;br /&gt;c = v - ((v &amp;gt;&amp;gt; 1) &amp;amp; B[0]);&lt;br /&gt;c = ((c &amp;gt;&amp;gt; S[1]) &amp;amp; B[1]) + (c &amp;amp; B[1]);&lt;br /&gt;c = ((c &amp;gt;&amp;gt; S[2]) + c) &amp;amp; B[2];&lt;br /&gt;c = ((c &amp;gt;&amp;gt; S[3]) + c) &amp;amp; B[3];&lt;br /&gt;c = ((c &amp;gt;&amp;gt; S[4]) + c) &amp;amp; B[4];&lt;p&gt;The B array, expressed as binary, is:&lt;/p&gt;B[0] = 0x55555555 = 01010101 01010101 01010101 01010101&lt;br /&gt;B[1] = 0x33333333 = 00110011 00110011 00110011 00110011&lt;br /&gt;B[2] = 0x0F0F0F0F = 00001111 00001111 00001111 00001111&lt;br /&gt;B[3] = 0x00FF00FF = 00000000 11111111 00000000 11111111&lt;br /&gt;B[4] = 0x0000FFFF = 00000000 00000000 11111111 11111111&lt;p&gt;We can adjust the method for larger integer sizes by continuing with the patterns for the &lt;em&gt;Binary Magic Numbers,&lt;/em&gt;B and S. If there are k bits, then we need the arrays S and B to be ceil(lg(k)) elements long, and we must compute the same number of expressions for c as S or B are long. For a 32-bit v, 16 operations are used.&lt;/p&gt;&lt;p&gt;The best method for counting bits in a 32-bit integer v is the following:&lt;/p&gt;v = v - ((v &amp;gt;&amp;gt; 1) &amp;amp; 0x55555555);                    // reuse input as temporary&lt;br /&gt;v = (v &amp;amp; 0x33333333) + ((v &amp;gt;&amp;gt; 2) &amp;amp; 0x33333333);     // temp&lt;br /&gt;c = ((v + (v &amp;gt;&amp;gt; 4) &amp;amp; 0xF0F0F0F) * 0x1010101) &amp;gt;&amp;gt; 24; // count&lt;p&gt;The best bit counting method takes only 12 operations, which is the same as the lookup-table method, but avoids the memory and potential cache misses of a table. It is a hybrid between the purely parallel method above and the earlier methods using multiplies (in the section on counting bits with 64-bit instructions), though it doesn't use 64-bit instructions. The counts of bits set in the bytes is done in parallel, and the sum total of the bits set in the bytes is computed by multiplying by 0x1010101 and shifting right 24 bits.&lt;/p&gt;&lt;p&gt;A generalization of the best bit counting method to integers of bit-widths upto 128 (parameterized by type T) is this:&lt;/p&gt;v = v - ((v &amp;gt;&amp;gt; 1) &amp;amp; (T)~(T)0/3);                           // temp&lt;br /&gt;v = (v &amp;amp; (T)~(T)0/15*3) + ((v &amp;gt;&amp;gt; 2) &amp;amp; (T)~(T)0/15*3);      // temp&lt;br /&gt;v = (v + (v &amp;gt;&amp;gt; 4)) &amp;amp; (T)~(T)0/255*15;                      // temp&lt;br /&gt;c = (T)(v * ((T)~(T)0/255)) &amp;gt;&amp;gt; (sizeof(T) - 1) * CHAR_BIT; // count&lt;p&gt;See &lt;a href="http://groups.google.com/groups?q=reverse+bits&amp;amp;num=100&amp;amp;hl=en&amp;amp;group=comp.graphics.algorithms&amp;amp;imgsafe=off&amp;amp;safe=off&amp;amp;rnum=2&amp;amp;ic=1&amp;amp;selm=4fulhm%248dn%40atlas.uniserve.com"&gt;Ian Ashdown's nice newsgroup post&lt;/a&gt; for more information on counting the number of bits set (also known as &lt;em&gt;sideways addition&lt;/em&gt;). The best bit counting method was brought to my attention on October 5, 2005 by &lt;a href="http://onezero.org/"&gt;Andrew Shapira&lt;/a&gt;; he found it in pages 187-188 of &lt;a href="http://www.amd.com/us-en/assets/content_type/white_papers_and_tech_docs/25112.PDF"&gt;Software Optimization Guide for AMD Athlon&amp;trade; 64 and Opteron&amp;trade; Processors&lt;/a&gt;. Charlie Gordon suggested a way to shave off one operation from the purely parallel version on December 14, 2005, and Don Clugston trimmed three more from it on December 30, 2005. I made a typo with Don's suggestion that Eric Cole spotted on January 8, 2006. Eric later suggested the arbitrary bit-width generalization to the best method on November 17, 2006. On April 5, 2007, Al Williams observed that I had a line of dead code at the top of the first method.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="#CountBitsFromMSBToPos"&gt;&lt;/a&gt;Count bits set (rank) from the most-significant bit upto a given position&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;The following finds the the rank of a bit, meaning it returns the sum of bits that are set to 1 from the most-signficant bit downto the bit at the given position.&lt;/p&gt;  uint64_t v;       // Compute the rank (bits set) in v from the MSB to pos.&lt;br /&gt;  unsigned int pos; // Bit position to count bits upto.&lt;br /&gt;  uint64_t r;       // Resulting rank of bit at pos goes here.&lt;br /&gt;&lt;br /&gt;  // Shift out bits after given position.&lt;br /&gt;  r = v &amp;gt;&amp;gt; (sizeof(v) * CHAR_BIT - pos);&lt;br /&gt;  // Count set bits in parallel.&lt;br /&gt;  // r = (r &amp;amp; 0x5555...) + ((r &amp;gt;&amp;gt; 1) &amp;amp; 0x5555...);&lt;br /&gt;  r = r - ((r &amp;gt;&amp;gt; 1) &amp;amp; ~0UL/3);&lt;br /&gt;  // r = (r &amp;amp; 0x3333...) + ((r &amp;gt;&amp;gt; 2) &amp;amp; 0x3333...);&lt;br /&gt;  r = (r &amp;amp; ~0UL/5) + ((r &amp;gt;&amp;gt; 2) &amp;amp; ~0UL/5);&lt;br /&gt;  // r = (r &amp;amp; 0x0f0f...) + ((r &amp;gt;&amp;gt; 4) &amp;amp; 0x0f0f...);&lt;br /&gt;  r = (r + (r &amp;gt;&amp;gt; 4)) &amp;amp; ~0UL/17;&lt;br /&gt;  // r = r % 255;&lt;br /&gt;  r = (r * (~0UL/255)) &amp;gt;&amp;gt; ((sizeof(v) - 1) * CHAR_BIT);&lt;p&gt;Juha J&amp;auml;rvi sent this to me on November 21, 2009 as an inverse operation to the computing the bit position with the given rank, which follows.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="#SelectPosFromMSBRank"&gt;&lt;/a&gt;Select the bit position (from the most-significant bit) with the given count (rank)&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;The following 64-bit code selects the position of the r&lt;sup&gt;th&lt;/sup&gt;1 bit when counting from the left. In other words if we start at the most significant bit and proceed to the right, counting the number of bits set to 1 until we reach the desired rank, r, then the position where we stop is returned. If the rank requested exceeds the count of bits set, then 64 is returned. The code may be modified for 32-bit or counting from the right.&lt;/p&gt;  uint64_t v;          // Input value to find position with rank r.&lt;br /&gt;  unsigned int r;      // Input: bit's desired rank [1-64].&lt;br /&gt;  unsigned int s;      // Output: Resulting position of bit with rank r [1-64]&lt;br /&gt;  uint64_t a, b, c, d; // Intermediate temporaries for bit count.&lt;br /&gt;  unsigned int t;      // Bit count temporary.&lt;br /&gt;&lt;br /&gt;  // Do a normal parallel bit count for a 64-bit integer,                     &lt;br /&gt;  // but store all intermediate steps.                                        &lt;br /&gt;  // a = (v &amp;amp; 0x5555...) + ((v &amp;gt;&amp;gt; 1) &amp;amp; 0x5555...);&lt;br /&gt;  a =  v - ((v &amp;gt;&amp;gt; 1) &amp;amp; ~0UL/3);&lt;br /&gt;  // b = (a &amp;amp; 0x3333...) + ((a &amp;gt;&amp;gt; 2) &amp;amp; 0x3333...);&lt;br /&gt;  b = (a &amp;amp; ~0UL/5) + ((a &amp;gt;&amp;gt; 2) &amp;amp; ~0UL/5);&lt;br /&gt;  // c = (b &amp;amp; 0x0f0f...) + ((b &amp;gt;&amp;gt; 4) &amp;amp; 0x0f0f...);&lt;br /&gt;  c = (b + (b &amp;gt;&amp;gt; 4)) &amp;amp; ~0UL/0x11;&lt;br /&gt;  // d = (c &amp;amp; 0x00ff...) + ((c &amp;gt;&amp;gt; 8) &amp;amp; 0x00ff...);&lt;br /&gt;  d = (c + (c &amp;gt;&amp;gt; 8)) &amp;amp; ~0UL/0x101;&lt;br /&gt;  t = (d &amp;gt;&amp;gt; 32) + (d &amp;gt;&amp;gt; 48);&lt;br /&gt;  // Now do branchless select!                                                &lt;br /&gt;  s  = 64;&lt;br /&gt;  // if (r &amp;gt; t) {s -= 32; r -= t;}&lt;br /&gt;  s -= ((t - r) &amp;amp; 256) &amp;gt;&amp;gt; 3; r -= (t &amp;amp; ((t - r) &amp;gt;&amp;gt; 8));&lt;br /&gt;  t  = (d &amp;gt;&amp;gt; (s - 16)) &amp;amp; 0xff;&lt;br /&gt;  // if (r &amp;gt; t) {s -= 16; r -= t;}&lt;br /&gt;  s -= ((t - r) &amp;amp; 256) &amp;gt;&amp;gt; 4; r -= (t &amp;amp; ((t - r) &amp;gt;&amp;gt; 8));&lt;br /&gt;  t  = (c &amp;gt;&amp;gt; (s - 8)) &amp;amp; 0xf;&lt;br /&gt;  // if (r &amp;gt; t) {s -= 8; r -= t;}&lt;br /&gt;  s -= ((t - r) &amp;amp; 256) &amp;gt;&amp;gt; 5; r -= (t &amp;amp; ((t - r) &amp;gt;&amp;gt; 8));&lt;br /&gt;  t  = (b &amp;gt;&amp;gt; (s - 4)) &amp;amp; 0x7;&lt;br /&gt;  // if (r &amp;gt; t) {s -= 4; r -= t;}&lt;br /&gt;  s -= ((t - r) &amp;amp; 256) &amp;gt;&amp;gt; 6; r -= (t &amp;amp; ((t - r) &amp;gt;&amp;gt; 8));&lt;br /&gt;  t  = (a &amp;gt;&amp;gt; (s - 2)) &amp;amp; 0x3;&lt;br /&gt;  // if (r &amp;gt; t) {s -= 2; r -= t;}&lt;br /&gt;  s -= ((t - r) &amp;amp; 256) &amp;gt;&amp;gt; 7; r -= (t &amp;amp; ((t - r) &amp;gt;&amp;gt; 8));&lt;br /&gt;  t  = (v &amp;gt;&amp;gt; (s - 1)) &amp;amp; 0x1;&lt;br /&gt;  // if (r &amp;gt; t) s--;&lt;br /&gt;  s -= ((t - r) &amp;amp; 256) &amp;gt;&amp;gt; 8;&lt;br /&gt;  s = 65 - s;&lt;p&gt;If branching is fast on your target CPU, consider uncommenting the if-statements and commenting the lines that follow them.&lt;/p&gt;&lt;p&gt;Juha J&amp;auml;rvi sent this to me on November 21, 2009.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ParityNaive"&gt;&lt;/a&gt;Computing parity the naive way&lt;/strong&gt;&lt;/p&gt;unsigned int v;       // word value to compute the parity of&lt;br /&gt;bool parity = false;  // parity will be the parity of v&lt;br /&gt;&lt;br /&gt;while (v)&lt;br /&gt;{&lt;br /&gt;  parity = !parity;&lt;br /&gt;  v = v &amp;amp; (v - 1);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;p&gt;The above code uses an approach like Brian Kernigan's bit counting, above. The time it takes is proportional to the number of bits set.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ParityLookupTable"&gt;&lt;/a&gt;Compute parity by lookup table&lt;/strong&gt;&lt;/p&gt;static const bool ParityTable256[256] = &lt;br /&gt;{&lt;br /&gt;#   define P2(n) n, n^1, n^1, n&lt;br /&gt;#   define P4(n) P2(n), P2(n^1), P2(n^1), P2(n)&lt;br /&gt;#   define P6(n) P4(n), P4(n^1), P4(n^1), P4(n)&lt;br /&gt;    P6(0), P6(1), P6(1), P6(0)&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;unsigned char b;  // byte value to compute the parity of&lt;br /&gt;bool parity = ParityTable256[b];&lt;br /&gt;&lt;br /&gt;// OR, for 32-bit words:&lt;br /&gt;unsigned int v;&lt;br /&gt;v ^= v &amp;gt;&amp;gt; 16;&lt;br /&gt;v ^= v &amp;gt;&amp;gt; 8;&lt;br /&gt;bool parity = ParityTable256[v &amp;amp; 0xff];&lt;br /&gt;&lt;br /&gt;// Variation:&lt;br /&gt;unsigned char * p = (unsigned char *) &amp;amp;v;&lt;br /&gt;parity = ParityTable256[p[0] ^ p[1] ^ p[2] ^ p[3]];&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Randal E. Bryant encouraged the addition of the (admittedly) obvious last variation with variable p on May 3, 2005. Bruce Rawles found a typo in an instance of the table variable's name on September 27, 2005, and he received a $10 bug bounty. On October 9, 2006, Fabrice Bellard suggested the 32-bit variations above, which require only one table lookup; the previous version had four lookups (one per byte) and were slower. On July 14, 2009 Hallvard Furuseth suggested the macro compacted table.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ParityWith64Bits"&gt;&lt;/a&gt;Compute parity of a byte using 64-bit multiply and modulus division&lt;/strong&gt;&lt;/p&gt;unsigned char b;  // byte value to compute the parity of&lt;br /&gt;bool parity = &lt;br /&gt;  (((b * 0x0101010101010101ULL) &amp;amp; 0x8040201008040201ULL) % 0x1FF) &amp;amp; 1;&lt;p&gt;The method above takes around 4 operations, but only works on bytes.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="#ParityMultiply"&gt;&lt;/a&gt;Compute parity of word with a multiply&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;The following method computes the parity of the 32-bit value in only 8 operations using a multiply.&lt;/p&gt;    unsigned int v; // 32-bit word&lt;br /&gt;    v ^= v &amp;gt;&amp;gt; 1;&lt;br /&gt;    v ^= v &amp;gt;&amp;gt; 2;&lt;br /&gt;    v = (v &amp;amp; 0x11111111U) * 0x11111111U;&lt;br /&gt;    return (v &amp;gt;&amp;gt; 28) &amp;amp; 1;&lt;p&gt;Also for 64-bits, 8 operations are still enough.&lt;/p&gt;    unsigned long long v; // 64-bit word&lt;br /&gt;    v ^= v &amp;gt;&amp;gt; 1;&lt;br /&gt;    v ^= v &amp;gt;&amp;gt; 2;&lt;br /&gt;    v = (v &amp;amp; 0x1111111111111111UL) * 0x1111111111111111UL;&lt;br /&gt;    return (v &amp;gt;&amp;gt; 60) &amp;amp; 1;&lt;p&gt;Andrew Shapira came up with this and sent it to me on Sept. 2, 2007.&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ParityParallel"&gt;&lt;/a&gt;Compute parity in parallel&lt;/strong&gt;&lt;/p&gt;unsigned int v;  // word value to compute the parity of&lt;br /&gt;v ^= v &amp;gt;&amp;gt; 16;&lt;br /&gt;v ^= v &amp;gt;&amp;gt; 8;&lt;br /&gt;v ^= v &amp;gt;&amp;gt; 4;&lt;br /&gt;v &amp;amp;= 0xf;&lt;br /&gt;return (0x6996 &amp;gt;&amp;gt; v) &amp;amp; 1;&lt;p&gt;The method above takes around 9 operations, and works for 32-bit words. It may be optimized to work just on bytes in 5 operations by removing the two lines immediately following "unsigned int v;". The method first shifts and XORs the eight nibbles of the 32-bit value together, leaving the result in the lowest nibble of v. Next, the binary number 0110 1001 1001 0110 (0x6996 in hex) is shifted to the right by the value represented in the lowest nibble of v. This number is like a miniature 16-bit parity-table indexed by the low four bits in v. The result has the parity of v in bit 1, which is masked and returned.&lt;/p&gt;&lt;p&gt;Thanks to Mathew Hendry for pointing out the shift-lookup idea at the end on Dec. 15, 2002. That optimization shaves two operations off using only shifting and XORing to find the parity.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="SwappingValuesSubAdd"&gt;&lt;/a&gt;Swapping values with subtraction and addition&lt;/strong&gt;&lt;/p&gt;#define SWAP(a, b) ((&amp;amp;(a) == &amp;amp;(b)) || \&lt;br /&gt;                    (((a) -= (b)), ((b) += (a)), ((a) = (b) - (a))))&lt;p&gt;This swaps the values of a and b &lt;em&gt;without using a temporary variable.&lt;/em&gt;The initial check for a and b being the same location in memory may be omitted when you know this can't happen. (The compiler may omit it anyway as an optimization.) If you enable overflows exceptions, then pass unsigned values so an exception isn't thrown. The XOR method that follows may be slightly faster on some machines. Don't use this with floating-point numbers (unless you operate on their raw integer representations).&lt;/p&gt;&lt;p&gt;Sanjeev Sivasankaran suggested I add this on June 12, 2007. Vincent Lef&amp;egrave;vre pointed out the potential for overflow exceptions on July 9, 2008&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="SwappingValuesXOR"&gt;&lt;/a&gt;Swapping values with XOR&lt;/strong&gt;&lt;/p&gt;#define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))&lt;p&gt;This is an old trick to exchange the values of the variables a and b &lt;em&gt;without using extra space for a temporary variable&lt;/em&gt;.&lt;/p&gt;&lt;p&gt;On January 20, 2005, Iain A. Fleming pointed out that the macro above doesn't work when you swap with the same memory location, such as SWAP(a[i], a[j]) with i == j. So if that may occur, consider defining the macro as (((a) == (b)) || (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))). On July 14, 2009, Hallvard Furuseth suggested that on some machines, (((a) ^ (b)) &amp;amp;&amp;amp; ((b) ^= (a) ^= (b), (a) ^= (b))) might be faster, since the (a) ^ (b) expression is reused.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="SwappingBitsXOR"&gt;&lt;/a&gt;Swapping individual bits with XOR&lt;/strong&gt;&lt;/p&gt;unsigned int i, j; // positions of bit sequences to swap&lt;br /&gt;unsigned int n;    // number of consecutive bits in each sequence&lt;br /&gt;unsigned int b;    // bits to swap reside in b&lt;br /&gt;unsigned int r;    // bit-swapped result goes here&lt;br /&gt;&lt;br /&gt;unsigned int x = ((b &amp;gt;&amp;gt; i) ^ (b &amp;gt;&amp;gt; j)) &amp;amp; ((1U &amp;lt;&amp;lt; n) - 1); // XOR temporary&lt;br /&gt;r = b ^ ((x &amp;lt;&amp;lt; i) | (x &amp;lt;&amp;lt; j));&lt;p&gt;As an example of swapping ranges of bits suppose we have have b = &lt;strong&gt;001&lt;/strong&gt;0&lt;strong&gt;111&lt;/strong&gt;1 (expressed in binary) and we want to swap the n = 3 consecutive bits starting at i = 1 (the second bit from the right) with the 3 consecutive bits starting at j = 5; the result would be r = &lt;strong&gt;111&lt;/strong&gt;0&lt;strong&gt;001&lt;/strong&gt;1 (binary).&lt;/p&gt;&lt;p&gt;This method of swapping is similar to the general purpose XOR swap trick, but intended for operating on individual bits.&amp;nbsp; The variable x stores the result of XORing the pairs of bit values we want to swap, and then the bits are set to the result of themselves XORed with x.&amp;nbsp; Of course, the result is undefined if the sequences overlap.&lt;/p&gt;&lt;p&gt;On July 14, 2009 Hallvard Furuseth suggested that I change the 1 &amp;lt;&amp;lt; n to 1U &amp;lt;&amp;lt; n because the value was being assigned to an unsigned and to avoid shifting into a sign bit.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="BitReverseObvious"&gt;&lt;/a&gt;Reverse bits the obvious way&lt;/strong&gt;&lt;/p&gt;unsigned int v;     // input bits to be reversed&lt;br /&gt;unsigned int r = v; // r will be reversed bits of v; first get LSB of v&lt;br /&gt;int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end&lt;br /&gt;&lt;br /&gt;for (v &amp;gt;&amp;gt;= 1; v; v &amp;gt;&amp;gt;= 1)&lt;br /&gt;{   &lt;br /&gt;  r &amp;lt;&amp;lt;= 1;&lt;br /&gt;  r |= v &amp;amp; 1;&lt;br /&gt;  s--;&lt;br /&gt;}&lt;br /&gt;r &amp;lt;&amp;lt;= s; // shift when v's highest bits are zero&lt;p&gt;On October 15, 2004, Michael Hoisie pointed out a bug in the original version. Randal E. Bryant suggested removing an extra operation on May 3, 2005. Behdad Esfabod suggested a slight change that eliminated one iteration of the loop on May 18, 2005. Then, on February 6, 2007, Liyong Zhou suggested a better version that loops while v is not 0, so rather than iterating over all bits it stops early.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="BitReverseTable"&gt;&lt;/a&gt;Reverse bits in word by lookup table&lt;/strong&gt;&lt;/p&gt;static const unsigned char BitReverseTable256[256] = &lt;br /&gt;{&lt;br /&gt;#   define R2(n)     n,     n + 2*64,     n + 1*64,     n + 3*64&lt;br /&gt;#   define R4(n) R2(n), R2(n + 2*16), R2(n + 1*16), R2(n + 3*16)&lt;br /&gt;#   define R6(n) R4(n), R4(n + 2*4 ), R4(n + 1*4 ), R4(n + 3*4 )&lt;br /&gt;    R6(0), R6(2), R6(1), R6(3)&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;unsigned int v; // reverse 32-bit value, 8 bits at time&lt;br /&gt;unsigned int c; // c will get v reversed&lt;br /&gt;&lt;br /&gt;// Option 1:&lt;br /&gt;c = (BitReverseTable256[v &amp;amp; 0xff] &amp;lt;&amp;lt; 24) | &lt;br /&gt;    (BitReverseTable256[(v &amp;gt;&amp;gt; 8) &amp;amp; 0xff] &amp;lt;&amp;lt; 16) | &lt;br /&gt;    (BitReverseTable256[(v &amp;gt;&amp;gt; 16) &amp;amp; 0xff] &amp;lt;&amp;lt; 8) |&lt;br /&gt;    (BitReverseTable256[(v &amp;gt;&amp;gt; 24) &amp;amp; 0xff]);&lt;br /&gt;&lt;br /&gt;// Option 2:&lt;br /&gt;unsigned char * p = (unsigned char *) &amp;amp;v;&lt;br /&gt;unsigned char * q = (unsigned char *) &amp;amp;c;&lt;br /&gt;q[3] = BitReverseTable256[p[0]]; &lt;br /&gt;q[2] = BitReverseTable256[p[1]]; &lt;br /&gt;q[1] = BitReverseTable256[p[2]]; &lt;br /&gt;q[0] = BitReverseTable256[p[3]];&lt;p&gt;The first method takes about 17 operations, and the second takes about 12, assuming your CPU can load and store bytes easily.&lt;/p&gt;&lt;p&gt;On July 14, 2009 Hallvard Furuseth suggested the macro compacted table.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ReverseByteWith64BitsDiv"&gt;&lt;/a&gt;Reverse the bits in a byte with 3 operations (64-bit multiply and modulus division):&lt;/strong&gt;&lt;/p&gt;unsigned char b; // reverse this (8-bit) byte&lt;br /&gt; &lt;br /&gt;b = (b * 0x0202020202ULL &amp;amp; 0x010884422010ULL) % 1023;&lt;p&gt;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 - 1, has the effect of merging together each set of 10 bits (from positions 0-9, 10-19, 20-29, ...) in the 64-bit value. They do not overlap, so the addition steps underlying the modulus division behave like or operations.&lt;/p&gt;&lt;p&gt;This method was attributed to Rich Schroeppel in the Programming Hacks section of &lt;a href="http://www.inwap.com/pdp10/hbaker/hakmem/hakmem.html"&gt;Beeler, M., Gosper, R. W., and Schroeppel, R. HAKMEM. MIT AI Memo 239, Feb. 29, 1972.&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ReverseByteWith64Bits"&gt;&lt;/a&gt;Reverse the bits in a byte with 4 operations (64-bit multiply, no division):&lt;/strong&gt;&lt;/p&gt;unsigned char b; // reverse this byte&lt;br /&gt; &lt;br /&gt;b = ((b * 0x80200802ULL) &amp;amp; 0x0884422110ULL) * 0x0101010101ULL &amp;gt;&amp;gt; 32;&lt;p&gt;The following shows the flow of the bit values with the boolean variables &lt;code&gt;a, b, c, d, e, f, g,&lt;/code&gt; and &lt;code&gt;h&lt;/code&gt;, which comprise an 8-bit byte. Notice how the first multiply fans out the bit pattern to multiple copies, while the last multiply combines them in the fifth byte from the right.&lt;/p&gt;                                                                                        abcd efgh (-&amp;gt; hgfe dcba)&lt;br /&gt;*                                                      1000 0000  0010 0000  0000 1000  0000 0010 (0x80200802)&lt;br /&gt;-------------------------------------------------------------------------------------------------&lt;br /&gt;                                            0abc defg  h00a bcde  fgh0 0abc  defg h00a  bcde fgh0&lt;br /&gt;&amp;amp;                                           0000 1000  1000 0100  0100 0010  0010 0001  0001 0000 (0x0884422110)&lt;br /&gt;-------------------------------------------------------------------------------------------------&lt;br /&gt;                                            0000 d000  h000 0c00  0g00 00b0  00f0 000a  000e 0000&lt;br /&gt;*                                           0000 0001  0000 0001  0000 0001  0000 0001  0000 0001 (0x0101010101)&lt;br /&gt;-------------------------------------------------------------------------------------------------&lt;br /&gt;                                            0000 d000  h000 0c00  0g00 00b0  00f0 000a  000e 0000&lt;br /&gt;                                 0000 d000  h000 0c00  0g00 00b0  00f0 000a  000e 0000&lt;br /&gt;                      0000 d000  h000 0c00  0g00 00b0  00f0 000a  000e 0000&lt;br /&gt;           0000 d000  h000 0c00  0g00 00b0  00f0 000a  000e 0000&lt;br /&gt;0000 d000  h000 0c00  0g00 00b0  00f0 000a  000e 0000&lt;br /&gt;-------------------------------------------------------------------------------------------------&lt;br /&gt;0000 d000  h000 dc00  hg00 dcb0  hgf0 dcba  hgfe dcba  hgfe 0cba  0gfe 00ba  00fe 000a  000e 0000&lt;br /&gt;&amp;gt;&amp;gt; 32&lt;br /&gt;-------------------------------------------------------------------------------------------------&lt;br /&gt;                                            0000 d000  h000 dc00  hg00 dcb0  hgf0 dcba  hgfe dcba  &lt;br /&gt;&amp;amp;                                                                                       1111 1111&lt;br /&gt;-------------------------------------------------------------------------------------------------&lt;br /&gt;                                                                                        hgfe dcba&lt;p&gt;Note that the last two steps can be combined on some processors because the registers can be accessed as bytes; just multiply so that a register stores the upper 32 bits of the result and the take the low byte. Thus, it may take only 6 operations.&lt;/p&gt;&lt;p&gt;Devised by Sean Anderson, July 13, 2001.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ReverseByteWith32Bits"&gt;&lt;/a&gt;Reverse the bits in a byte with 7 operations (no 64-bit):&lt;/strong&gt;&lt;/p&gt;b = ((b * 0x0802LU &amp;amp; 0x22110LU) | (b * 0x8020LU &amp;amp; 0x88440LU)) * 0x10101LU &amp;gt;&amp;gt; 16; &lt;p&gt;Make sure you assign or cast the result to an unsigned char to remove garbage in the higher bits. Devised by Sean Anderson, July 13, 2001. Typo spotted and correction supplied by Mike Keith, January 3, 2002.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ReverseParallel"&gt;&lt;/a&gt;Reverse an N-bit quantity in parallel in 5 * lg(N) operations:&lt;/strong&gt;&lt;/p&gt;unsigned int v; // 32-bit word to reverse bit order&lt;br /&gt;&lt;br /&gt;// swap odd and even bits&lt;br /&gt;v = ((v &amp;gt;&amp;gt; 1) &amp;amp; 0x55555555) | ((v &amp;amp; 0x55555555) &amp;lt;&amp;lt; 1);&lt;br /&gt;// swap consecutive pairs&lt;br /&gt;v = ((v &amp;gt;&amp;gt; 2) &amp;amp; 0x33333333) | ((v &amp;amp; 0x33333333) &amp;lt;&amp;lt; 2);&lt;br /&gt;// swap nibbles ... &lt;br /&gt;v = ((v &amp;gt;&amp;gt; 4) &amp;amp; 0x0F0F0F0F) | ((v &amp;amp; 0x0F0F0F0F) &amp;lt;&amp;lt; 4);&lt;br /&gt;// swap bytes&lt;br /&gt;v = ((v &amp;gt;&amp;gt; 8) &amp;amp; 0x00FF00FF) | ((v &amp;amp; 0x00FF00FF) &amp;lt;&amp;lt; 8);&lt;br /&gt;// swap 2-byte long pairs&lt;br /&gt;v = ( v &amp;gt;&amp;gt; 16             ) | ( v               &amp;lt;&amp;lt; 16);&lt;p&gt;The following variation is also O(lg(N)), however it requires more operations to reverse v. Its virtue is in taking less slightly memory by computing the constants on the fly.&lt;/p&gt;unsigned int s = sizeof(v) * CHAR_BIT; // bit size; must be power of 2 &lt;br /&gt;unsigned int mask = ~0;         &lt;br /&gt;while ((s &amp;gt;&amp;gt;= 1) &amp;gt; 0) &lt;br /&gt;{&lt;br /&gt;  mask ^= (mask &amp;lt;&amp;lt; s);&lt;br /&gt;  v = ((v &amp;gt;&amp;gt; s) &amp;amp; mask) | ((v &amp;lt;&amp;lt; s) &amp;amp; ~mask);&lt;br /&gt;}&lt;p&gt;These methods above are best suited to situations where N is large. If you use the above with 64-bit ints (or larger), then you need to add more lines (following the pattern); otherwise only the lower 32 bits will be reversed and the result will be in the lower 32 bits.&lt;/p&gt;&lt;p&gt;See Dr. Dobb's Journal 1983, Edwin Freed's article on Binary Magic Numbers for more information. The second variation was suggested by Ken Raeburn on September 13, 2005. Veldmeijer mentioned that the first version could do without ANDS in the last line on March 19, 2006.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ModulusDivisionEasy"&gt;&lt;/a&gt;Compute modulus division by 1 &amp;lt;&amp;lt; s without a division operator&lt;/strong&gt;&lt;/p&gt;const unsigned int n;          // numerator&lt;br /&gt;const unsigned int s;&lt;br /&gt;const unsigned int d = 1U &amp;lt;&amp;lt; s; // So d will be one of: 1, 2, 4, 8, 16, 32, ...&lt;br /&gt;unsigned int m;                // m will be n % d&lt;br /&gt;m = n &amp;amp; (d - 1); &lt;p&gt;Most programmers learn this trick early, but it was included for the sake of completeness.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ModulusDivision"&gt;&lt;/a&gt;Compute modulus division by (1 &amp;lt;&amp;lt; s) - 1 without a division operator&lt;/strong&gt;&lt;/p&gt;unsigned int n;                      // numerator&lt;br /&gt;const unsigned int s;                // s &amp;gt; 0&lt;br /&gt;const unsigned int d = (1 &amp;lt;&amp;lt; s) - 1; // so d is either 1, 3, 7, 15, 31, ...).&lt;br /&gt;unsigned int m;                      // n % d goes here.&lt;br /&gt;&lt;br /&gt;for (m = n; n &amp;gt; d; n = m)&lt;br /&gt;{&lt;br /&gt;  for (m = 0; n; n &amp;gt;&amp;gt;= s)&lt;br /&gt;  {&lt;br /&gt;    m += n &amp;amp; d;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;// Now m is a value from 0 to d, but since with modulus division&lt;br /&gt;// we want m to be 0 when it is d.&lt;br /&gt;m = m == d ? 0 : m;&lt;p&gt;This method of modulus division by an integer that is one less than a power of 2 takes at most 5 + (4 + 5 * ceil(N / s)) * ceil(lg(N / s)) operations, where N is the number of bits in the numerator. In other words, it takes at most O(N * lg(N)) time.&lt;/p&gt;&lt;p&gt;Devised by Sean Anderson, August 15, 2001. Before Sean A. Irvine corrected me on June 17, 2004, I mistakenly commented that we could alternatively assign &lt;code&gt;m = ((m + 1) &amp;amp; d) - 1;&lt;/code&gt; at the end. Michael Miller spotted a typo in the code April 25, 2005.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ModulusDivisionParallel"&gt;&lt;/a&gt;Compute modulus division by (1 &amp;lt;&amp;lt; s) - 1 in parallel without a division operator&lt;/strong&gt;&lt;/p&gt;// The following is for a word size of 32 bits!&lt;br /&gt;&lt;br /&gt;static const unsigned int M[] = &lt;br /&gt;{&lt;br /&gt;  0x00000000, 0x55555555, 0x33333333, 0xc71c71c7,  &lt;br /&gt;  0x0f0f0f0f, 0xc1f07c1f, 0x3f03f03f, 0xf01fc07f, &lt;br /&gt;  0x00ff00ff, 0x07fc01ff, 0x3ff003ff, 0xffc007ff,&lt;br /&gt;  0xff000fff, 0xfc001fff, 0xf0003fff, 0xc0007fff,&lt;br /&gt;  0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff, &lt;br /&gt;  0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,&lt;br /&gt;  0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,&lt;br /&gt;  0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;static const unsigned int Q[][6] = &lt;br /&gt;{&lt;br /&gt;  { 0,  0,  0,  0,  0,  0}, {16,  8,  4,  2,  1,  1}, {16,  8,  4,  2,  2,  2},&lt;br /&gt;  {15,  6,  3,  3,  3,  3}, {16,  8,  4,  4,  4,  4}, {15,  5,  5,  5,  5,  5},&lt;br /&gt;  {12,  6,  6,  6 , 6,  6}, {14,  7,  7,  7,  7,  7}, {16,  8,  8,  8,  8,  8},&lt;br /&gt;  { 9,  9,  9,  9,  9,  9}, {10, 10, 10, 10, 10, 10}, {11, 11, 11, 11, 11, 11},&lt;br /&gt;  {12, 12, 12, 12, 12, 12}, {13, 13, 13, 13, 13, 13}, {14, 14, 14, 14, 14, 14},&lt;br /&gt;  {15, 15, 15, 15, 15, 15}, {16, 16, 16, 16, 16, 16}, {17, 17, 17, 17, 17, 17},&lt;br /&gt;  {18, 18, 18, 18, 18, 18}, {19, 19, 19, 19, 19, 19}, {20, 20, 20, 20, 20, 20},&lt;br /&gt;  {21, 21, 21, 21, 21, 21}, {22, 22, 22, 22, 22, 22}, {23, 23, 23, 23, 23, 23},&lt;br /&gt;  {24, 24, 24, 24, 24, 24}, {25, 25, 25, 25, 25, 25}, {26, 26, 26, 26, 26, 26},&lt;br /&gt;  {27, 27, 27, 27, 27, 27}, {28, 28, 28, 28, 28, 28}, {29, 29, 29, 29, 29, 29},&lt;br /&gt;  {30, 30, 30, 30, 30, 30}, {31, 31, 31, 31, 31, 31}&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;static const unsigned int R[][6] = &lt;br /&gt;{&lt;br /&gt;  {0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000},&lt;br /&gt;  {0x0000ffff, 0x000000ff, 0x0000000f, 0x00000003, 0x00000001, 0x00000001},&lt;br /&gt;  {0x0000ffff, 0x000000ff, 0x0000000f, 0x00000003, 0x00000003, 0x00000003},&lt;br /&gt;  {0x00007fff, 0x0000003f, 0x00000007, 0x00000007, 0x00000007, 0x00000007},&lt;br /&gt;  {0x0000ffff, 0x000000ff, 0x0000000f, 0x0000000f, 0x0000000f, 0x0000000f},&lt;br /&gt;  {0x00007fff, 0x0000001f, 0x0000001f, 0x0000001f, 0x0000001f, 0x0000001f},&lt;br /&gt;  {0x00000fff, 0x0000003f, 0x0000003f, 0x0000003f, 0x0000003f, 0x0000003f},&lt;br /&gt;  {0x00003fff, 0x0000007f, 0x0000007f, 0x0000007f, 0x0000007f, 0x0000007f},&lt;br /&gt;  {0x0000ffff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff, 0x000000ff},&lt;br /&gt;  {0x000001ff, 0x000001ff, 0x000001ff, 0x000001ff, 0x000001ff, 0x000001ff}, &lt;br /&gt;  {0x000003ff, 0x000003ff, 0x000003ff, 0x000003ff, 0x000003ff, 0x000003ff}, &lt;br /&gt;  {0x000007ff, 0x000007ff, 0x000007ff, 0x000007ff, 0x000007ff, 0x000007ff}, &lt;br /&gt;  {0x00000fff, 0x00000fff, 0x00000fff, 0x00000fff, 0x00000fff, 0x00000fff}, &lt;br /&gt;  {0x00001fff, 0x00001fff, 0x00001fff, 0x00001fff, 0x00001fff, 0x00001fff}, &lt;br /&gt;  {0x00003fff, 0x00003fff, 0x00003fff, 0x00003fff, 0x00003fff, 0x00003fff}, &lt;br /&gt;  {0x00007fff, 0x00007fff, 0x00007fff, 0x00007fff, 0x00007fff, 0x00007fff}, &lt;br /&gt;  {0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff, 0x0000ffff}, &lt;br /&gt;  {0x0001ffff, 0x0001ffff, 0x0001ffff, 0x0001ffff, 0x0001ffff, 0x0001ffff}, &lt;br /&gt;  {0x0003ffff, 0x0003ffff, 0x0003ffff, 0x0003ffff, 0x0003ffff, 0x0003ffff}, &lt;br /&gt;  {0x0007ffff, 0x0007ffff, 0x0007ffff, 0x0007ffff, 0x0007ffff, 0x0007ffff},&lt;br /&gt;  {0x000fffff, 0x000fffff, 0x000fffff, 0x000fffff, 0x000fffff, 0x000fffff}, &lt;br /&gt;  {0x001fffff, 0x001fffff, 0x001fffff, 0x001fffff, 0x001fffff, 0x001fffff}, &lt;br /&gt;  {0x003fffff, 0x003fffff, 0x003fffff, 0x003fffff, 0x003fffff, 0x003fffff}, &lt;br /&gt;  {0x007fffff, 0x007fffff, 0x007fffff, 0x007fffff, 0x007fffff, 0x007fffff}, &lt;br /&gt;  {0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff, 0x00ffffff},&lt;br /&gt;  {0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff, 0x01ffffff}, &lt;br /&gt;  {0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff, 0x03ffffff}, &lt;br /&gt;  {0x07ffffff, 0x07ffffff, 0x07ffffff, 0x07ffffff, 0x07ffffff, 0x07ffffff},&lt;br /&gt;  {0x0fffffff, 0x0fffffff, 0x0fffffff, 0x0fffffff, 0x0fffffff, 0x0fffffff},&lt;br /&gt;  {0x1fffffff, 0x1fffffff, 0x1fffffff, 0x1fffffff, 0x1fffffff, 0x1fffffff}, &lt;br /&gt;  {0x3fffffff, 0x3fffffff, 0x3fffffff, 0x3fffffff, 0x3fffffff, 0x3fffffff}, &lt;br /&gt;  {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff}&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;unsigned int n;       // numerator&lt;br /&gt;const unsigned int s; // s &amp;gt; 0&lt;br /&gt;const unsigned int d = (1 &amp;lt;&amp;lt; s) - 1; // so d is either 1, 3, 7, 15, 31, ...).&lt;br /&gt;unsigned int m;       // n % d goes here.&lt;br /&gt;&lt;br /&gt;m = (n &amp;amp; M[s]) + ((n &amp;gt;&amp;gt; s) &amp;amp; M[s]);&lt;br /&gt;&lt;br /&gt;for (const unsigned int * q = &amp;amp;Q[s][0], * r = &amp;amp;R[s][0]; m &amp;gt; d; q++, r++)&lt;br /&gt;{&lt;br /&gt;  m = (m &amp;gt;&amp;gt; *q) + (m &amp;amp; *r);&lt;br /&gt;}&lt;br /&gt;m = m == d ? 0 : m; // OR, less portably: m = m &amp;amp; -((signed)(m - d) &amp;gt;&amp;gt; s);&lt;p&gt;This method of finding modulus division by an integer that is one less than a power of 2 takes at most O(lg(N)) time, where N is the number of bits in the numerator (32 bits, for the code above). The number of operations is at most 12 + 9 * ceil(lg(N)). The tables may be removed if you know the denominator at compile time; just extract the few relevent entries and unroll the loop. It may be easily extended to more bits.&lt;/p&gt;&lt;p&gt;It finds the result by summing the values in base (1 &amp;lt;&amp;lt; s) in parallel. First every other base (1 &amp;lt;&amp;lt; s) value is added to the previous one. Imagine that the result is written on a piece of paper. Cut the paper in half, so that half the values are on each cut piece. Align the values and sum them onto a new piece of paper. Repeat by cutting this paper in half (which will be a quarter of the size of the previous one) and summing, until you cannot cut further. After performing lg(N/s/2) cuts, we cut no more; just continue to add the values and put the result onto a new piece of paper as before, while there are at least two s-bit values.&lt;/p&gt;&lt;p&gt;Devised by Sean Anderson, August 20, 2001. A typo was spotted by Randy E. Bryant on May 3, 2005 (after pasting the code, I had later added "unsinged" to a variable declaration). As in the previous hack, I mistakenly commented that we could alternatively assign &lt;code&gt;m = ((m + 1) &amp;amp; d) - 1;&lt;/code&gt; at the end, and Don Knuth corrected me on April 19, 2006 and suggested &lt;code&gt;m = m &amp;amp; -((signed)(m - d) &amp;gt;&amp;gt; s)&lt;/code&gt;. On June 18, 2009 Sean Irvine proposed a change that used &lt;code&gt;((n &amp;gt;&amp;gt; s) &amp;amp; M[s])&lt;/code&gt; instead of &lt;code&gt;((n &amp;amp; ~M[s]) &amp;gt;&amp;gt; s)&lt;/code&gt;, which typically requires fewer operations because the M[s] constant is already loaded.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="IntegerLogObvious"&gt;&lt;/a&gt;Find the log base 2 of an integer with the MSB N set in O(N) operations (the obvious way)&lt;/strong&gt;&lt;/p&gt;unsigned int v; // 32-bit word to find the log base 2 of&lt;br /&gt;unsigned int r = 0; // r will be lg(v)&lt;br /&gt;&lt;br /&gt;while (v &amp;gt;&amp;gt;= 1) // unroll for more speed...&lt;br /&gt;{&lt;br /&gt;  r++;&lt;br /&gt;}&lt;p&gt;The log base 2 of an integer is the same as the position of the highest bit set (or most significant bit set, MSB). The following log base 2 methods are faster than this one.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="IntegerLogIEEE64Float"&gt;&lt;/a&gt;Find the integer log base 2 of an integer with an 64-bit IEEE float&lt;/strong&gt;&lt;/p&gt;int v; // 32-bit integer to find the log base 2 of&lt;br /&gt;int r; // result of log_2(v) goes here&lt;br /&gt;union { unsigned int u[2]; double d; } t; // temp&lt;br /&gt;&lt;br /&gt;t.u[__FLOAT_WORD_ORDER==LITTLE_ENDIAN] = 0x43300000;&lt;br /&gt;t.u[__FLOAT_WORD_ORDER!=LITTLE_ENDIAN] = v;&lt;br /&gt;t.d -= 4503599627370496.0;&lt;br /&gt;r = (t.u[__FLOAT_WORD_ORDER==LITTLE_ENDIAN] &amp;gt;&amp;gt; 20) - 0x3FF;&lt;p&gt;The code above loads a 64-bit (IEEE-754 floating-point) double with a 32-bit integer (with no paddding bits) by storing the integer in the mantissa while the exponent is set to 2&lt;sup&gt;52&lt;/sup&gt;. From this newly minted double, 2&lt;sup&gt;52&lt;/sup&gt;(expressed as a double) is subtracted, which sets the resulting exponent to the log base 2 of the input value, v. All that is left is shifting the exponent bits into position (20 bits right) and subtracting the bias, 0x3FF (which is 1023 decimal). This technique only takes 5 operations, but many CPUs are slow at manipulating doubles, and the endianess of the architecture must be accommodated.&lt;/p&gt;&lt;p&gt;Eric Cole sent me this on January 15, 2006. Evan Felix pointed out a typo on April 4, 2006. Vincent Lef&amp;egrave;vre told me on July 9, 2008 to change the endian check to use the float's endian, which could differ from the integer's endian.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="IntegerLogLookup"&gt;&lt;/a&gt;Find the log base 2 of an integer with a lookup table&lt;/strong&gt;&lt;/p&gt;static const char LogTable256[256] = &lt;br /&gt;{&lt;br /&gt;#define LT(n) n, n, n, n, n, n, n, n, n, n, n, n, n, n, n, n&lt;br /&gt;    -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,&lt;br /&gt;    LT(4), LT(5), LT(5), LT(6), LT(6), LT(6), LT(6),&lt;br /&gt;    LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7), LT(7)&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;unsigned int v; // 32-bit word to find the log of&lt;br /&gt;unsigned r;     // r will be lg(v)&lt;br /&gt;register unsigned int t, tt; // temporaries&lt;br /&gt;&lt;br /&gt;if (tt = v &amp;gt;&amp;gt; 16)&lt;br /&gt;{&lt;br /&gt;  r = (t = tt &amp;gt;&amp;gt; 8) ? 24 + LogTable256[t] : 16 + LogTable256[tt];&lt;br /&gt;}&lt;br /&gt;else &lt;br /&gt;{&lt;br /&gt;  r = (t = v &amp;gt;&amp;gt; 8) ? 8 + LogTable256[t] : LogTable256[v];&lt;br /&gt;}&lt;p&gt;The lookup table method takes only about 7 operations to find the log of a 32-bit value. If extended for 64-bit quantities, it would take roughly 9 operations. Another operation can be trimmed off by using four tables, with the possible additions incorporated into each. Using int table elements may be faster, depending on your architecture.&lt;/p&gt;&lt;p&gt;The code above is tuned to uniformly distributed &lt;em&gt;output&lt;/em&gt; values. If your &lt;em&gt;inputs&lt;/em&gt; are evenly distributed across all 32-bit values, then consider using the following:&lt;/p&gt;if (tt = v &amp;gt;&amp;gt; 24) &lt;br /&gt;{&lt;br /&gt;  r = 24 + LogTable256[tt];&lt;br /&gt;} &lt;br /&gt;else if (tt = v &amp;gt;&amp;gt; 16) &lt;br /&gt;{&lt;br /&gt;  r = 16 + LogTable256[tt];&lt;br /&gt;} &lt;br /&gt;else if (tt = v &amp;gt;&amp;gt; 8) &lt;br /&gt;{&lt;br /&gt;  r = 8 + LogTable256[tt];&lt;br /&gt;} &lt;br /&gt;else &lt;br /&gt;{&lt;br /&gt;  r = LogTable256[v];&lt;br /&gt;}&lt;p&gt;To initially generate the log table algorithmically:&lt;/p&gt;LogTable256[0] = LogTable256[1] = 0;&lt;br /&gt;for (int i = 2; i &amp;lt; 256; i++) &lt;br /&gt;{&lt;br /&gt;  LogTable256[i] = 1 + LogTable256[i / 2];&lt;br /&gt;}&lt;br /&gt;LogTable256[0] = -1; // if you want log(0) to return -1&lt;p&gt;Behdad Esfahbod and I shaved off a fraction of an operation (on average) on May 18, 2005. Yet another fraction of an operation was removed on November 14, 2006 by Emanuel Hoogeveen. The variation that is tuned to evenly distributed input values was suggested by David A. Butterfield on September 19, 2008. Venkat Reddy told me on January 5, 2009 that log(0) should return -1 to indicate an error, so I changed the first entry in the table to that.&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="IntegerLog"&gt;&lt;/a&gt;Find the log base 2 of an N-bit integer in O(lg(N)) operations&lt;/strong&gt;&lt;/p&gt;unsigned int v;  // 32-bit value to find the log2 of &lt;br /&gt;const unsigned int b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};&lt;br /&gt;const unsigned int S[] = {1, 2, 4, 8, 16};&lt;br /&gt;int i;&lt;br /&gt;&lt;br /&gt;register unsigned int r = 0; // result of log2(v) will go here&lt;br /&gt;for (i = 4; i &amp;gt;= 0; i--) // unroll for speed...&lt;br /&gt;{&lt;br /&gt;  if (v &amp;amp; b[i])&lt;br /&gt;  {&lt;br /&gt;    v &amp;gt;&amp;gt;= S[i];&lt;br /&gt;    r |= S[i];&lt;br /&gt;  } &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// OR (IF YOUR CPU BRANCHES SLOWLY):&lt;br /&gt;&lt;br /&gt;unsigned int v;         // 32-bit value to find the log2 of &lt;br /&gt;register unsigned int r; // result of log2(v) will go here&lt;br /&gt;register unsigned int shift;&lt;br /&gt;&lt;br /&gt;r =     (v &amp;gt; 0xFFFF) &amp;lt;&amp;lt; 4; v &amp;gt;&amp;gt;= r;&lt;br /&gt;shift = (v &amp;gt; 0xFF  ) &amp;lt;&amp;lt; 3; v &amp;gt;&amp;gt;= shift; r |= shift;&lt;br /&gt;shift = (v &amp;gt; 0xF   ) &amp;lt;&amp;lt; 2; v &amp;gt;&amp;gt;= shift; r |= shift;&lt;br /&gt;shift = (v &amp;gt; 0x3   ) &amp;lt;&amp;lt; 1; v &amp;gt;&amp;gt;= shift; r |= shift;&lt;br /&gt;                                        r |= (v &amp;gt;&amp;gt; 1);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// OR (IF YOU KNOW v IS A POWER OF 2):&lt;br /&gt;&lt;br /&gt;unsigned int v;  // 32-bit value to find the log2 of &lt;br /&gt;static const unsigned int b[] = {0xAAAAAAAA, 0xCCCCCCCC, 0xF0F0F0F0, &lt;br /&gt;                                 0xFF00FF00, 0xFFFF0000};&lt;br /&gt;register unsigned int r = (v &amp;amp; b[0]) != 0;&lt;br /&gt;for (i = 4; i &amp;gt; 0; i--) // unroll for speed...&lt;br /&gt;{&lt;br /&gt;  r |= ((v &amp;amp; b[i]) != 0) &amp;lt;&amp;lt; i;&lt;br /&gt;}&lt;p&gt;Of course, to extend the code to find the log of a 33- to 64-bit number, we would append another element, 0xFFFFFFFF00000000, to b, append 32 to S, and loop from 5 to 0. This method is much slower than the earlier table-lookup version, but if you don't want big table or your architecture is slow to access memory, it's a good choice. The second variation involves slightly more operations, but it may be faster on machines with high branch costs (e.g. PowerPC).&lt;/p&gt;&lt;p&gt;The second version was sent to me by &lt;a href="http://www.balance-software.com/ec/"&gt;Eric Cole&lt;/a&gt; on January 7, 2006. Andrew Shapira subsequently trimmed a few operations off of it and sent me his variation (above) on Sept. 1, 2007. The third variation was suggested to me by &lt;a href="http://www.ece.ucdavis.edu/%7Ejowens/"&gt;John Owens&lt;/a&gt; on April 24, 2002; it's faster, but &lt;em&gt;it is only suitable when the input is known to be a power of 2&lt;/em&gt;. On May 25, 2003, Ken Raeburn suggested improving the general case by using smaller numbers for b[], which load faster on some architectures (for instance if the word size is 16 bits, then only one load instruction may be needed). These values work for the general version, but not for the special-case version below it, where v is a power of 2; Glenn Slayden brought this oversight to my attention on December 12, 2003.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="IntegerLogDeBruijn"&gt;&lt;/a&gt;Find the log base 2 of an N-bit integer in O(lg(N)) operations with multiply and lookup&lt;/strong&gt;&lt;/p&gt;uint32_t v; // find the log base 2 of 32-bit v&lt;br /&gt;int r;      // result goes here&lt;br /&gt;&lt;br /&gt;static const int MultiplyDeBruijnBitPosition[32] = &lt;br /&gt;{&lt;br /&gt;  0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,&lt;br /&gt;  8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;v |= v &amp;gt;&amp;gt; 1; // first round down to one less than a power of 2 &lt;br /&gt;v |= v &amp;gt;&amp;gt; 2;&lt;br /&gt;v |= v &amp;gt;&amp;gt; 4;&lt;br /&gt;v |= v &amp;gt;&amp;gt; 8;&lt;br /&gt;v |= v &amp;gt;&amp;gt; 16;&lt;br /&gt;&lt;br /&gt;r = MultiplyDeBruijnBitPosition[(uint32_t)(v * 0x07C4ACDDU) &amp;gt;&amp;gt; 27];&lt;p&gt;The code above computes the log base 2 of a 32-bit integer with a small table lookup and multiply. It requires only 13 operations, compared to (up to) 20 for the previous method. The purely table-based method requires the fewest operations, but this offers a reasonable compromise between table size and speed.&lt;/p&gt;&lt;p&gt;If you know that v is a power of 2, then you only need the following:&lt;/p&gt;static const int MultiplyDeBruijnBitPosition2[32] = &lt;br /&gt;{&lt;br /&gt;  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, &lt;br /&gt;  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9&lt;br /&gt;};&lt;br /&gt;r = MultiplyDeBruijnBitPosition2[(uint32_t)(v * 0x077CB531U) &amp;gt;&amp;gt; 27];&lt;p&gt;Eric Cole devised this January 8, 2006 after reading about the entry below to &lt;a href="http://graphics.stanford.edu/%7Eseander/bithacks.html#RoundUpPowerOf2"&gt;round up to a power of 2&lt;/a&gt; and the method below for &lt;a href="http://graphics.stanford.edu/%7Eseander/bithacks.html#ZerosOnRightMultLookup"&gt;computing the number of trailing bits with a multiply and lookup&lt;/a&gt; using a DeBruijn sequence. On December 10, 2009, Mark Dickinson shaved off a couple operations by requiring v be rounded up to one less than the next power of 2 rather than the power of 2.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="IntegerLog10"&gt;&lt;/a&gt;Find integer log base 10 of an integer&lt;/strong&gt;&lt;/p&gt;unsigned int v; // non-zero 32-bit integer value to compute the log base 10 of &lt;br /&gt;int r;          // result goes here&lt;br /&gt;int t;          // temporary&lt;br /&gt;&lt;br /&gt;static unsigned int const PowersOf10[] = &lt;br /&gt;    {1, 10, 100, 1000, 10000, 100000,&lt;br /&gt;     1000000, 10000000, 100000000, 1000000000};&lt;br /&gt;&lt;br /&gt;t = (IntegerLogBase2(v) + 1) * 1233 &amp;gt;&amp;gt; 12; // (use a lg2 method from above)&lt;br /&gt;r = t - (v &amp;lt; PowersOf10[t]);&lt;p&gt;The integer log base 10 is computed by first using one of the techniques above for finding the log base 2. By the relationship log&lt;sub&gt;10&lt;/sub&gt;(v) = log&lt;sub&gt;2&lt;/sub&gt;(v) / log&lt;sub&gt;2&lt;/sub&gt;(10), we need to multiply it by 1/log&lt;sub&gt;2&lt;/sub&gt;(10), which is approximately 1233/4096, or 1233 followed by a right shift of 12. Adding one is needed because the IntegerLogBase2 rounds down. Finally, since the value t is only an approximation that may be off by one, the exact value is found by subtracting the result of v &amp;lt; PowersOf10[t].&lt;/p&gt;&lt;p&gt;This method takes 6 more operations than IntegerLogBase2. It may be sped up (on machines with fast memory access) by modifying the log base 2 table-lookup method above so that the entries hold what is computed for t (that is, pre-add, -mulitply, and -shift). Doing so would require a total of only 9 operations to find the log base 10, assuming 4 tables were used (one for each byte of v).&lt;/p&gt;&lt;p&gt;Eric Cole suggested I add a version of this on January 7, 2006.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="IntegerLog10Obvious"&gt;&lt;/a&gt;Find integer log base 10 of an integer the obvious way&lt;/strong&gt;&lt;/p&gt;unsigned int v; // non-zero 32-bit integer value to compute the log base 10 of &lt;br /&gt;int r;          // result goes here&lt;br /&gt;&lt;br /&gt;r = (v &amp;gt;= 1000000000) ? 9 : (v &amp;gt;= 100000000) ? 8 : (v &amp;gt;= 10000000) ? 7 : &lt;br /&gt;    (v &amp;gt;= 1000000) ? 6 : (v &amp;gt;= 100000) ? 5 : (v &amp;gt;= 10000) ? 4 : &lt;br /&gt;    (v &amp;gt;= 1000) ? 3 : (v &amp;gt;= 100) ? 2 : (v &amp;gt;= 10) ? 1 : 0;&lt;p&gt;This method works well when the input is uniformly distributed over 32-bit values because 76% of the inputs are caught by the first compare, 21% are caught by the second compare, 2% are caught by the third, and so on (chopping the remaining down by 90% with each comparision). As a result, less than 2.6 operations are needed on average.&lt;/p&gt;&lt;p&gt;On April 18, 2007, Emanuel Hoogeveen suggested a variation on this where the conditions used divisions, which were not as fast as simple comparisons.&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="IntegerLogFloat"&gt;&lt;/a&gt;Find integer log base 2 of a 32-bit IEEE float&lt;/strong&gt;&lt;/p&gt;const float v; // find int(log2(v)), where v &amp;gt; 0.0 &amp;amp;&amp;amp; finite(v) &amp;amp;&amp;amp; isnormal(v)&lt;br /&gt;int c;         // 32-bit int c gets the result;&lt;br /&gt;&lt;br /&gt;c = *(const int *) &amp;amp;v;  // OR, for portability:  memcpy(&amp;amp;c, &amp;amp;v, sizeof c);&lt;br /&gt;c = (c &amp;gt;&amp;gt; 23) - 127;&lt;p&gt;The above is fast, but IEEE 754-compliant architectures utilize &lt;em&gt;subnormal&lt;/em&gt; (also called &lt;em&gt;denormal&lt;/em&gt;) floating point numbers. These have the exponent bits set to zero (signifying pow(2,-127)), and the mantissa is not normalized, so it contains leading zeros and thus the log2 must be computed from the mantissa. To accomodate for subnormal numbers, use the following:&lt;/p&gt;const float v;              // find int(log2(v)), where v &amp;gt; 0.0 &amp;amp;&amp;amp; finite(v)&lt;br /&gt;int c;                      // 32-bit int c gets the result;&lt;br /&gt;int x = *(const int *) &amp;amp;v;  // OR, for portability:  memcpy(&amp;amp;x, &amp;amp;v, sizeof x);&lt;br /&gt;&lt;br /&gt;c = x &amp;gt;&amp;gt; 23;          &lt;br /&gt;&lt;br /&gt;if (c)&lt;br /&gt;{&lt;br /&gt;  c -= 127;&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{ // subnormal, so recompute using mantissa: c = intlog2(x) - 149;&lt;br /&gt;  register unsigned int t; // temporary&lt;br /&gt;  // Note that LogTable256 was defined &lt;a href="http://graphics.stanford.edu/%7Eseander/bithacks.html#IntegerLogLookup"&gt;earlier&lt;/a&gt;&lt;br/&gt;  if (t = x &amp;gt;&amp;gt; 16)&lt;br/&gt;  {&lt;br/&gt;    c = LogTable256[t] - 133;&lt;br/&gt;  }&lt;br/&gt;  else&lt;br/&gt;  {&lt;br/&gt;    c = (t = x &amp;gt;&amp;gt; 8) ? LogTable256[t] - 141 : LogTable256[x] - 149;&lt;br/&gt;  }&lt;br/&gt;}&lt;br/&gt;&lt;p&gt;On June 20, 2004, Sean A. Irvine suggested that I include code to handle subnormal numbers. On June 11, 2005, Falk H&amp;uuml;ffner pointed out that ISO C99 6.5/7 specified undefined behavior for the common type punning idiom *(int *)&amp;amp;, though it has worked on 99.9% of C compilers. He proposed using memcpy for maximum portability or a union with a float and an int for better code generation than memcpy on some compilers.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="IntegerLogRootFloat"&gt;&lt;/a&gt;Find integer log base 2 of the pow(2, r)-root of a 32-bit IEEE float (for unsigned integer r)&lt;/strong&gt;&lt;/p&gt;const int r;&lt;br /&gt;const float v; // find int(log2(pow((double) v, 1. / pow(2, r)))), &lt;br /&gt;               // where isnormal(v) and v &amp;gt; 0&lt;br /&gt;int c;         // 32-bit int c gets the result;&lt;br /&gt;&lt;br /&gt;c = *(const int *) &amp;amp;v;  // OR, for portability:  memcpy(&amp;amp;c, &amp;amp;v, sizeof c);&lt;br /&gt;c = ((((c - 0x3f800000) &amp;gt;&amp;gt; r) + 0x3f800000) &amp;gt;&amp;gt; 23) - 127;&lt;p&gt;So, if r is 0, for example, we have c = int(log2((double) v)). If r is 1, then we have c = int(log2(sqrt((double) v))). If r is 2, then we have c = int(log2(pow((double) v, 1./4))).&lt;/p&gt;&lt;p&gt;On June 11, 2005, Falk H&amp;uuml;ffner pointed out that ISO C99 6.5/7 left the type punning idiom *(int *)&amp;amp; undefined, and he suggested using memcpy.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ZerosOnRightLinear"&gt;&lt;/a&gt;Count the consecutive zero bits (trailing) on the right linearly&lt;/strong&gt;&lt;/p&gt;unsigned int v;  // input to count trailing zero bits&lt;br /&gt;int c;  // output: c will count v's trailing zero bits,&lt;br /&gt;        // so if v is 1101000 (base 2), then c will be 3&lt;br /&gt;if (v)&lt;br /&gt;{&lt;br /&gt;  v = (v ^ (v - 1)) &amp;gt;&amp;gt; 1;  // Set v's trailing 0s to 1s and zero rest&lt;br /&gt;  for (c = 0; v; c++)&lt;br /&gt;  {&lt;br /&gt;    v &amp;gt;&amp;gt;= 1;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;  c = CHAR_BIT * sizeof(v);&lt;br /&gt;}&lt;p&gt;The average number of trailing zero bits in a (uniformly distributed) random binary number is one, so this O(trailing zeros) solution isn't that bad compared to the faster methods below.&lt;/p&gt;&lt;p&gt;Jim Cole suggested I add a linear-time method for counting the trailing zeros on August 15, 2007. On October 22, 2007, Jason Cunningham pointed out that I had neglected to paste the unsigned modifier for v.&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ZerosOnRightParallel"&gt;&lt;/a&gt;Count the consecutive zero bits (trailing) on the right in parallel&lt;/strong&gt;&lt;/p&gt;unsigned int v;      // 32-bit word input to count zero bits on right&lt;br /&gt;unsigned int c = 32; // c will be the number of zero bits on the right&lt;br /&gt;v &amp;amp;= -signed(v);&lt;br /&gt;if (v) c--;&lt;br /&gt;if (v &amp;amp; 0x0000FFFF) c -= 16;&lt;br /&gt;if (v &amp;amp; 0x00FF00FF) c -= 8;&lt;br /&gt;if (v &amp;amp; 0x0F0F0F0F) c -= 4;&lt;br /&gt;if (v &amp;amp; 0x33333333) c -= 2;&lt;br /&gt;if (v &amp;amp; 0x55555555) c -= 1;&lt;p&gt;Here, we are basically doing the same operations as finding the log base 2 in parallel, but we first isolate the lowest 1 bit, and then proceed with c starting at the maximum and decreasing. The number of operations is at most 3 * lg(N) + 4, roughly, for N bit words.&lt;/p&gt;&lt;p&gt;Bill Burdick suggested an optimization, reducing the time from 4 * lg(N) on February 4, 2011.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ZerosOnRightBinSearch"&gt;&lt;/a&gt;Count the consecutive zero bits (trailing) on the right by binary search&lt;/strong&gt;&lt;/p&gt;unsigned int v;     // 32-bit word input to count zero bits on right&lt;br /&gt;unsigned int c;     // c will be the number of zero bits on the right,&lt;br /&gt;                    // so if v is 1101000 (base 2), then c will be 3&lt;br /&gt;// NOTE: if 0 == v, then c = 31.&lt;br /&gt;if (v &amp;amp; 0x1) &lt;br /&gt;{&lt;br /&gt;  // special case for odd v (assumed to happen half of the time)&lt;br /&gt;  c = 0;&lt;br /&gt;}&lt;br /&gt;else&lt;br /&gt;{&lt;br /&gt;  c = 1;&lt;br /&gt;  if ((v &amp;amp; 0xffff) == 0) &lt;br /&gt;  {  &lt;br /&gt;    v &amp;gt;&amp;gt;= 16;  &lt;br /&gt;    c += 16;&lt;br /&gt;  }&lt;br /&gt;  if ((v &amp;amp; 0xff) == 0) &lt;br /&gt;  {  &lt;br /&gt;    v &amp;gt;&amp;gt;= 8;  &lt;br /&gt;    c += 8;&lt;br /&gt;  }&lt;br /&gt;  if ((v &amp;amp; 0xf) == 0) &lt;br /&gt;  {  &lt;br /&gt;    v &amp;gt;&amp;gt;= 4;&lt;br /&gt;    c += 4;&lt;br /&gt;  }&lt;br /&gt;  if ((v &amp;amp; 0x3) == 0) &lt;br /&gt;  {  &lt;br /&gt;    v &amp;gt;&amp;gt;= 2;&lt;br /&gt;    c += 2;&lt;br /&gt;  }&lt;br /&gt;  c -= v &amp;amp; 0x1;&lt;br /&gt;}&lt;p&gt;The code above is similar to the previous method, but it computes the number of trailing zeros by accumulating c in a manner akin to binary search. In the first step, it checks if the bottom 16 bits of v are zeros, and if so, shifts v right 16 bits and adds 16 to c, which reduces the number of bits in v to consider by half. Each of the subsequent conditional steps likewise halves the number of bits until there is only 1. This method is faster than the last one (by about 33%) because the bodies of the if statements are executed less often.&lt;/p&gt;&lt;p&gt;Matt Whitlock suggested this on January 25, 2006. Andrew Shapira shaved a couple operations off on Sept. 5, 2007 (by setting c=1 and unconditionally subtracting at the end).&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ZerosOnRightFloatCast"&gt;&lt;/a&gt;Count the consecutive zero bits (trailing) on the right by casting to a float&lt;/strong&gt;&lt;/p&gt;unsigned int v;            // find the number of trailing zeros in v&lt;br /&gt;int r;                     // the result goes here&lt;br /&gt;float f = (float)(v &amp;amp; -v); // cast the least significant bit in v to a float&lt;br /&gt;r = (*(uint32_t *)&amp;amp;f &amp;gt;&amp;gt; 23) - 0x7f;&lt;p&gt;Although this only takes about 6 operations, the time to convert an integer to a float can be high on some machines. The exponent of the 32-bit IEEE floating point representation is shifted down, and the bias is subtracted to give the position of the least significant 1 bit set in v. If v is zero, then the result is -127.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ZerosOnRightModLookup"&gt;&lt;/a&gt;Count the consecutive zero bits (trailing) on the right with modulus division and lookup&lt;/strong&gt;&lt;/p&gt;unsigned int v;  // find the number of trailing zeros in v&lt;br /&gt;int r;           // put the result in r&lt;br /&gt;static const int Mod37BitPosition[] = // map a bit value mod 37 to its position&lt;br /&gt;{&lt;br /&gt;  32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4,&lt;br /&gt;  7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5,&lt;br /&gt;  20, 8, 19, 18&lt;br /&gt;};&lt;br /&gt;r = Mod37BitPosition[(-v &amp;amp; v) % 37];&lt;p&gt;The code above finds the number of zeros that are trailing on the right, so binary 0100 would produce 2. It makes use of the fact that the first 32 bit position values are relatively prime with 37, so performing a modulus division with 37 gives a unique number from 0 to 36 for each. These numbers may then be mapped to the number of zeros using a small lookup table. It uses only 4 operations, however indexing into a table and performing modulus division may make it unsuitable for some situations. I came up with this independently and then searched for a subsequence of the table values, and found it was invented earlier by Reiser, according to &lt;a href="http://www.hackersdelight.org/HDcode/ntz.c.txt"&gt;Hacker's Delight&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ZerosOnRightMultLookup"&gt;&lt;/a&gt;Count the consecutive zero bits (trailing) on the right with multiply and lookup&lt;/strong&gt;&lt;/p&gt;unsigned int v;  // find the number of trailing zeros in 32-bit v &lt;br /&gt;int r;           // result goes here&lt;br /&gt;static const int MultiplyDeBruijnBitPosition[32] = &lt;br /&gt;{&lt;br /&gt;  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, &lt;br /&gt;  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9&lt;br /&gt;};&lt;br /&gt;r = MultiplyDeBruijnBitPosition[((uint32_t)((v &amp;amp; -v) * 0x077CB531U)) &amp;gt;&amp;gt; 27];&lt;p&gt;Converting bit vectors to indices of set bits is an example use for this. It requires one more operation than the earlier one involving modulus division, but the multiply may be faster. The expression (v &amp;amp; -v) extracts the least significant 1 bit from v. The constant 0x077CB531UL is a de Bruijn sequence, which produces a unique pattern of bits into the high 5 bits for each possible bit position that it is multiplied against. When there are no bits set, it returns 0. More information can be found by reading the paper &lt;a href="http://citeseer.ist.psu.edu/leiserson98using.html"&gt;Using de Bruijn Sequences to Index 1 in a Computer Word&lt;/a&gt;by Charles E. Leiserson, Harald Prokof, and Keith H. Randall.&lt;/p&gt;&lt;p&gt;On October 8, 2005 &lt;a href="http://onezero.org/"&gt;Andrew Shapira&lt;/a&gt; suggested I add this. Dustin Spicuzza asked me on April 14, 2009 to cast the result of the multiply to a 32-bit type so it would work when compiled with 64-bit ints.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="RoundUpPowerOf2Float"&gt;&lt;/a&gt;Round up to the next highest power of 2 by float casting&lt;/strong&gt;&lt;/p&gt;unsigned int const v; // Round this 32-bit value to the next highest power of 2&lt;br /&gt;unsigned int r;       // Put the result here. (So v=3 -&amp;gt; r=4; v=8 -&amp;gt; r=8)&lt;br /&gt;&lt;br /&gt;if (v &amp;gt; 1) &lt;br /&gt;{&lt;br /&gt;  float f = (float)v;&lt;br /&gt;  unsigned int const t = 1U &amp;lt;&amp;lt; ((*(unsigned int *)&amp;amp;f &amp;gt;&amp;gt; 23) - 0x7f);&lt;br /&gt;  r = t &amp;lt;&amp;lt; (t &amp;lt; v);&lt;br /&gt;}&lt;br /&gt;else &lt;br /&gt;{&lt;br /&gt;  r = 1;&lt;br /&gt;}&lt;p&gt;The code above uses 8 operations, but works on all v &amp;lt;= (1&amp;lt;&amp;lt;31).&lt;/p&gt;&lt;p&gt;Quick and dirty version, for domain of 1 &amp;lt; v &amp;lt; (1&amp;lt;&amp;lt;25):&lt;/p&gt;float f = (float)(v - 1);  &lt;br /&gt;r = 1U &amp;lt;&amp;lt; ((*(unsigned int*)(&amp;amp;f) &amp;gt;&amp;gt; 23) - 126);&lt;p&gt;Although the quick and dirty version only uses around 6 operations, it is roughly three times slower than the &lt;a href="http://graphics.stanford.edu/%7Eseander/bithacks.html#RoundUpPowerOf2"&gt;technique below&lt;/a&gt;(which involves 12 operations) when benchmarked on an Athlon&amp;trade; XP 2100+ CPU. Some CPUs will fare better with it, though.&lt;/p&gt;&lt;p&gt;On September 27, 2005 Andi Smithers suggested I include a technique for casting to floats to find the lg of a number for rounding up to a power of 2. Similar to the quick and dirty version here, his version worked with values less than (1&amp;lt;&amp;lt;25), due to mantissa rounding, but it used one more operation.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="RoundUpPowerOf2"&gt;&lt;/a&gt;Round up to the next highest power of 2&lt;/strong&gt;&lt;/p&gt;unsigned int v; // compute the next highest power of 2 of 32-bit v&lt;br /&gt;&lt;br /&gt;v--;&lt;br /&gt;v |= v &amp;gt;&amp;gt; 1;&lt;br /&gt;v |= v &amp;gt;&amp;gt; 2;&lt;br /&gt;v |= v &amp;gt;&amp;gt; 4;&lt;br /&gt;v |= v &amp;gt;&amp;gt; 8;&lt;br /&gt;v |= v &amp;gt;&amp;gt; 16;&lt;br /&gt;v++;&lt;p&gt;In 12 operations, this code computes the next highest power of 2 for a 32-bit integer. The result may be expressed by the formula 1U &amp;lt;&amp;lt; (lg(v - 1) + 1). Note that in the edge case where v is 0, it returns 0, which isn't a power of 2; you might append the expression v += (v == 0) to remedy this if it matters. It would be faster by 2 operations to use the formula and the log base 2 methed that uses a lookup table, but in some situations, lookup tables are not suitable, so the above code may be best. (On a Athlon&amp;trade; XP 2100+ I've found the above shift-left and then OR code is as fast as using a single BSR assembly language instruction, which scans in reverse to find the highest set bit.) It works by copying the highest set bit to all of the lower bits, and then adding one, which results in carries that set all of the lower bits to 0 and one bit beyond the highest set bit to 1. If the original number was a power of 2, then the decrement will reduce it to one less, so that we round up to the same original value.&lt;/p&gt;&lt;p&gt;You might alternatively compute the next higher power of 2 in only 8 or 9 operations using a lookup table for floor(lg(v)) and then evaluating 1&amp;lt;&amp;lt;(1+floor(lg(v))); Atul Divekar suggested I mention this on September 5, 2010.&lt;/p&gt;&lt;p&gt;Devised by Sean Anderson, Sepember 14, 2001. Pete Hart pointed me to &lt;a href="http://groups.google.com/group/comp.lang.python/browse_thread/thread/c4d3aae0df917df5/6fdae3872f9de79d?lnk=st&amp;amp;q=comp.lang.python+zeddy&amp;amp;rnum=6#6fdae3872f9de79d"&gt;a couple newsgroup posts&lt;/a&gt; by him and William Lewis in February of 1997, where they arrive at the same algorithm.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="InterleaveTableObvious"&gt;&lt;/a&gt;Interleave bits the obvious way&lt;/strong&gt;&lt;/p&gt;unsigned short x;   // Interleave bits of x and y, so that all of the&lt;br /&gt;unsigned short y;   // bits of x are in the even positions and y in the odd;&lt;br /&gt;unsigned int z = 0; // z gets the resulting Morton Number.&lt;br /&gt;&lt;br /&gt;for (int i = 0; i &amp;lt; sizeof(x) * CHAR_BIT; i++) // unroll for more speed...&lt;br /&gt;{&lt;br /&gt;  z |= (x &amp;amp; 1U &amp;lt;&amp;lt; i) &amp;lt;&amp;lt; i | (y &amp;amp; 1U &amp;lt;&amp;lt; i) &amp;lt;&amp;lt; (i + 1);&lt;br /&gt;}&lt;p&gt;Interleaved bits (aka Morton numbers) are useful for linearizing 2D integer coordinates, so x and y are combined into a single number that can be compared easily and has the property that a number is usually close to another if their x and y values are close.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="InterleaveTableLookup"&gt;&lt;/a&gt;Interleave bits by table lookup&lt;/strong&gt;&lt;/p&gt;static const unsigned short MortonTable256[256] = &lt;br /&gt;{&lt;br /&gt;  0x0000, 0x0001, 0x0004, 0x0005, 0x0010, 0x0011, 0x0014, 0x0015, &lt;br /&gt;  0x0040, 0x0041, 0x0044, 0x0045, 0x0050, 0x0051, 0x0054, 0x0055, &lt;br /&gt;  0x0100, 0x0101, 0x0104, 0x0105, 0x0110, 0x0111, 0x0114, 0x0115, &lt;br /&gt;  0x0140, 0x0141, 0x0144, 0x0145, 0x0150, 0x0151, 0x0154, 0x0155, &lt;br /&gt;  0x0400, 0x0401, 0x0404, 0x0405, 0x0410, 0x0411, 0x0414, 0x0415, &lt;br /&gt;  0x0440, 0x0441, 0x0444, 0x0445, 0x0450, 0x0451, 0x0454, 0x0455, &lt;br /&gt;  0x0500, 0x0501, 0x0504, 0x0505, 0x0510, 0x0511, 0x0514, 0x0515, &lt;br /&gt;  0x0540, 0x0541, 0x0544, 0x0545, 0x0550, 0x0551, 0x0554, 0x0555, &lt;br /&gt;  0x1000, 0x1001, 0x1004, 0x1005, 0x1010, 0x1011, 0x1014, 0x1015, &lt;br /&gt;  0x1040, 0x1041, 0x1044, 0x1045, 0x1050, 0x1051, 0x1054, 0x1055, &lt;br /&gt;  0x1100, 0x1101, 0x1104, 0x1105, 0x1110, 0x1111, 0x1114, 0x1115, &lt;br /&gt;  0x1140, 0x1141, 0x1144, 0x1145, 0x1150, 0x1151, 0x1154, 0x1155, &lt;br /&gt;  0x1400, 0x1401, 0x1404, 0x1405, 0x1410, 0x1411, 0x1414, 0x1415, &lt;br /&gt;  0x1440, 0x1441, 0x1444, 0x1445, 0x1450, 0x1451, 0x1454, 0x1455, &lt;br /&gt;  0x1500, 0x1501, 0x1504, 0x1505, 0x1510, 0x1511, 0x1514, 0x1515, &lt;br /&gt;  0x1540, 0x1541, 0x1544, 0x1545, 0x1550, 0x1551, 0x1554, 0x1555, &lt;br /&gt;  0x4000, 0x4001, 0x4004, 0x4005, 0x4010, 0x4011, 0x4014, 0x4015, &lt;br /&gt;  0x4040, 0x4041, 0x4044, 0x4045, 0x4050, 0x4051, 0x4054, 0x4055, &lt;br /&gt;  0x4100, 0x4101, 0x4104, 0x4105, 0x4110, 0x4111, 0x4114, 0x4115, &lt;br /&gt;  0x4140, 0x4141, 0x4144, 0x4145, 0x4150, 0x4151, 0x4154, 0x4155, &lt;br /&gt;  0x4400, 0x4401, 0x4404, 0x4405, 0x4410, 0x4411, 0x4414, 0x4415, &lt;br /&gt;  0x4440, 0x4441, 0x4444, 0x4445, 0x4450, 0x4451, 0x4454, 0x4455, &lt;br /&gt;  0x4500, 0x4501, 0x4504, 0x4505, 0x4510, 0x4511, 0x4514, 0x4515, &lt;br /&gt;  0x4540, 0x4541, 0x4544, 0x4545, 0x4550, 0x4551, 0x4554, 0x4555, &lt;br /&gt;  0x5000, 0x5001, 0x5004, 0x5005, 0x5010, 0x5011, 0x5014, 0x5015, &lt;br /&gt;  0x5040, 0x5041, 0x5044, 0x5045, 0x5050, 0x5051, 0x5054, 0x5055, &lt;br /&gt;  0x5100, 0x5101, 0x5104, 0x5105, 0x5110, 0x5111, 0x5114, 0x5115, &lt;br /&gt;  0x5140, 0x5141, 0x5144, 0x5145, 0x5150, 0x5151, 0x5154, 0x5155, &lt;br /&gt;  0x5400, 0x5401, 0x5404, 0x5405, 0x5410, 0x5411, 0x5414, 0x5415, &lt;br /&gt;  0x5440, 0x5441, 0x5444, 0x5445, 0x5450, 0x5451, 0x5454, 0x5455, &lt;br /&gt;  0x5500, 0x5501, 0x5504, 0x5505, 0x5510, 0x5511, 0x5514, 0x5515, &lt;br /&gt;  0x5540, 0x5541, 0x5544, 0x5545, 0x5550, 0x5551, 0x5554, 0x5555&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;unsigned short x; // Interleave bits of x and y, so that all of the&lt;br /&gt;unsigned short y; // bits of x are in the even positions and y in the odd;&lt;br /&gt;unsigned int z;   // z gets the resulting 32-bit Morton Number.&lt;br /&gt;&lt;br /&gt;z = MortonTable256[y &amp;gt;&amp;gt; 8]   &amp;lt;&amp;lt; 17 | &lt;br /&gt;    MortonTable256[x &amp;gt;&amp;gt; 8]   &amp;lt;&amp;lt; 16 |&lt;br /&gt;    MortonTable256[y &amp;amp; 0xFF] &amp;lt;&amp;lt;  1 | &lt;br /&gt;    MortonTable256[x &amp;amp; 0xFF];&lt;br /&gt;&lt;br /&gt;&lt;p&gt;For more speed, use an additional table with values that are MortonTable256 pre-shifted one bit to the left. This second table could then be used for the y lookups, thus reducing the operations by two, but almost doubling the memory required. Extending this same idea, four tables could be used, with two of them pre-shifted by 16 to the left of the previous two, so that we would only need 11 operations total.&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="Interleave64bitOps"&gt;&lt;/a&gt;Interleave bits with 64-bit multiply&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;In 11 operations, this version interleaves bits of two bytes (rather than shorts, as in the other versions), but many of the operations are 64-bit multiplies so it isn't appropriate for all machines. The input parameters, x and y, should be less than 256.&lt;/p&gt;unsigned char x;  // Interleave bits of (8-bit) x and y, so that all of the&lt;br /&gt;unsigned char y;  // bits of x are in the even positions and y in the odd;&lt;br /&gt;unsigned short z; // z gets the resulting 16-bit Morton Number.&lt;br /&gt;&lt;br /&gt;z = ((x * 0x0101010101010101ULL &amp;amp; 0x8040201008040201ULL) * &lt;br /&gt;     0x0102040810204081ULL &amp;gt;&amp;gt; 49) &amp;amp; 0x5555 |&lt;br /&gt;    ((y * 0x0101010101010101ULL &amp;amp; 0x8040201008040201ULL) * &lt;br /&gt;     0x0102040810204081ULL &amp;gt;&amp;gt; 48) &amp;amp; 0xAAAA;&lt;p&gt;Holger Bettag was inspired to suggest this technique on October 10, 2004 after reading the multiply-based bit reversals here.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="InterleaveBMN"&gt;&lt;/a&gt;Interleave bits by Binary Magic Numbers&lt;/strong&gt;&lt;/p&gt;static const unsigned int B[] = {0x55555555, 0x33333333, 0x0F0F0F0F, 0x00FF00FF};&lt;br /&gt;static const unsigned int S[] = {1, 2, 4, 8};&lt;br /&gt;&lt;br /&gt;unsigned int x; // Interleave lower 16 bits of x and y, so the bits of x&lt;br /&gt;unsigned int y; // are in the even positions and bits from y in the odd;&lt;br /&gt;unsigned int z; // z gets the resulting 32-bit Morton Number.  &lt;br /&gt;                // x and y must initially be less than 65536.&lt;br /&gt;&lt;br /&gt;x = (x | (x &amp;lt;&amp;lt; S[3])) &amp;amp; B[3];&lt;br /&gt;x = (x | (x &amp;lt;&amp;lt; S[2])) &amp;amp; B[2];&lt;br /&gt;x = (x | (x &amp;lt;&amp;lt; S[1])) &amp;amp; B[1];&lt;br /&gt;x = (x | (x &amp;lt;&amp;lt; S[0])) &amp;amp; B[0];&lt;br /&gt;&lt;br /&gt;y = (y | (y &amp;lt;&amp;lt; S[3])) &amp;amp; B[3];&lt;br /&gt;y = (y | (y &amp;lt;&amp;lt; S[2])) &amp;amp; B[2];&lt;br /&gt;y = (y | (y &amp;lt;&amp;lt; S[1])) &amp;amp; B[1];&lt;br /&gt;y = (y | (y &amp;lt;&amp;lt; S[0])) &amp;amp; B[0];&lt;br /&gt;&lt;br /&gt;z = x | (y &amp;lt;&amp;lt; 1);&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="ZeroInWord"&gt;&lt;/a&gt;Determine if a word has a zero byte&lt;/strong&gt;&lt;/p&gt;// Fewer operations:&lt;br /&gt;unsigned int v; // 32-bit word to check if any 8-bit byte in it is 0&lt;br /&gt;bool hasZeroByte = ~((((v &amp;amp; 0x7F7F7F7F) + 0x7F7F7F7F) | v) | 0x7F7F7F7F);&lt;p&gt;The code above may be useful when doing a fast string copy in which a word is copied at a time; it uses 5 operations. On the other hand, testing for a null byte in the obvious ways (which follow) have at least 7 operations (when counted in the most sparing way), and at most 12.&lt;/p&gt;// More operations:&lt;br /&gt;bool hasNoZeroByte = ((v &amp;amp; 0xff) &amp;amp;&amp;amp; (v &amp;amp; 0xff00) &amp;amp;&amp;amp; (v &amp;amp; 0xff0000) &amp;amp;&amp;amp; (v &amp;amp; 0xff000000))&lt;br /&gt;// OR:&lt;br /&gt;unsigned char * p = (unsigned char *) &amp;amp;v;  &lt;br /&gt;bool hasNoZeroByte = *p &amp;amp;&amp;amp; *(p + 1) &amp;amp;&amp;amp; *(p + 2) &amp;amp;&amp;amp; *(p + 3);&lt;p&gt;The code at the beginning of this section (labeled "Fewer operations") works by first zeroing the high bits of the 4 bytes in the word. Subsequently, it adds a number that will result in an overflow to the high bit of a byte if any of the low bits were initialy set. Next the high bits of the original word are ORed with these values; thus, the high bit of a byte is set iff any bit in the byte was set. Finally, we determine if any of these high bits are zero by ORing with ones everywhere except the high bits and inverting the result. Extending to 64 bits is trivial; simply increase the constants to be 0x7F7F7F7F7F7F7F7F.&lt;/p&gt;&lt;p&gt;For an additional improvement, a fast pretest that requires only 4 operations may be performed to determine if the word &lt;em&gt;may&lt;/em&gt; have a zero byte. The test also returns true if the high byte is 0x80, so there are occasional false positives, but the slower and more reliable version above may then be used on candidates for an overall increase in speed with correct output.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;bool hasZeroByte = ((v + 0x7efefeff) ^ ~v) &amp;amp; 0x81010100;&lt;br /&gt;if (hasZeroByte) // or may just have 0x80 in the high byte&lt;br /&gt;{&lt;br /&gt;  hasZeroByte = ~((((v &amp;amp; 0x7F7F7F7F) + 0x7F7F7F7F) | v) | 0x7F7F7F7F);&lt;br /&gt;}&lt;p&gt;There is yet a faster method &amp;mdash; use &lt;a href="http://graphics.stanford.edu/%7Eseander/bithacks.html#HasLessInWord"&gt;&lt;code&gt;hasless&lt;/code&gt;&lt;/a&gt;(v, 1), which is defined below; it works in 4 operations and requires no subsquent verification. It simplifies to&lt;/p&gt;#define haszero(v) (((v) - 0x01010101UL) &amp;amp; ~(v) &amp;amp; 0x80808080UL)&lt;p&gt;The subexpression (v - 0x01010101UL), evaluates to a high bit set in any byte whenever the corresponding byte in v is zero or greater than 0x80. The sub-expression ~v &amp;amp; 0x80808080UL evaluates to high bits set in bytes where the byte of v doesn't have its high bit set (so the byte was less than 0x80). Finally, by ANDing these two sub-expressions the result is the high bits set where the bytes in v were zero, since the high bits set due to a value greater than 0x80 in the first sub-expression are masked off by the second.&lt;/p&gt;&lt;p&gt;Paul Messmer suggested the fast pretest improvement on October 2, 2004. Juha J&amp;auml;rvi later suggested &lt;code&gt;hasless(v, 1)&lt;/code&gt; on April 6, 2005, which he found on &lt;a href="http://www.azillionmonkeys.com/qed/asmexample.html"&gt;Paul Hsieh's Assembly Lab&lt;/a&gt;; previously it was written in a newsgroup post on April 27, 1987 by Alan Mycroft.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="#ValueInWord"&gt;&lt;/a&gt;Determine if a word has a byte equal to n&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;We may want to know if any byte in a word has a specific value. To do so, we can XOR the value to test with a word that has been filled with the byte values in which we're interested. Because XORing a value with itself results in a zero byte and nonzero otherwise, we can pass the result to &lt;code&gt;haszero&lt;/code&gt;.&lt;/p&gt;#define hasvalue(x,n) \&lt;br /&gt;(haszero((x) ^ (~0UL/255 * (n))))&lt;p&gt;Stephen M Bennet suggested this on December 13, 2009 after reading the entry for &lt;code&gt;haszero&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="HasLessInWord"&gt;&lt;/a&gt;Determine if a word has a byte less than n&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Test if a word x contains an unsigned byte with value &amp;lt; n. Specifically for n=1, it can be used to find a 0-byte by examining one long at a time, or any byte by XORing x with a mask first. Uses 4 arithmetic/logical operations when n is constant.&lt;/p&gt;&lt;p&gt;Requirements: x&amp;gt;=0; 0&amp;lt;=n&amp;lt;=128&lt;/p&gt;#define hasless(x,n) (((x)-~0UL/255*(n))&amp;amp;~(x)&amp;amp;~0UL/255*128)&lt;p&gt;To count the number of bytes in x that are less than n in 7 operations, use&lt;/p&gt;#define countless(x,n) \&lt;br /&gt;(((~0UL/255*(127+(n))-((x)&amp;amp;~0UL/255*127))&amp;amp;~(x)&amp;amp;~0UL/255*128)/128%255)&lt;p&gt;Juha J&amp;auml;rvi sent this clever technique to me on April 6, 2005. The &lt;code&gt;countless&lt;/code&gt; macro was added by Sean Anderson on April 10, 2005, inspired by Juha's &lt;code&gt;countmore&lt;/code&gt;, below.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="HasMoreInWord"&gt;&lt;/a&gt;Determine if a word has a byte greater than n&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Test if a word x contains an unsigned byte with value &amp;gt; n. Uses 3 arithmetic/logical operations when n is constant.&lt;/p&gt;&lt;p&gt;Requirements: x&amp;gt;=0; 0&amp;lt;=n&amp;lt;=127&lt;/p&gt;#define hasmore(x,n) (((x)+~0UL/255*(127-(n))|(x))&amp;amp;~0UL/255*128)&lt;p&gt;To count the number of bytes in x that are more than n in 6 operations, use:&lt;/p&gt;#define countmore(x,n) \&lt;br /&gt;(((((x)&amp;amp;~0UL/255*127)+~0UL/255*(127-(n))|(x))&amp;amp;~0UL/255*128)/128%255)&lt;p&gt;The macro &lt;code&gt;hasmore&lt;/code&gt; was suggested by Juha J&amp;auml;rvi on April 6, 2005, and he added &lt;code&gt;countmore&lt;/code&gt; on April 8, 2005.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="HasBetweenInWord"&gt;&lt;/a&gt;Determine if a word has a byte between m and n&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;When m &amp;lt; n, this technique tests if a word x contains an unsigned byte value, such that m &amp;lt; value &amp;lt; n. &lt;!--When m&amp;nbsp;&amp;gt;&amp;nbsp;n, it tests for byte valuesoutside the range; that is value &amp;lt; n and m &amp;lt;= value.--&gt;It uses 7 arithmetic/logical operations when n and m are constant.&lt;/p&gt;&lt;p&gt;Note: Bytes that equal n can be reported by &lt;code&gt;likelyhasbetween&lt;/code&gt; as false positives, so this should be checked by character if a certain result is needed.&lt;/p&gt;&lt;p&gt;Requirements: x&amp;gt;=0; 0&amp;lt;=m&amp;lt;=127; 0&amp;lt;=n&amp;lt;=128&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;#define likelyhasbetween(x,m,n) \&lt;br /&gt;((((x)-~0UL/255*(n))&amp;amp;~(x)&amp;amp;((x)&amp;amp;~0UL/255*127)+~0UL/255*(127-(m)))&amp;amp;~0UL/255*128)&lt;p&gt;This technique would be suitable for a fast pretest. A variation that takes one more operation (8 total for constant m and n) but provides the exact answer is:&lt;/p&gt;#define hasbetween(x,m,n) \&lt;br /&gt;((~0UL/255*(127+(n))-((x)&amp;amp;~0UL/255*127)&amp;amp;~(x)&amp;amp;((x)&amp;amp;~0UL/255*127)+~0UL/255*(127-(m)))&amp;amp;~0UL/255*128)&lt;p&gt;To count the number of bytes in x that are between m and n (exclusive) in 10 operations, use:&lt;/p&gt;#define countbetween(x,m,n) (hasbetween(x,m,n)/128%255)&lt;p&gt;Juha J&amp;auml;rvi suggested &lt;code&gt;likelyhasbetween&lt;/code&gt; on April 6, 2005. From there, Sean Anderson created &lt;code&gt;hasbetween&lt;/code&gt; and &lt;code&gt;countbetween&lt;/code&gt; on April 10, 2005.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;hr /&gt;&lt;p&gt;&lt;strong&gt;&lt;a name="NextBitPermutation"&gt;&lt;/a&gt;Compute the lexicographically next bit permutation&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Suppose we have a pattern of N bits set to 1 in an integer and we want the next permutation of N 1 bits in a lexicographical sense. For example, if N is 3 and the bit pattern is 00010011, the next patterns would be 00010101, 00010110, 00011001,00011010, 00011100, 00100011, and so forth. The following is a fast way to compute the next permutation.&lt;/p&gt;unsigned int v; // current permutation of bits &lt;br /&gt;unsigned int w; // next permutation of bits&lt;br /&gt;&lt;br /&gt;unsigned int t = v | (v - 1); // t gets v's least significant 0 bits set to 1&lt;br /&gt;// Next set to 1 the most significant bit to change, &lt;br /&gt;// set to 0 the least significant ones, and add the necessary 1 bits.&lt;br /&gt;w = (t + 1) | (((~t &amp;amp; -~t) - 1) &amp;gt;&amp;gt; (__builtin_ctz(v) + 1));  &lt;p&gt;The __builtin_ctz(v) GNU C compiler intrinsic for x86 CPUs returns the number of trailing zeros. If you are using Microsoft compilers for x86, the intrinsic is _BitScanForward. These both emit a bsf instruction, but equivalents may be available for other architectures. If not, then consider using one of the methods for counting the consecutive zero bits mentioned earlier.&lt;/p&gt;&lt;p&gt;Here is another version that tends to be slower because of its division operator, but it does not require counting the trailing zeros.&lt;/p&gt;unsigned int t = (v | (v - 1)) + 1;  &lt;br /&gt;w = t | ((((t &amp;amp; -t) / (v &amp;amp; -v)) &amp;gt;&amp;gt; 1) - 1);  &lt;p&gt;Thanks to Dario Sneidermanis of Argentina, who provided this on November 28, 2009.&lt;/p&gt;&lt;p&gt;&lt;a href="http://webhostingrating.com/libs/bithacks-be"&gt;A Belorussian translation&lt;/a&gt; (provided by &lt;a href="http://webhostingrating.com/"&gt;Webhostingrating&lt;/a&gt;) is available.&lt;!--&lt;hr&gt;&lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;b&gt;Hits:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;&lt;img src="http://www.anderoid.com/cgi-bin/countess.gif?seander2" width=56 height=16&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;--&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/oomusou/aggbug/2376353.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/oomusou/archive/2012/03/01/bit_hacks.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/oomusou/archive/2012/02/04/virtual_box_remove_driver.html</id><title type="text">(筆記) 如何移除VirtualBox所遺留下的驅動程式? (SOC) (VirtualBox)</title><summary type="text">有時儘管依照正常程序移除了VirtualBox，但是在裝置管理員還是會看到遺留下來無法移除的驅動程式，本文討論如何手動移除這些無法移除的驅動程式。</summary><published>2012-02-04T09:55:00Z</published><updated>2012-02-04T09:55:00Z</updated><author><name>真 OO无双</name><uri>http://www.cnblogs.com/oomusou/</uri></author><link rel="alternate" href="http://www.cnblogs.com/oomusou/archive/2012/02/04/virtual_box_remove_driver.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/oomusou/archive/2012/02/04/virtual_box_remove_driver.html"/><content type="html">&lt;p&gt;&lt;strong&gt;Abstract&lt;br /&gt;&lt;/strong&gt;有時儘管依照正常程序移除了VirtualBox，但是在裝置管理員還是會看到遺留下來無法移除的驅動程式，本文討論如何手動移除這些無法移除的驅動程式。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Introduction&lt;br /&gt;使用環境：Windows XP XP3 + VirtualBox 4.1.2&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Symptom&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;如下圖是當VirtualBox正常移除後，卻在裝置管理員所遺留下來的驅動程式：&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border: 0px;" title="vb_remove01" src="http://images.cnblogs.com/cnblogs_com/oomusou/201202/201202041754524814.gif" alt="vb_remove01" width="807" height="620" border="0" /&gt;&lt;/p&gt;&lt;p&gt;若在裝置管理員手動移除這些驅動程式，會出現以下錯誤訊息而無法移除。&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border: 0px;" title="vb_remove02" src="http://images.cnblogs.com/cnblogs_com/oomusou/201202/2012020417545619.gif" alt="vb_remove02" width="578" height="191" border="0" /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Root Cause &amp;amp; Solution&lt;br /&gt;Step 1：裝置例項識別碼&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;選擇所要移除的驅動程式，滑鼠右鍵 &amp;ndash;&amp;gt; 內容 &amp;ndash;&amp;gt; 詳細資料&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border: 0px;" title="vb_remove03" src="http://images.cnblogs.com/cnblogs_com/oomusou/201202/20120204175457675.gif" alt="vb_remove03" width="404" height="426" border="0" /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Step 2：手動在登錄編輯程式刪除註冊機碼&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;開啟登入編輯程式，在以下位置HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum找到如上的註冊機碼&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border: 0px;" title="vb_remove04" src="http://images.cnblogs.com/cnblogs_com/oomusou/201202/201202041754594428.gif" alt="vb_remove04" width="807" height="620" border="0" /&gt;&lt;/p&gt;&lt;p&gt;此時若你直接刪除此註冊機碼，會出現無法刪除的錯誤訊息，主要是因為我們沒有權限去刪除之，這也是我們無法從裝置管理員去刪除此驅動程式的root cause。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Step 3：改變註冊機碼的權限&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;選擇要刪除的註冊機碼，滑鼠右鍵 &amp;ndash;&amp;gt; 使用權限&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border: 0px;" title="vb_remove05" src="http://images.cnblogs.com/cnblogs_com/oomusou/201202/201202041755003447.gif" alt="vb_remove05" width="367" height="422" border="0" /&gt;&lt;/p&gt;&lt;p&gt;將Everyone加入的『完全控制』變成『允許』。&lt;/p&gt;&lt;p&gt;或許你會擔心這樣的權限適當嗎? 反正這個註冊機碼馬上就要被刪除，所以讓Everyone完全控制也沒有關係。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Step 4：刪除註冊機碼&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border: 0px;" title="vb_remove06" src="http://images.cnblogs.com/cnblogs_com/oomusou/201202/201202041755015498.gif" alt="vb_remove06" width="507" height="191" border="0" /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Step 5：到裝置管理員確認&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;原本無法移除的驅動程式已經正常移除了&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border: 0px;" title="vb_remove067" src="http://images.cnblogs.com/cnblogs_com/oomusou/201202/201202041755029218.gif" alt="vb_remove067" width="807" height="620" border="0" /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br /&gt;這是參考Microsoft的官方解法&lt;a href="http://blogs.technet.com/b/csstwplatform/archive/2010/05/12/windows-driver-uninstallation-failed-in-windows-xp.aspx" target="_blank"&gt;Windows Driver uninstallation Failed in Windows XP&lt;/a&gt;，不只適用在VirtualBox，其他驅動程式若無法解除，也可以使用這種方式。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Reference&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://blogs.technet.com/b/csstwplatform/archive/2010/05/12/windows-driver-uninstallation-failed-in-windows-xp.aspx" target="_blank"&gt;Windows Driver uninstallation Failed in Windows XP&lt;/a&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/oomusou/aggbug/2338151.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/oomusou/archive/2012/02/04/virtual_box_remove_driver.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/oomusou/archive/2012/01/29/verilog_else.html</id><title type="text">(筆記) always block內省略else所代表的電路 (SOC) (Verilog)</title><summary type="text">在Verilog中，always block可以用來代表Flip-Flop, Combination Logic與Latch，本文比較在不寫else下，always block所代表的電路。</summary><published>2012-01-29T10:45:00Z</published><updated>2012-01-29T10:45:00Z</updated><author><name>真 OO无双</name><uri>http://www.cnblogs.com/oomusou/</uri></author><link rel="alternate" href="http://www.cnblogs.com/oomusou/archive/2012/01/29/verilog_else.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/oomusou/archive/2012/01/29/verilog_else.html"/><content type="html">&lt;p&gt;&lt;strong&gt;Abstract&lt;/strong&gt;&lt;br /&gt;在Verilog中，always block可以用來代表Flip-Flop, Combination Logic與Latch，本文比較在不寫else下，always block所代表的電路。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br /&gt;在C語言裡，省略else只是代表不處理而；已但在Verilog裡，省略else所代表的是不同的電路。&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;always&lt;/span&gt;@(a &lt;span style="color: #0000ff;"&gt;or&lt;/span&gt; b &lt;span style="color: #0000ff;"&gt;or&lt;/span&gt; en)&lt;br /&gt;  &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (en)&lt;br /&gt;    c = a &amp;amp; b;&lt;/div&gt;&lt;p&gt;在combination logic中省略else，由於必須在~en保留原本的值，所以會產生latch。&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="or00" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201292027184722.gif" alt="or00" width="288" height="138" border="0" /&gt;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;always&lt;/span&gt;@(posedge clk)&lt;br /&gt;  &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (en)&lt;br /&gt;    c &amp;lt;= a &amp;amp; b;&lt;/div&gt;&lt;p&gt;雖然也必須在~en保留原本的值，但由於flip-flop就有記憶的功能，所以不會產生latch。if將產生mux，並將flip-flop的值拉回給mux。&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="or01" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201292027214006.gif" alt="or01" width="360" height="186" border="0" /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br /&gt;在Verilog中，雖然只是小小的差異，但結果卻有天大的差異。&lt;/p&gt;&lt;p&gt;全文完。&lt;/p&gt;&lt;img src="http://www.cnblogs.com/oomusou/aggbug/2331388.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/oomusou/archive/2012/01/29/verilog_else.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/oomusou/archive/2012/01/18/qsys_tristate_bridge.html</id><title type="text">(原創) 如何使用Tri-state Conduit Bridge連接Flash與SSRAM? (SOC) (Nios II) (Qsys) (NEEK)</title><summary type="text">在實務上為了節省FPGA的pin腳，我們常會將Flash與SSRAM的address與data共用，在Qsys中必須使用Tri-state Conduit Bridge, Tri-state Conduit Pin Sharer與Generic Tristate Controller與Flash與SSRAM連在一起，和SOPC Builder只使用Tri-state Bridge不同。</summary><published>2012-01-17T23:23:00Z</published><updated>2012-01-17T23:23:00Z</updated><author><name>真 OO无双</name><uri>http://www.cnblogs.com/oomusou/</uri></author><link rel="alternate" href="http://www.cnblogs.com/oomusou/archive/2012/01/18/qsys_tristate_bridge.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/oomusou/archive/2012/01/18/qsys_tristate_bridge.html"/><content type="html">&lt;p&gt;&lt;strong&gt;Abstract&lt;/strong&gt;&lt;br&gt;在實務上為了節省FPGA的pin腳，我們常會將Flash與SSRAM的address與data共用，在Qsys中必須使用Tri-state Conduit Bridge, Tri-state Conduit Pin Sharer與Generic Tristate Controller與Flash與SSRAM連在一起，和SOPC Builder只使用Tri-state Bridge不同。&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Introduction&lt;br&gt;使用環境：Windows XP SP3 + VirtualBox 4.1.2 + Quartus II 11.0 + NEEK&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;本文將討論以下主題：&lt;/p&gt; &lt;p&gt;1.Flash與SSRAM共用address與data的設計&lt;/p&gt; &lt;p&gt;2.如何在Qsys使用Tri-state Conduit Bridge與Tri-state Conduit Pin Sharer？&lt;/p&gt; &lt;p&gt;3.Address與Data floating問題與解決方法&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Reference&lt;/strong&gt;&lt;br&gt;[1] &lt;a href="http://www.altera.com/products/devkits/altera/kit-cyc3-embedded.html" target="_blank"&gt;Nios II Embedded Evaluation Kit (NEEK)&lt;/a&gt;&lt;br&gt;[2] &lt;a href="http://www.altera.com/literature/tt/tt_nios2_system_architect.pdf" target="_blank"&gt;Nios II System Architect Design Tutorial&lt;/a&gt;&lt;br&gt;[3] &lt;a href="http://www.cnblogs.com/oomusou/admin/www.altera.com/literature/hb/qts/qsys_interconnect.pdf" target="_blank"&gt;Qsys Interconnect&lt;/a&gt;&lt;br&gt;[4] &lt;a href="http://www.altera.com/literature/ug/ug_avalon_tc.pdf" target="_blank"&gt;Avalon Tri-State Conduit Components User Guide&lt;/a&gt;&lt;br&gt;[5] &lt;a href="http://www.altera.com/literature/an/an632.pdf" target="_blank"&gt;SOPC Builder to Qsys Migration Guidelines&lt;/a&gt;&lt;/p&gt; &lt;p&gt;未完，待續。&lt;/p&gt;&lt;img src="http://www.cnblogs.com/oomusou/aggbug/2325157.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/oomusou/archive/2012/01/18/qsys_tristate_bridge.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/oomusou/archive/2012/01/04/subsystem_interrupt.html</id><title type="text">(原創) 如何在Qsys Subsystem使用Interrupt? (SOC) (Nios II) (Qsys)</title><summary type="text">Qsys其中之一的賣點就是允許你建立hierarchical的subsystem。在SOC設計中，通常我們會將慢速的周邊使用較慢的clock而自成一個系統，在Qsys中允許我們將這些較慢的周邊包成一個peripheral subsystem，而不是像SOPC Builder一樣只能有一個system。當我們使用subsystem的架構時，該如何讓subsystem內IP使用interrupt的方式與Nios II溝通呢?</summary><published>2012-01-04T15:12:00Z</published><updated>2012-01-04T15:12:00Z</updated><author><name>真 OO无双</name><uri>http://www.cnblogs.com/oomusou/</uri></author><link rel="alternate" href="http://www.cnblogs.com/oomusou/archive/2012/01/04/subsystem_interrupt.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/oomusou/archive/2012/01/04/subsystem_interrupt.html"/><content type="html">&lt;p&gt;&lt;strong&gt;Abstract&lt;/strong&gt;&lt;br /&gt;Qsys其中之一的賣點就是允許你建立hierarchical的subsystem。在SOC設計中，通常我們會將慢速的周邊使用較慢的clock而自成一個系統，在Qsys中允許我們將這些較慢的周邊包成一個peripheral subsystem，而不是像SOPC Builder一樣只能有一個system。當我們使用subsystem的架構時，該如何讓subsystem內IP使用interrupt的方式與Nios II溝通呢?&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Introduction&lt;br /&gt;使用環境：Windows XP SP3 + VirtualBox 4.1.2 + Quartus II 11.0&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;本文將討論以下主題：&lt;/p&gt;&lt;p&gt;1.如何使用IRQ Bridge連接subsystem的IRQ export到Nios II Processor?&lt;/p&gt;&lt;p&gt;2.如何避開Nios II SBT目前無法自動產生IRQ number常數在system.h的bug?&lt;/p&gt;&lt;p&gt;3.如何在App project使用Interrupt?&lt;/p&gt;&lt;p&gt;&lt;strong&gt;1.如何使用IRQ Bridge連接subsystem的IRQ export到Nios II Processor?&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;根據[1] &lt;a href="http://www.altera.com/literature/hb/qts/qsys_interconnect.pdf" target="_blank"&gt;Qsys Interconnect&lt;/a&gt; p.7-27 &lt;strong&gt;IRQ Bridge&lt;/strong&gt;章節的建議，若要在subsystem使用interrupt，必須在包含Nios II的CPU subsystem使用IRQ Bridge接收Peripheral subsystem所export出來的IRQ，然後才連到Nios II Processor。至於下圖中的Merlin IRQ Mapper則並不是必需的，若你在CPU subsystem中也有IP要使用interrupt，這必須再包一層Merlin IRQ Mapper；若你在CPU subsystem沒有IP要使用interrupt，只有Peripheral subsystem使用interrupt，可以直接只使用IRQ Bridge連接到Nios II Processor。&lt;/p&gt;&lt;p&gt;常見的錯誤(也是我第一次的錯誤)是直接將Peripheral subsystem所export的IRQ直接連到Nios II Processor，這樣Qsys與Quartus II編譯都沒有任何錯誤，Nios II也沒有錯誤，不過Nios II的C code將收不到來自Peripheral subsystem的interrupt。&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="irq000" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201042354236820.gif" alt="irq000" width="734" height="540" border="0" /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;2.如何避開Nios II SBT目前無法自動產生IRQ number常數在system.h的bug?&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;除此之外，在[2] &lt;a href="http://www.altera.com/literature/rn/archives/rn_qts_110.pdf" target="_blank"&gt;Quartus II 11.0 Release Notes&lt;/a&gt;的p.15已經承認在Quartus II 11.0有以下的bug。&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="irq001" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201042354293651.gif" alt="irq001" width="747" height="125" border="0" /&gt;&lt;/p&gt;&lt;p&gt;簡單的說，就是若你在subsystem中使用interrupt，當你使用Generate BSP時，Nios II SBT根據*.sopcinfo在system.h所產生相對應的IRQ number常數，永遠都是-1，並無法反應出目前硬體架構中真正的IRQ number。且經過實測，這個問題在Quartus II 11.1也依然存在。&lt;/p&gt;&lt;p&gt;如圖所示，我們對push button使用interrupt，但在system.h的PERIPHERAL_SYSTEM_BUTTON_PIO_IRQ常數卻是-1，與在Qsys上的IRQ number不同。&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="irq002" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201052216194777.gif" alt="irq002" width="807" height="620" border="0" /&gt;&lt;/p&gt;&lt;p&gt;這會造成什麼影響呢?只要你的App project使用到有使用interrupt的IP，編譯就會產生錯誤，如下圖我們在system timer有使用interrupt，所以一編譯就產生錯誤無法繼續。&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="irq003" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201052216339289.gif" alt="irq003" width="807" height="620" border="0" /&gt;&lt;/p&gt;&lt;p&gt;詳細錯誤訊息如下：&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;Error: Interrupt not connected for peripheral_system_timer_0. The system clock driver requires an interrupt to be connected. Please select an IRQ for this device in SOPC builder.&lt;/div&gt;&lt;p&gt;root cause就是system.h的IRQ number都是-1，所以App project找不到該IP的IRQ number而無法編譯。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Solution&lt;br /&gt;&lt;/strong&gt;既然在system.h的IRQ number都是-1，我們只好重新自己在system.h手動定義IRQ number。根據[3] &lt;a href="http://www.alterawiki.com/wiki/New_Qsys_Issues#Interrupts" target="_blank"&gt;Altera Wiki : New Qsys Issues for Interrupts&lt;/a&gt;的建議&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="irq014" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201052333158485.gif" alt="irq014" width="781" height="197" border="0" /&gt;&lt;/p&gt;&lt;p&gt;當然你可以直接去修改system.h，但別忘了BSP project的所有檔案最好不要手動修改，應該交由Nios II SBT或者command line script去修改才對，因為每當你執行Generate BSP或者BSP Editor時，Nios II SBT都會重新產生system.h，也就是你做的所有修改都會被覆蓋掉。所以[3] &lt;a href="http://www.alterawiki.com/wiki/New_Qsys_Issues#Interrupts" target="_blank"&gt;Altera Wiki : New Qsys Issues for Interrupts&lt;/a&gt; 的解法雖然可行，但並不完美。&lt;/p&gt;&lt;p&gt;根據 [2] &lt;a href="http://www.altera.com/literature/rn/archives/rn_qts_110.pdf" target="_blank"&gt;Quartus II 11.0 Release Notes&lt;/a&gt;的建議&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="irq001" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201042354293651.gif" alt="irq001" width="747" height="125" border="0" /&gt;&lt;/p&gt;&lt;p&gt;看起來是要我們自己另開一個my_system.h，然後將system.h所有內容複製到system.h，自己在my_system.h加上IRQ number常數，並且App project也使用my_system.h，這樣Generate BSP與BSP Editor就不會改到我的my_system.h了。&lt;/p&gt;&lt;p&gt;這樣看似理想，但卻衍生另外一個問題：BSP project有大量的檔案都include "system.h"，你必須手動一一的改成include "my_system.h"，這也是個大工程。所以[2] &lt;a href="http://www.altera.com/literature/rn/archives/rn_qts_110.pdf" target="_blank"&gt;Quartus II 11.0 Release Notes&lt;/a&gt; 看似也不盡理想。&lt;/p&gt;&lt;p&gt;我在此提供一個小技巧對付Nios II SBT目前這個bug。&lt;/p&gt;&lt;p&gt;在system.h的尾部自己加上IP IRQ number的定義如下圖，先執行#undef取消這個常數的定義，再用#define重新這個常數，因為若同一個常數在system.h重複#define，會產生warning，若使用#undef就完全沒有warning。&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="irq005" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201052244451593.gif" alt="irq005" width="807" height="620" border="0" /&gt;&lt;/p&gt;&lt;p&gt;然後將這些#undef與#define另存一個文字檔，每當你執行Generate BSP或者BSP Editor時，system.h就會被Nios II SBT重新產生蓋掉你原先的設定，&lt;strong&gt;&lt;span style="color: #008080;"&gt;再將這些#undef與#define貼到system.h的最下方。如此就只需修改system.h，其他BSP檔案都不用做任何修改。&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;如此修改重新編譯後，就可順利Build Project，沒有任何錯誤。&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="irq006" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/20120105224450607.gif" alt="irq006" width="807" height="620" border="0" /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;3.如何在App project使用Interrupt?&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;根據[4] &lt;a href="http://www.altera.com/literature/hb/nios2/n2sw_nii52006.pdf" target="_blank"&gt;Exception Handling&lt;/a&gt; p.8-16與p.8-17的建議，我們將App project寫成如下的code使用interrupt。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;nios2_onchip_leg_push_button.c / C&lt;/strong&gt;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #008080;"&gt; 1&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 2&lt;/span&gt; &lt;span style="color: #008000;"&gt;(C) OOMusou 2011 &lt;/span&gt;&lt;span style="color: #008000; text-decoration: underline;"&gt;http://oomusou.cnblogs.com&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 3&lt;/span&gt; &lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 4&lt;/span&gt; &lt;span style="color: #008000;"&gt;Filename    : nios2_onchip_led_push_button.c&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 5&lt;/span&gt; &lt;span style="color: #008000;"&gt;Compiler    : Nios II SBT 11.0&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 6&lt;/span&gt; &lt;span style="color: #008000;"&gt;Description : test led, push_button &amp;amp; interrupt in subsystem&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 7&lt;/span&gt; &lt;span style="color: #008000;"&gt;Release     : Jan.06,2012 1.0&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 8&lt;/span&gt; &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 9&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;10&lt;/span&gt; #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;11&lt;/span&gt; #include &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;system.h&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;12&lt;/span&gt; #include &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;alt_types.h&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;13&lt;/span&gt; #include &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;sys/alt_irq.h&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;14&lt;/span&gt; #include &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;altera_avalon_pio_regs.h&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;15&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;16&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;volatile&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; edge_capture;&lt;br /&gt;&lt;span style="color: #008080;"&gt;17&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;18&lt;/span&gt; #ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT&lt;br /&gt;&lt;span style="color: #008080;"&gt;19&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; handle_key_interrupts(&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; *context) {&lt;br /&gt;&lt;span style="color: #008080;"&gt;20&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#else&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;21&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; handle_key_interrupts(&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; *context, alt_u32 id) {&lt;br /&gt;&lt;span style="color: #008080;"&gt;22&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;23&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;24&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;volatile&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; *edge_capture_ptr = (&lt;span style="color: #0000ff;"&gt;volatile&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt;*)context;&lt;br /&gt;&lt;span style="color: #008080;"&gt;25&lt;/span&gt;   *edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(PERIPHERAL_SYSTEM_BUTTON_PIO_BASE);&lt;br /&gt;&lt;span style="color: #008080;"&gt;26&lt;/span&gt;   IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PERIPHERAL_SYSTEM_BUTTON_PIO_BASE, &lt;span style="color: #800080;"&gt;0&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;27&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;28&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (edge_capture == &lt;span style="color: #800080;"&gt;0x01&lt;/span&gt;) {&lt;br /&gt;&lt;span style="color: #008080;"&gt;29&lt;/span&gt;     printf(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;key 1 pressed!\n&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;30&lt;/span&gt;     IOWR(PERIPHERAL_SYSTEM_LED_PIO_BASE, &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;, &lt;span style="color: #800080;"&gt;0xFE&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;31&lt;/span&gt;     edge_capture = &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;32&lt;/span&gt;   }&lt;br /&gt;&lt;span style="color: #008080;"&gt;33&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (edge_capture == &lt;span style="color: #800080;"&gt;0x02&lt;/span&gt;) {&lt;br /&gt;&lt;span style="color: #008080;"&gt;34&lt;/span&gt;     printf(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;key 2 pressed\n&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;35&lt;/span&gt;     IOWR(PERIPHERAL_SYSTEM_LED_PIO_BASE, &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;, &lt;span style="color: #800080;"&gt;0xFD&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;36&lt;/span&gt;     edge_capture = &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;37&lt;/span&gt;   }&lt;br /&gt;&lt;span style="color: #008080;"&gt;38&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (edge_capture == &lt;span style="color: #800080;"&gt;0x04&lt;/span&gt;) {&lt;br /&gt;&lt;span style="color: #008080;"&gt;39&lt;/span&gt;     printf(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;key 3 pressed\n&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;40&lt;/span&gt;     IOWR(PERIPHERAL_SYSTEM_LED_PIO_BASE, &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;, &lt;span style="color: #800080;"&gt;0xFB&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;41&lt;/span&gt;     edge_capture = &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;42&lt;/span&gt;   }&lt;br /&gt;&lt;span style="color: #008080;"&gt;43&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (edge_capture == &lt;span style="color: #800080;"&gt;0x08&lt;/span&gt;) {&lt;br /&gt;&lt;span style="color: #008080;"&gt;44&lt;/span&gt;     printf(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;key 4 pressed\n&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;45&lt;/span&gt;     IOWR(PERIPHERAL_SYSTEM_LED_PIO_BASE, &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;, &lt;span style="color: #800080;"&gt;0xF7&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;46&lt;/span&gt;     edge_capture = &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;47&lt;/span&gt;   }&lt;br /&gt;&lt;span style="color: #008080;"&gt;48&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (edge_capture == &lt;span style="color: #800080;"&gt;0x10&lt;/span&gt;) {&lt;br /&gt;&lt;span style="color: #008080;"&gt;49&lt;/span&gt;     printf(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;key 5 pressed\n&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;50&lt;/span&gt;     IOWR(PERIPHERAL_SYSTEM_LED_PIO_BASE, &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;, &lt;span style="color: #800080;"&gt;0xEF&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;51&lt;/span&gt;     edge_capture = &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;52&lt;/span&gt;   }&lt;br /&gt;&lt;span style="color: #008080;"&gt;53&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (edge_capture == &lt;span style="color: #800080;"&gt;0x20&lt;/span&gt;) {&lt;br /&gt;&lt;span style="color: #008080;"&gt;54&lt;/span&gt;     printf(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;key 6 pressed\n&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;55&lt;/span&gt;     IOWR(PERIPHERAL_SYSTEM_LED_PIO_BASE, &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;, &lt;span style="color: #800080;"&gt;0xDF&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;56&lt;/span&gt;     edge_capture = &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;57&lt;/span&gt;   }&lt;br /&gt;&lt;span style="color: #008080;"&gt;58&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (edge_capture == &lt;span style="color: #800080;"&gt;0x40&lt;/span&gt;) {&lt;br /&gt;&lt;span style="color: #008080;"&gt;59&lt;/span&gt;     printf(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;key 7 pressed\n&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;60&lt;/span&gt;     IOWR(PERIPHERAL_SYSTEM_LED_PIO_BASE, &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;, &lt;span style="color: #800080;"&gt;0xBF&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;61&lt;/span&gt;     edge_capture = &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;62&lt;/span&gt;   }&lt;br /&gt;&lt;span style="color: #008080;"&gt;63&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;else&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (edge_capture == &lt;span style="color: #800080;"&gt;0x80&lt;/span&gt;) {&lt;br /&gt;&lt;span style="color: #008080;"&gt;64&lt;/span&gt;     printf(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;key 8 pressed\n&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;65&lt;/span&gt;     IOWR(PERIPHERAL_SYSTEM_LED_PIO_BASE, &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;, &lt;span style="color: #800080;"&gt;0x7F&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;66&lt;/span&gt;     edge_capture = &lt;span style="color: #800080;"&gt;0x00&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;67&lt;/span&gt;   }&lt;br /&gt;&lt;span style="color: #008080;"&gt;68&lt;/span&gt; }&lt;br /&gt;&lt;span style="color: #008080;"&gt;69&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;70&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;71&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; init_key() {&lt;br /&gt;&lt;span style="color: #008080;"&gt;72&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; *edge_capture_ptr = (&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; *)&amp;amp;edge_capture;&lt;br /&gt;&lt;span style="color: #008080;"&gt;73&lt;/span&gt;   IOWR_ALTERA_AVALON_PIO_IRQ_MASK(PERIPHERAL_SYSTEM_BUTTON_PIO_BASE, &lt;span style="color: #800080;"&gt;0xFF&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;74&lt;/span&gt;   IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PERIPHERAL_SYSTEM_BUTTON_PIO_BASE, &lt;span style="color: #800080;"&gt;0x0&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;75&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;76&lt;/span&gt; #ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT&lt;br /&gt;&lt;span style="color: #008080;"&gt;77&lt;/span&gt;   alt_ic_isr_register(PERIPHERAL_SYSTEM_BUTTON_PIO_IRQ_INTERRUPT_CONTROLLER_ID,&lt;br /&gt;&lt;span style="color: #008080;"&gt;78&lt;/span&gt;                       PERIPHERAL_SYSTEM_BUTTON_PIO_IRQ,&lt;br /&gt;&lt;span style="color: #008080;"&gt;79&lt;/span&gt;                       handle_key_interrupts,&lt;br /&gt;&lt;span style="color: #008080;"&gt;80&lt;/span&gt;                       edge_capture_ptr,&lt;br /&gt;&lt;span style="color: #008080;"&gt;81&lt;/span&gt;                       &lt;span style="color: #800080;"&gt;0x0&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;82&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#else&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;83&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;84&lt;/span&gt;   alt_irq_register(PERIPHERAL_SYSTEM_BUTTON_PIO_IRQ,&lt;br /&gt;&lt;span style="color: #008080;"&gt;85&lt;/span&gt;                    edge_capture_ptr,&lt;br /&gt;&lt;span style="color: #008080;"&gt;86&lt;/span&gt;                    handle_key_interrupts);&lt;br /&gt;&lt;span style="color: #008080;"&gt;87&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;88&lt;/span&gt; }&lt;br /&gt;&lt;span style="color: #008080;"&gt;89&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;90&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;91&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; main() {&lt;br /&gt;&lt;span style="color: #008080;"&gt;92&lt;/span&gt;   printf(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;Nios II + Onchip + LED + Push Button Test!\n&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;93&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;94&lt;/span&gt;   init_key();&lt;br /&gt;&lt;span style="color: #008080;"&gt;95&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;96&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;while&lt;/span&gt;(&lt;span style="color: #800080;"&gt;1&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;97&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;98&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; &lt;span style="color: #800080;"&gt;0&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;99&lt;/span&gt; }&lt;/div&gt;&lt;p&gt;以上的code我不打算一行一行講解，詳細請參考[4] &lt;a href="http://www.altera.com/literature/hb/nios2/n2sw_nii52006.pdf" target="_blank"&gt;Exception Handling&lt;/a&gt; p.8-16與p.8-17，我要強調的是，&lt;strong&gt;&lt;span style="color: #008080;"&gt;一般在使用interrupt時，Altera都會建議你使用新的alt_ic_isr_register()，而不要使用舊的alt_irq_register()，並且說未來Nios II SBT版本將只支援alt_ic_isr_register()&lt;/span&gt;&lt;/strong&gt;，若在只有一層Qsys的system下，可以使用alt_ic_isr_register()沒有問題，但在具有Qsys subsystem下使用interrupt時，若使用alt_ic_isr_register()將會造成編譯錯誤，會說找不到alt_ic_isr_register()，為什麼會這樣呢?&lt;/p&gt;&lt;p&gt;在一般只有一層Qsys system，在system.h可以看到&lt;strong&gt;&lt;span style="color: #008080;"&gt;#define ALT_ENHANCED_INTERRUPT_API_PRESENT&lt;/span&gt;&lt;/strong&gt;，如下圖所示：&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="irq007" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201052244578943.gif" alt="irq007" width="807" height="620" border="0" /&gt;&lt;/p&gt;&lt;p&gt;但在使用Qsys subsystem的system.h下，看不到&lt;strong&gt;&lt;span style="color: #008080;"&gt;#define ALT_ENHANCED_INTERRUPT_API_PRESENT&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="irq008" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201052320193404.gif" alt="irq008" width="807" height="620" border="0" /&gt;&lt;/p&gt;&lt;p&gt;在BSP的&lt;/p&gt;&lt;p&gt;&lt;strong&gt;sys/alt_irq.h / C&lt;/strong&gt;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #008080;"&gt;  1&lt;/span&gt; #ifndef __ALT_IRQ_H__&lt;br /&gt;&lt;span style="color: #008080;"&gt;  2&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#define&lt;/span&gt; __ALT_IRQ_H__&lt;br /&gt;&lt;span style="color: #008080;"&gt;  3&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;  4&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;*****************************************************************************&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;  5&lt;/span&gt; &lt;span style="color: #008000;"&gt;*                                                                             *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;  6&lt;/span&gt; &lt;span style="color: #008000;"&gt;* License Agreement                                                           *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;  7&lt;/span&gt; &lt;span style="color: #008000;"&gt;*                                                                             *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;  8&lt;/span&gt; &lt;span style="color: #008000;"&gt;* Copyright (c) 2009 Altera Corporation, San Jose, California, USA.           *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;  9&lt;/span&gt; &lt;span style="color: #008000;"&gt;* All rights reserved.                                                        *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 10&lt;/span&gt; &lt;span style="color: #008000;"&gt;*                                                                             *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 11&lt;/span&gt; &lt;span style="color: #008000;"&gt;* Permission is hereby granted, free of charge, to any person obtaining a     *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 12&lt;/span&gt; &lt;span style="color: #008000;"&gt;* copy of this software and associated documentation files (the "Software"),  *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 13&lt;/span&gt; &lt;span style="color: #008000;"&gt;* to deal in the Software without restriction, including without limitation   *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 14&lt;/span&gt; &lt;span style="color: #008000;"&gt;* the rights to use, copy, modify, merge, publish, distribute, sublicense,    *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 15&lt;/span&gt; &lt;span style="color: #008000;"&gt;* and/or sell copies of the Software, and to permit persons to whom the       *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 16&lt;/span&gt; &lt;span style="color: #008000;"&gt;* Software is furnished to do so, subject to the following conditions:        *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 17&lt;/span&gt; &lt;span style="color: #008000;"&gt;*                                                                             *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 18&lt;/span&gt; &lt;span style="color: #008000;"&gt;* The above copyright notice and this permission notice shall be included in  *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 19&lt;/span&gt; &lt;span style="color: #008000;"&gt;* all copies or substantial portions of the Software.                         *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 20&lt;/span&gt; &lt;span style="color: #008000;"&gt;*                                                                             *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 21&lt;/span&gt; &lt;span style="color: #008000;"&gt;* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 22&lt;/span&gt; &lt;span style="color: #008000;"&gt;* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 23&lt;/span&gt; &lt;span style="color: #008000;"&gt;* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 24&lt;/span&gt; &lt;span style="color: #008000;"&gt;* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 25&lt;/span&gt; &lt;span style="color: #008000;"&gt;* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 26&lt;/span&gt; &lt;span style="color: #008000;"&gt;* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 27&lt;/span&gt; &lt;span style="color: #008000;"&gt;* DEALINGS IN THE SOFTWARE.                                                   *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 28&lt;/span&gt; &lt;span style="color: #008000;"&gt;*                                                                             *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 29&lt;/span&gt; &lt;span style="color: #008000;"&gt;* This agreement shall be governed in all respects by the laws of the State   *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 30&lt;/span&gt; &lt;span style="color: #008000;"&gt;* of California and by the laws of the United States of America.              *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 31&lt;/span&gt; &lt;span style="color: #008000;"&gt;*                                                                             *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 32&lt;/span&gt; &lt;span style="color: #008000;"&gt;*****************************************************************************&lt;/span&gt;&lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 33&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 34&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 35&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_irq.h is the Nios II specific implementation of the interrupt controller &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 36&lt;/span&gt; &lt;span style="color: #008000;"&gt; * interface.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 37&lt;/span&gt; &lt;span style="color: #008000;"&gt; *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 38&lt;/span&gt; &lt;span style="color: #008000;"&gt; * Nios II includes optional support for an external interrupt controller. &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 39&lt;/span&gt; &lt;span style="color: #008000;"&gt; * When an external controller is present, the "Enhanced" interrupt API&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 40&lt;/span&gt; &lt;span style="color: #008000;"&gt; * must be used to manage individual interrupts. The enhanced API also&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 41&lt;/span&gt; &lt;span style="color: #008000;"&gt; * supports the processor's internal interrupt controller. Certain API&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 42&lt;/span&gt; &lt;span style="color: #008000;"&gt; * members are accessible from either the "legacy" or "enhanced" interrpt&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 43&lt;/span&gt; &lt;span style="color: #008000;"&gt; * API. &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 44&lt;/span&gt; &lt;span style="color: #008000;"&gt; *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 45&lt;/span&gt; &lt;span style="color: #008000;"&gt; * Regardless of which API is in use, this file should be included by&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 46&lt;/span&gt; &lt;span style="color: #008000;"&gt; * application code and device drivers that register ISRs or manage interrpts.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 47&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 48&lt;/span&gt; #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 49&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 50&lt;/span&gt; #include &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;nios2.h&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 51&lt;/span&gt; #include &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;alt_types.h&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 52&lt;/span&gt; #include &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;system.h&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 53&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 54&lt;/span&gt; #ifdef __cplusplus&lt;br /&gt;&lt;span style="color: #008080;"&gt; 55&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;extern&lt;/span&gt; &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;C&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 56&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: #008080;"&gt; 57&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt; /* __cplusplus */&lt;br /&gt;&lt;span style="color: #008080;"&gt; 58&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 59&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 60&lt;/span&gt; &lt;span style="color: #008000;"&gt; * Macros used by alt_irq_enabled&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 61&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 62&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#define&lt;/span&gt; ALT_IRQ_ENABLED  1&lt;br /&gt;&lt;span style="color: #008080;"&gt; 63&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#define&lt;/span&gt; ALT_IRQ_DISABLED 0  &lt;br /&gt;&lt;span style="color: #008080;"&gt; 64&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 65&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 66&lt;/span&gt; &lt;span style="color: #008000;"&gt; * Number of available interrupts in internal interrupt controller.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 67&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 68&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#define&lt;/span&gt; ALT_NIRQ NIOS2_NIRQ&lt;br /&gt;&lt;span style="color: #008080;"&gt; 69&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 70&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 71&lt;/span&gt; &lt;span style="color: #008000;"&gt; * Used by alt_irq_disable_all() and alt_irq_enable_all().&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 72&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 73&lt;/span&gt; typedef &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; alt_irq_context;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 74&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 75&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt; ISR Prototype &lt;/span&gt;&lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 76&lt;/span&gt; #ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT&lt;br /&gt;&lt;span style="color: #008080;"&gt; 77&lt;/span&gt; typedef &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; (*alt_isr_func)(&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt;* isr_context);&lt;br /&gt;&lt;span style="color: #008080;"&gt; 78&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#else&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 79&lt;/span&gt; typedef &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; (*alt_isr_func)(&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt;* isr_context, alt_u32 id);&lt;br /&gt;&lt;span style="color: #008080;"&gt; 80&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 81&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 82&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 83&lt;/span&gt; &lt;span style="color: #008000;"&gt; * The following protypes and routines are supported by both&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 84&lt;/span&gt; &lt;span style="color: #008000;"&gt; * the enhanced and legacy interrupt APIs&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 85&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 86&lt;/span&gt;  &lt;br /&gt;&lt;span style="color: #008080;"&gt; 87&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 88&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_irq_enabled can be called to determine if the processor's global&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 89&lt;/span&gt; &lt;span style="color: #008000;"&gt; * interrupt enable is asserted. The return value is zero if interrupts &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 90&lt;/span&gt; &lt;span style="color: #008000;"&gt; * are disabled, and non-zero otherwise.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 91&lt;/span&gt; &lt;span style="color: #008000;"&gt; *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 92&lt;/span&gt; &lt;span style="color: #008000;"&gt; * Whether the internal or external interrupt controller is present, &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 93&lt;/span&gt; &lt;span style="color: #008000;"&gt; * individual interrupts may still be disabled. Use the other API to query&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 94&lt;/span&gt; &lt;span style="color: #008000;"&gt; * a specific interrupt. &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 95&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 96&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; ALT_INLINE &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; ALT_ALWAYS_INLINE alt_irq_enabled (&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt;)&lt;br /&gt;&lt;span style="color: #008080;"&gt; 97&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: #008080;"&gt; 98&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; status;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 99&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;100&lt;/span&gt;   NIOS2_READ_STATUS (status);&lt;br /&gt;&lt;span style="color: #008080;"&gt;101&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;102&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; status &amp;amp; NIOS2_STATUS_PIE_MSK; &lt;br /&gt;&lt;span style="color: #008080;"&gt;103&lt;/span&gt; }&lt;br /&gt;&lt;span style="color: #008080;"&gt;104&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;105&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;106&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_irq_disable_all() &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;107&lt;/span&gt; &lt;span style="color: #008000;"&gt; *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;108&lt;/span&gt; &lt;span style="color: #008000;"&gt; * This routine inhibits all interrupts by negating the status register PIE &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;109&lt;/span&gt; &lt;span style="color: #008000;"&gt; * bit. It returns the previous contents of the CPU status register (IRQ &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;110&lt;/span&gt; &lt;span style="color: #008000;"&gt; * context) which can be used to restore the status register PIE bit to its &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;111&lt;/span&gt; &lt;span style="color: #008000;"&gt; * state before this routine was called.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;112&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;113&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; ALT_INLINE alt_irq_context ALT_ALWAYS_INLINE &lt;br /&gt;&lt;span style="color: #008080;"&gt;114&lt;/span&gt;        alt_irq_disable_all (&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt;)&lt;br /&gt;&lt;span style="color: #008080;"&gt;115&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: #008080;"&gt;116&lt;/span&gt;   alt_irq_context context;&lt;br /&gt;&lt;span style="color: #008080;"&gt;117&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;118&lt;/span&gt;   NIOS2_READ_STATUS (context);&lt;br /&gt;&lt;span style="color: #008080;"&gt;119&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;120&lt;/span&gt;   NIOS2_WRITE_STATUS (context &amp;amp; ~NIOS2_STATUS_PIE_MSK);&lt;br /&gt;&lt;span style="color: #008080;"&gt;121&lt;/span&gt;   &lt;br /&gt;&lt;span style="color: #008080;"&gt;122&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; context;&lt;br /&gt;&lt;span style="color: #008080;"&gt;123&lt;/span&gt; }&lt;br /&gt;&lt;span style="color: #008080;"&gt;124&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;125&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;126&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_irq_enable_all() &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;127&lt;/span&gt; &lt;span style="color: #008000;"&gt; *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;128&lt;/span&gt; &lt;span style="color: #008000;"&gt; * Enable all interrupts that were previously disabled by alt_irq_disable_all()&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;129&lt;/span&gt; &lt;span style="color: #008000;"&gt; *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;130&lt;/span&gt; &lt;span style="color: #008000;"&gt; * This routine accepts a context to restore the CPU status register PIE bit&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;131&lt;/span&gt; &lt;span style="color: #008000;"&gt; * to the state prior to a call to alt_irq_disable_all().&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;132&lt;/span&gt; &lt;span style="color: #008000;"&gt; &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;133&lt;/span&gt; &lt;span style="color: #008000;"&gt; * In the case of nested calls to alt_irq_disable_all()/alt_irq_enable_all(), &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;134&lt;/span&gt; &lt;span style="color: #008000;"&gt; * this means that alt_irq_enable_all() does not necessarily re-enable&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;135&lt;/span&gt; &lt;span style="color: #008000;"&gt; * interrupts.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;136&lt;/span&gt; &lt;span style="color: #008000;"&gt; *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;137&lt;/span&gt; &lt;span style="color: #008000;"&gt; * This routine will perform a read-modify-write sequence to restore only&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;138&lt;/span&gt; &lt;span style="color: #008000;"&gt; * status.PIE if the processor is configured with options that add additional &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;139&lt;/span&gt; &lt;span style="color: #008000;"&gt; * writeable status register bits. These include the MMU, MPU, the enhanced &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;140&lt;/span&gt; &lt;span style="color: #008000;"&gt; * interrupt controller port, and shadow registers. Otherwise, as a performance&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;141&lt;/span&gt; &lt;span style="color: #008000;"&gt; * enhancement, status is overwritten with the prior context. &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;142&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;143&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; ALT_INLINE &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; ALT_ALWAYS_INLINE &lt;br /&gt;&lt;span style="color: #008080;"&gt;144&lt;/span&gt;        alt_irq_enable_all (alt_irq_context context)&lt;br /&gt;&lt;span style="color: #008080;"&gt;145&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: #008080;"&gt;146&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#if&lt;/span&gt; (NIOS2_NUM_OF_SHADOW_REG_SETS &amp;gt; 0) || (defined NIOS2_EIC_PRESENT) || \&lt;br /&gt;&lt;span style="color: #008080;"&gt;147&lt;/span&gt;     (defined NIOS2_MMU_PRESENT) || (defined NIOS2_MPU_PRESENT)&lt;br /&gt;&lt;span style="color: #008080;"&gt;148&lt;/span&gt;   alt_irq_context status;&lt;br /&gt;&lt;span style="color: #008080;"&gt;149&lt;/span&gt;   &lt;br /&gt;&lt;span style="color: #008080;"&gt;150&lt;/span&gt;   NIOS2_READ_STATUS (status);&lt;br /&gt;&lt;span style="color: #008080;"&gt;151&lt;/span&gt;   &lt;br /&gt;&lt;span style="color: #008080;"&gt;152&lt;/span&gt;   status &amp;amp;= ~NIOS2_STATUS_PIE_MSK;&lt;br /&gt;&lt;span style="color: #008080;"&gt;153&lt;/span&gt;   status |= (context &amp;amp; NIOS2_STATUS_PIE_MSK);&lt;br /&gt;&lt;span style="color: #008080;"&gt;154&lt;/span&gt;   &lt;br /&gt;&lt;span style="color: #008080;"&gt;155&lt;/span&gt;   NIOS2_WRITE_STATUS (status);&lt;br /&gt;&lt;span style="color: #008080;"&gt;156&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#else&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;157&lt;/span&gt;   NIOS2_WRITE_STATUS (context);&lt;br /&gt;&lt;span style="color: #008080;"&gt;158&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;159&lt;/span&gt; }&lt;br /&gt;&lt;span style="color: #008080;"&gt;160&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;161&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;162&lt;/span&gt; &lt;span style="color: #008000;"&gt; * The function alt_irq_init() is defined within the auto-generated file&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;163&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_sys_init.c. This function calls the initilization macros for all&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;164&lt;/span&gt; &lt;span style="color: #008000;"&gt; * interrupt controllers in the system at config time, before any other&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;165&lt;/span&gt; &lt;span style="color: #008000;"&gt; * non-interrupt controller driver is initialized.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;166&lt;/span&gt; &lt;span style="color: #008000;"&gt; *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;167&lt;/span&gt; &lt;span style="color: #008000;"&gt; * The "base" parameter is ignored and only present for backwards-compatibility.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;168&lt;/span&gt; &lt;span style="color: #008000;"&gt; * It is recommended that NULL is passed in for the "base" parameter.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;169&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;170&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;extern&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; alt_irq_init (&lt;span style="color: #0000ff;"&gt;const&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt;* &lt;span style="color: #0000ff;"&gt;base&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;171&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;172&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;173&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_irq_cpu_enable_interrupts() enables the CPU to start taking interrupts.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;174&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;175&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; ALT_INLINE &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; ALT_ALWAYS_INLINE &lt;br /&gt;&lt;span style="color: #008080;"&gt;176&lt;/span&gt;        alt_irq_cpu_enable_interrupts ()&lt;br /&gt;&lt;span style="color: #008080;"&gt;177&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: #008080;"&gt;178&lt;/span&gt;     NIOS2_WRITE_STATUS(NIOS2_STATUS_PIE_MSK&lt;br /&gt;&lt;span style="color: #008080;"&gt;179&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#if&lt;/span&gt; defined(NIOS2_EIC_PRESENT) &amp;amp;&amp;amp; (NIOS2_NUM_OF_SHADOW_REG_SETS &amp;gt; 0)&lt;br /&gt;&lt;span style="color: #008080;"&gt;180&lt;/span&gt;     | NIOS2_STATUS_RSIE_MSK&lt;br /&gt;&lt;span style="color: #008080;"&gt;181&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt;      &lt;br /&gt;&lt;span style="color: #008080;"&gt;182&lt;/span&gt;       );&lt;br /&gt;&lt;span style="color: #008080;"&gt;183&lt;/span&gt; }&lt;br /&gt;&lt;span style="color: #008080;"&gt;184&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;185&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;186&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;187&lt;/span&gt; &lt;span style="color: #008000;"&gt; * Prototypes for the enhanced interrupt API.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;188&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;189&lt;/span&gt; #ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT&lt;br /&gt;&lt;span style="color: #008080;"&gt;190&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;191&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_ic_isr_register() can be used to register an interrupt handler. If the&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;192&lt;/span&gt; &lt;span style="color: #008000;"&gt; * function is succesful, then the requested interrupt will be enabled upon &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;193&lt;/span&gt; &lt;span style="color: #008000;"&gt; * return.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;194&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;195&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;extern&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; alt_ic_isr_register(alt_u32 ic_id,&lt;br /&gt;&lt;span style="color: #008080;"&gt;196&lt;/span&gt;                         alt_u32 irq,&lt;br /&gt;&lt;span style="color: #008080;"&gt;197&lt;/span&gt;                         alt_isr_func isr,&lt;br /&gt;&lt;span style="color: #008080;"&gt;198&lt;/span&gt;                         &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; *isr_context,&lt;br /&gt;&lt;span style="color: #008080;"&gt;199&lt;/span&gt;                         &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; *flags);&lt;br /&gt;&lt;span style="color: #008080;"&gt;200&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;201&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;202&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_ic_irq_enable() and alt_ic_irq_disable() enable/disable a specific &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;203&lt;/span&gt; &lt;span style="color: #008000;"&gt; * interrupt by using IRQ port and interrupt controller instance.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;204&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;205&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; alt_ic_irq_enable (alt_u32 ic_id, alt_u32 irq);&lt;br /&gt;&lt;span style="color: #008080;"&gt;206&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; alt_ic_irq_disable(alt_u32 ic_id, alt_u32 irq);        &lt;br /&gt;&lt;span style="color: #008080;"&gt;207&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;208&lt;/span&gt;  &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;209&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_ic_irq_enabled() indicates whether a specific interrupt, as&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;210&lt;/span&gt; &lt;span style="color: #008000;"&gt; * specified by IRQ port and interrupt controller instance is enabled.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;211&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;        &lt;br /&gt;&lt;span style="color: #008080;"&gt;212&lt;/span&gt; alt_u32 alt_ic_irq_enabled(alt_u32 ic_id, alt_u32 irq);&lt;br /&gt;&lt;span style="color: #008080;"&gt;213&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;214&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#else&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;215&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;216&lt;/span&gt; &lt;span style="color: #008000;"&gt; * Prototypes for the legacy interrupt API.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;217&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;218&lt;/span&gt; #include &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;priv/alt_legacy_irq.h&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;219&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;220&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;221&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;222&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;223&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_irq_pending() returns a bit list of the current pending interrupts.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;224&lt;/span&gt; &lt;span style="color: #008000;"&gt; * This is used by alt_irq_handler() to determine which registered interrupt&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;225&lt;/span&gt; &lt;span style="color: #008000;"&gt; * handlers should be called.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;226&lt;/span&gt; &lt;span style="color: #008000;"&gt; *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;227&lt;/span&gt; &lt;span style="color: #008000;"&gt; * This routine is only available for the Nios II internal interrupt&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;228&lt;/span&gt; &lt;span style="color: #008000;"&gt; * controller.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;229&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;230&lt;/span&gt; #ifndef NIOS2_EIC_PRESENT&lt;br /&gt;&lt;span style="color: #008080;"&gt;231&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; ALT_INLINE alt_u32 ALT_ALWAYS_INLINE alt_irq_pending (&lt;span style="color: #0000ff;"&gt;void&lt;/span&gt;)&lt;br /&gt;&lt;span style="color: #008080;"&gt;232&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: #008080;"&gt;233&lt;/span&gt;   alt_u32 active;&lt;br /&gt;&lt;span style="color: #008080;"&gt;234&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;235&lt;/span&gt;   NIOS2_READ_IPENDING (active);&lt;br /&gt;&lt;span style="color: #008080;"&gt;236&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;237&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; active;&lt;br /&gt;&lt;span style="color: #008080;"&gt;238&lt;/span&gt; }&lt;br /&gt;&lt;span style="color: #008080;"&gt;239&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;240&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;241&lt;/span&gt; #ifdef __cplusplus&lt;br /&gt;&lt;span style="color: #008080;"&gt;242&lt;/span&gt; }&lt;br /&gt;&lt;span style="color: #008080;"&gt;243&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt; /* __cplusplus */&lt;br /&gt;&lt;span style="color: #008080;"&gt;244&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;245&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt; /* __ALT_IRQ_H__ */&lt;/div&gt;&lt;p&gt;186行&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #008080;"&gt; 1&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 2&lt;/span&gt; &lt;span style="color: #008000;"&gt; * Prototypes for the enhanced interrupt API.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 3&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 4&lt;/span&gt; #ifdef ALT_ENHANCED_INTERRUPT_API_PRESENT&lt;br /&gt;&lt;span style="color: #008080;"&gt; 5&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 6&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_ic_isr_register() can be used to register an interrupt handler. If the&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 7&lt;/span&gt; &lt;span style="color: #008000;"&gt; * function is succesful, then the requested interrupt will be enabled upon &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 8&lt;/span&gt; &lt;span style="color: #008000;"&gt; * return.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 9&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;10&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;extern&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; alt_ic_isr_register(alt_u32 ic_id,&lt;br /&gt;&lt;span style="color: #008080;"&gt;11&lt;/span&gt;                         alt_u32 irq,&lt;br /&gt;&lt;span style="color: #008080;"&gt;12&lt;/span&gt;                         alt_isr_func isr,&lt;br /&gt;&lt;span style="color: #008080;"&gt;13&lt;/span&gt;                         &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; *isr_context,&lt;br /&gt;&lt;span style="color: #008080;"&gt;14&lt;/span&gt;                         &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; *flags);&lt;br /&gt;&lt;span style="color: #008080;"&gt;15&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;16&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;17&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_ic_irq_enable() and alt_ic_irq_disable() enable/disable a specific &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;18&lt;/span&gt; &lt;span style="color: #008000;"&gt; * interrupt by using IRQ port and interrupt controller instance.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;19&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;20&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; alt_ic_irq_enable (alt_u32 ic_id, alt_u32 irq);&lt;br /&gt;&lt;span style="color: #008080;"&gt;21&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; alt_ic_irq_disable(alt_u32 ic_id, alt_u32 irq);        &lt;br /&gt;&lt;span style="color: #008080;"&gt;22&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;23&lt;/span&gt;  &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;24&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_ic_irq_enabled() indicates whether a specific interrupt, as&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;25&lt;/span&gt; &lt;span style="color: #008000;"&gt; * specified by IRQ port and interrupt controller instance is enabled.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;26&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;        &lt;br /&gt;&lt;span style="color: #008080;"&gt;27&lt;/span&gt; alt_u32 alt_ic_irq_enabled(alt_u32 ic_id, alt_u32 irq);&lt;br /&gt;&lt;span style="color: #008080;"&gt;28&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;29&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#else&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;30&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;31&lt;/span&gt; &lt;span style="color: #008000;"&gt; * Prototypes for the legacy interrupt API.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;32&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;33&lt;/span&gt; #include &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;priv/alt_legacy_irq.h&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;34&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt; &lt;/div&gt;&lt;p&gt;我們可以發現，當system.h有定義&lt;strong&gt;ALT_ENHANCED_INTERRUPT_API_PRESENT&lt;/strong&gt;常數時，就有alt_ic_isr_register()可用，若沒定義，則&lt;strong&gt;#inlclude priv/alt_legacy_irq.h&lt;/strong&gt;。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;alt_legacy_irq.h / C&lt;/strong&gt;&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #008080;"&gt;  1&lt;/span&gt; #ifndef __ALT_LEGACY_IRQ_H__&lt;br /&gt;&lt;span style="color: #008080;"&gt;  2&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#define&lt;/span&gt; __ALT_LEGACY_IRQ_H__&lt;br /&gt;&lt;span style="color: #008080;"&gt;  3&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;  4&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;*****************************************************************************&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;  5&lt;/span&gt; &lt;span style="color: #008000;"&gt;*                                                                             *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;  6&lt;/span&gt; &lt;span style="color: #008000;"&gt;* License Agreement                                                           *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;  7&lt;/span&gt; &lt;span style="color: #008000;"&gt;*                                                                             *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;  8&lt;/span&gt; &lt;span style="color: #008000;"&gt;* Copyright (c) 2009 Altera Corporation, San Jose, California, USA.           *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;  9&lt;/span&gt; &lt;span style="color: #008000;"&gt;* All rights reserved.                                                        *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 10&lt;/span&gt; &lt;span style="color: #008000;"&gt;*                                                                             *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 11&lt;/span&gt; &lt;span style="color: #008000;"&gt;* Permission is hereby granted, free of charge, to any person obtaining a     *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 12&lt;/span&gt; &lt;span style="color: #008000;"&gt;* copy of this software and associated documentation files (the "Software"),  *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 13&lt;/span&gt; &lt;span style="color: #008000;"&gt;* to deal in the Software without restriction, including without limitation   *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 14&lt;/span&gt; &lt;span style="color: #008000;"&gt;* the rights to use, copy, modify, merge, publish, distribute, sublicense,    *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 15&lt;/span&gt; &lt;span style="color: #008000;"&gt;* and/or sell copies of the Software, and to permit persons to whom the       *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 16&lt;/span&gt; &lt;span style="color: #008000;"&gt;* Software is furnished to do so, subject to the following conditions:        *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 17&lt;/span&gt; &lt;span style="color: #008000;"&gt;*                                                                             *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 18&lt;/span&gt; &lt;span style="color: #008000;"&gt;* The above copyright notice and this permission notice shall be included in  *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 19&lt;/span&gt; &lt;span style="color: #008000;"&gt;* all copies or substantial portions of the Software.                         *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 20&lt;/span&gt; &lt;span style="color: #008000;"&gt;*                                                                             *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 21&lt;/span&gt; &lt;span style="color: #008000;"&gt;* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 22&lt;/span&gt; &lt;span style="color: #008000;"&gt;* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 23&lt;/span&gt; &lt;span style="color: #008000;"&gt;* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 24&lt;/span&gt; &lt;span style="color: #008000;"&gt;* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 25&lt;/span&gt; &lt;span style="color: #008000;"&gt;* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 26&lt;/span&gt; &lt;span style="color: #008000;"&gt;* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 27&lt;/span&gt; &lt;span style="color: #008000;"&gt;* DEALINGS IN THE SOFTWARE.                                                   *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 28&lt;/span&gt; &lt;span style="color: #008000;"&gt;*                                                                             *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 29&lt;/span&gt; &lt;span style="color: #008000;"&gt;* This agreement shall be governed in all respects by the laws of the State   *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 30&lt;/span&gt; &lt;span style="color: #008000;"&gt;* of California and by the laws of the United States of America.              *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 31&lt;/span&gt; &lt;span style="color: #008000;"&gt;*                                                                             *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 32&lt;/span&gt; &lt;span style="color: #008000;"&gt;*****************************************************************************&lt;/span&gt;&lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 33&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 34&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 35&lt;/span&gt; &lt;span style="color: #008000;"&gt; * This file provides prototypes and inline implementations of certain routines&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 36&lt;/span&gt; &lt;span style="color: #008000;"&gt; * used by the legacy interrupt API. Do not include this in your driver or &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 37&lt;/span&gt; &lt;span style="color: #008000;"&gt; * application source files, use "sys/alt_irq.h" instead to access the proper&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 38&lt;/span&gt; &lt;span style="color: #008000;"&gt; * public API.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 39&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 40&lt;/span&gt;  &lt;br /&gt;&lt;span style="color: #008080;"&gt; 41&lt;/span&gt; #include &amp;lt;errno.h&amp;gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 42&lt;/span&gt; #include &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;system.h&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 43&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 44&lt;/span&gt; #ifndef NIOS2_EIC_PRESENT&lt;br /&gt;&lt;span style="color: #008080;"&gt; 45&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 46&lt;/span&gt; #include &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;nios2.h&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 47&lt;/span&gt; #include &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;alt_types.h&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 48&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 49&lt;/span&gt; #include &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;sys/alt_irq.h&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 50&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 51&lt;/span&gt; #ifdef __cplusplus&lt;br /&gt;&lt;span style="color: #008080;"&gt; 52&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;extern&lt;/span&gt; &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;C&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 53&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: #008080;"&gt; 54&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt; /* __cplusplus */&lt;br /&gt;&lt;span style="color: #008080;"&gt; 55&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 56&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 57&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_irq_register() can be used to register an interrupt handler. If the &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 58&lt;/span&gt; &lt;span style="color: #008000;"&gt; * function is succesful, then the requested interrupt will be enabled upon &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 59&lt;/span&gt; &lt;span style="color: #008000;"&gt; * return.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 60&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 61&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;extern&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; alt_irq_register (alt_u32 id, &lt;br /&gt;&lt;span style="color: #008080;"&gt; 62&lt;/span&gt;                              &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt;*   context, &lt;br /&gt;&lt;span style="color: #008080;"&gt; 63&lt;/span&gt;                              alt_isr_func handler);&lt;br /&gt;&lt;span style="color: #008080;"&gt; 64&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 65&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 66&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_irq_disable() disables the individual interrupt indicated by "id".&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 67&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 68&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; ALT_INLINE &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; ALT_ALWAYS_INLINE alt_irq_disable (alt_u32 id)&lt;br /&gt;&lt;span style="color: #008080;"&gt; 69&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: #008080;"&gt; 70&lt;/span&gt;   alt_irq_context  status;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 71&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;extern&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;volatile&lt;/span&gt; alt_u32 alt_irq_active;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 72&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 73&lt;/span&gt;   status = alt_irq_disable_all ();&lt;br /&gt;&lt;span style="color: #008080;"&gt; 74&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 75&lt;/span&gt;   alt_irq_active &amp;amp;= ~(&lt;span style="color: #800080;"&gt;1&lt;/span&gt; &amp;lt;&amp;lt; id);&lt;br /&gt;&lt;span style="color: #008080;"&gt; 76&lt;/span&gt;   NIOS2_WRITE_IENABLE (alt_irq_active);&lt;br /&gt;&lt;span style="color: #008080;"&gt; 77&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 78&lt;/span&gt;   alt_irq_enable_all(status);&lt;br /&gt;&lt;span style="color: #008080;"&gt; 79&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 80&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; &lt;span style="color: #800080;"&gt;0&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 81&lt;/span&gt; }&lt;br /&gt;&lt;span style="color: #008080;"&gt; 82&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 83&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 84&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_irq_enable() enables the individual interrupt indicated by "id".&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 85&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 86&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; ALT_INLINE &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; ALT_ALWAYS_INLINE alt_irq_enable (alt_u32 id)&lt;br /&gt;&lt;span style="color: #008080;"&gt; 87&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: #008080;"&gt; 88&lt;/span&gt;   alt_irq_context  status;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 89&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;extern&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;volatile&lt;/span&gt; alt_u32 alt_irq_active;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 90&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 91&lt;/span&gt;   status = alt_irq_disable_all ();&lt;br /&gt;&lt;span style="color: #008080;"&gt; 92&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 93&lt;/span&gt;   alt_irq_active |= (&lt;span style="color: #800080;"&gt;1&lt;/span&gt; &amp;lt;&amp;lt; id);&lt;br /&gt;&lt;span style="color: #008080;"&gt; 94&lt;/span&gt;   NIOS2_WRITE_IENABLE (alt_irq_active);&lt;br /&gt;&lt;span style="color: #008080;"&gt; 95&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 96&lt;/span&gt;   alt_irq_enable_all(status);&lt;br /&gt;&lt;span style="color: #008080;"&gt; 97&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt; 98&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; &lt;span style="color: #800080;"&gt;0&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 99&lt;/span&gt; }&lt;br /&gt;&lt;span style="color: #008080;"&gt;100&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;101&lt;/span&gt; #ifndef ALT_EXCEPTION_STACK&lt;br /&gt;&lt;span style="color: #008080;"&gt;102&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;103&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_irq_initerruptable() should only be called from within an ISR. It is used&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;104&lt;/span&gt; &lt;span style="color: #008000;"&gt; * to allow higer priority interrupts to interrupt the current ISR. The input&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;105&lt;/span&gt; &lt;span style="color: #008000;"&gt; * argument, "priority", is the priority, i.e. interrupt number of the current&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;106&lt;/span&gt; &lt;span style="color: #008000;"&gt; * interrupt.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;107&lt;/span&gt; &lt;span style="color: #008000;"&gt; *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;108&lt;/span&gt; &lt;span style="color: #008000;"&gt; * If this function is called, then the ISR is required to make a call to&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;109&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_irq_non_interruptible() before returning. The input argument to&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;110&lt;/span&gt; &lt;span style="color: #008000;"&gt; * alt_irq_non_interruptible() is the return value from alt_irq_interruptible().&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;111&lt;/span&gt; &lt;span style="color: #008000;"&gt; *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;112&lt;/span&gt; &lt;span style="color: #008000;"&gt; * Care should be taken when using this pair of functions, since they increasing&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;113&lt;/span&gt; &lt;span style="color: #008000;"&gt; * the system overhead associated with interrupt handling.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;114&lt;/span&gt; &lt;span style="color: #008000;"&gt; *&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;115&lt;/span&gt; &lt;span style="color: #008000;"&gt; * If you are using an exception stack then nested interrupts won't work, so&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;116&lt;/span&gt; &lt;span style="color: #008000;"&gt; * these functions are not available in that case.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;117&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;118&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; ALT_INLINE alt_u32 ALT_ALWAYS_INLINE alt_irq_interruptible (alt_u32 priority)&lt;br /&gt;&lt;span style="color: #008080;"&gt;119&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: #008080;"&gt;120&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;extern&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;volatile&lt;/span&gt; alt_u32 alt_priority_mask;&lt;br /&gt;&lt;span style="color: #008080;"&gt;121&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;extern&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;volatile&lt;/span&gt; alt_u32 alt_irq_active;&lt;br /&gt;&lt;span style="color: #008080;"&gt;122&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;123&lt;/span&gt;   alt_u32 old_priority;&lt;br /&gt;&lt;span style="color: #008080;"&gt;124&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;125&lt;/span&gt;   old_priority      = alt_priority_mask;&lt;br /&gt;&lt;span style="color: #008080;"&gt;126&lt;/span&gt;   alt_priority_mask = (&lt;span style="color: #800080;"&gt;1&lt;/span&gt; &amp;lt;&amp;lt; priority) - &lt;span style="color: #800080;"&gt;1&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;127&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;128&lt;/span&gt;   NIOS2_WRITE_IENABLE (alt_irq_active &amp;amp; alt_priority_mask);&lt;br /&gt;&lt;span style="color: #008080;"&gt;129&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;130&lt;/span&gt;   NIOS2_WRITE_STATUS (&lt;span style="color: #800080;"&gt;1&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;131&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;132&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; old_priority; &lt;br /&gt;&lt;span style="color: #008080;"&gt;133&lt;/span&gt; }&lt;br /&gt;&lt;span style="color: #008080;"&gt;134&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;135&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;136&lt;/span&gt; &lt;span style="color: #008000;"&gt; * See Comments above for alt_irq_interruptible() for an explanation of the use of this&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;137&lt;/span&gt; &lt;span style="color: #008000;"&gt; * function.&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt;138&lt;/span&gt;  &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;139&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;static&lt;/span&gt; ALT_INLINE &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt; ALT_ALWAYS_INLINE alt_irq_non_interruptible (alt_u32 mask)&lt;br /&gt;&lt;span style="color: #008080;"&gt;140&lt;/span&gt; {&lt;br /&gt;&lt;span style="color: #008080;"&gt;141&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;extern&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;volatile&lt;/span&gt; alt_u32 alt_priority_mask;&lt;br /&gt;&lt;span style="color: #008080;"&gt;142&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;extern&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;volatile&lt;/span&gt; alt_u32 alt_irq_active;&lt;br /&gt;&lt;span style="color: #008080;"&gt;143&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;144&lt;/span&gt;   NIOS2_WRITE_STATUS (&lt;span style="color: #800080;"&gt;0&lt;/span&gt;);  &lt;br /&gt;&lt;span style="color: #008080;"&gt;145&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;146&lt;/span&gt;   alt_priority_mask = mask;&lt;br /&gt;&lt;span style="color: #008080;"&gt;147&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;148&lt;/span&gt;   NIOS2_WRITE_IENABLE (mask &amp;amp; alt_irq_active);  &lt;br /&gt;&lt;span style="color: #008080;"&gt;149&lt;/span&gt; }&lt;br /&gt;&lt;span style="color: #008080;"&gt;150&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt; /* ALT_EXCEPTION_STACK */&lt;br /&gt;&lt;span style="color: #008080;"&gt;151&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;152&lt;/span&gt; #ifdef __cplusplus&lt;br /&gt;&lt;span style="color: #008080;"&gt;153&lt;/span&gt; }&lt;br /&gt;&lt;span style="color: #008080;"&gt;154&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt; /* __cplusplus */&lt;br /&gt;&lt;span style="color: #008080;"&gt;155&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;156&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt; /* NIOS2_EIC_PRESENT */&lt;br /&gt;&lt;span style="color: #008080;"&gt;157&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;158&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;#endif&lt;/span&gt; /* __ALT_LEGACY_IRQ_H__ */&lt;/div&gt;&lt;p&gt;61行&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #0000ff;"&gt;extern&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; alt_irq_register (alt_u32 id, &lt;br /&gt;                             &lt;span style="color: #0000ff;"&gt;void&lt;/span&gt;*   context, &lt;br /&gt;                             alt_isr_func handler);&lt;/div&gt;&lt;p&gt;則是我們所用舊的alt_irq_register()。&lt;/p&gt;&lt;p&gt;至於ISR的prototype新舊不一樣，trace的方式也一樣，就不再贅述。&lt;/p&gt;&lt;p&gt;至於為什麼目前在subsystem的interrupt只能使用舊的ISR register方式，我並不清楚，或許Quartus II未來版本會有所改變。&lt;/p&gt;&lt;p&gt;使用以上方法後，就可正確的在App project收到subsystem的interrupt了。&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="irq011" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201052357357972.gif" alt="irq011" width="807" height="620" border="0" /&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br /&gt;我們在本文看到了Qsys與SOPC Builder在subsystem與interrupt的處理有很大的差異，也看到了目前Qsys與Nios II SBT尚有些bug待修正。目前Quartus II 11.0與Quartus II 11.1都還有system.h的bug，相信未來的版本一定會修正這個問題。假如你現在要在subsystem使用interrupt，可暫時使用本文的方式當作short term solution。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Reference&lt;br /&gt;&lt;/strong&gt;[1] &lt;a href="http://www.altera.com/literature/hb/qts/qsys_interconnect.pdf" target="_blank"&gt;Qsys Interconnect&lt;/a&gt;&lt;br /&gt;[2] &lt;a href="http://www.altera.com/literature/rn/archives/rn_qts_110.pdf" target="_blank"&gt;Quartus II 11.0 Release Notes&lt;/a&gt;&lt;br /&gt;[3] &lt;a href="http://www.alterawiki.com/wiki/New_Qsys_Issues#Interrupts" target="_blank"&gt;Altera Wiki : New Qsys Issues for Interrupts&lt;/a&gt;&lt;br /&gt;[4] &lt;a href="http://www.altera.com/literature/hb/nios2/n2sw_nii52006.pdf" target="_blank"&gt;Exception Handling&lt;/a&gt;&lt;/p&gt;&lt;p&gt;全文完。&lt;/p&gt;&lt;img src="http://www.cnblogs.com/oomusou/aggbug/2312573.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/oomusou/archive/2012/01/04/subsystem_interrupt.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/oomusou/archive/2012/01/03/revising_bsp.html</id><title type="text">(原創) Qsys或RTL做修改後，Nios II SBT該如何面對新的硬體? (SOC) (Nios II) (Qsys)</title><summary type="text">SOPC最可貴之處就是它的彈性，所以儘管已經建立了Nios II SBT project，你還可以去更改FPGA的RTL或者Qsys的架構與IP，此時Nios II SBT project該做哪些步驟才能反映出更改過的硬體架構呢?是該Generate BSP？還是該BSP Editor?或者該Build Project?其執行順序又是如何呢？</summary><published>2012-01-03T12:51:00Z</published><updated>2012-01-03T12:51:00Z</updated><author><name>真 OO无双</name><uri>http://www.cnblogs.com/oomusou/</uri></author><link rel="alternate" href="http://www.cnblogs.com/oomusou/archive/2012/01/03/revising_bsp.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/oomusou/archive/2012/01/03/revising_bsp.html"/><content type="html">&lt;p&gt;&lt;strong&gt;Abstract&lt;/strong&gt;&lt;br&gt;SOPC最可貴之處就是它的彈性，所以儘管已經建立了Nios II SBT project，你還可以去更改FPGA的RTL或者Qsys的架構與IP，此時Nios II SBT project該做哪些步驟才能反映出更改過的硬體架構呢?是該Generate BSP？還是該BSP Editor?或者該Build Project?其執行順序又是如何呢？&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Introduction&lt;br&gt;使用環境：Windows XP SP3 + VirtualBox 4.1.2 + Quartus II 11.0&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;自從使用Nios II SBT，每次Qsys或者FPGA的RTL做修改後，就一直猶豫到底要只執行Build Project就好?還是要先Clean Project?還是必須先執行Generate BSP?那BSP Editor也要執行嗎?因為有時總會有些不可預期的錯誤產生，一直到我看到[1] &lt;a href="http://www.altera.com/literature/hb/nios2/n2sw_nii52015.pdf" target="_blank"&gt;Nios II Software Build Tools&lt;/a&gt;的p.4-30的&lt;strong&gt;&lt;font color="#008080"&gt;Revising Your BSP&lt;/font&gt;&lt;/strong&gt;章節，所有的疑問才豁然開朗。建議你也完整的看一下[1] &lt;a href="http://www.altera.com/literature/hb/nios2/n2sw_nii52015.pdf" target="_blank"&gt;Nios II Software Build Tools&lt;/a&gt;的&lt;strong&gt;&lt;font color="#008080"&gt;Revising Your BSP&lt;/font&gt;&lt;/strong&gt;章節。本文只是個人理解下的總結，不能取代Altera的官方資料。&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Generate BSP&lt;/strong&gt;：其執行流程如下：&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="revise004" border="0" alt="revise004" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201032205404267.gif" width="323" height="500"&gt; &lt;/p&gt; &lt;p&gt;值得注意的是Generate BSP會重新根據*.sopcinfo去產生drivers /HAL，也就是說若你在Qsys移除了某個IP，Generate BSP會自動幫你刪除drivers / HAL相關的檔案，且你之前若手動對drivers / HAL的C code做了修改，Generate BSP也會重新覆蓋過去，所以不建議手動去修改drivers / HAL的C code，應該由Generate BSP自動去產生即可。&lt;/p&gt; &lt;p&gt;&lt;strong&gt;BSP Editor&lt;/strong&gt;：修改settings.bsp的設定，主要是偏韌體方面的設定。&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Clean Project&lt;/strong&gt;：相當於make clean，會將所有的object files刪除，由於若你在Qsys刪除某個IP，在Generate BSP會刪除該IP的drivers / HAL檔案，卻不會刪除該IP的object files，因此必須手動用Clean Project刪除。&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Build Project&lt;/strong&gt;：相當於make all，會重新根據Makefile編譯所有檔案。&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Conclusion&lt;br&gt;&lt;/strong&gt;大部分的人都知道最後是Clean Project與Build Project，這點問題不大，但Generate BSP與BSP Editor該誰先執行呢?&lt;strong&gt;&lt;font color="#008080"&gt;答案是應先執行Generate BSP再執行BSP Editor&lt;/font&gt;&lt;/strong&gt;，因為若先執行BSP Editor，你所做的設定是根據舊*.sopcinfo的settings.bsp所做的設定，並無法反映出最新的硬體，所以必須先執行Generate BSP後，先產生出反應最新硬體的settings.bsp，然後再執行BSP Editor。&lt;/p&gt; &lt;p&gt;總結其流程如下：&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="revise006" border="0" alt="revise006" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201042258147877.gif" width="552" height="482"&gt;&amp;nbsp;&amp;nbsp; &lt;/p&gt; &lt;p&gt;&lt;strong&gt;See Also&lt;br&gt;&lt;/strong&gt;&lt;a href="http://www.cnblogs.com/oomusou/archive/2011/12/21/nios2_project_moved.html"&gt;(原創) 如何解決目錄改變時，Nios II project無法編譯的問題? (SOC) (Nios II) (DE2-70)&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Reference&lt;/strong&gt;&lt;br&gt;[1] &lt;a href="http://www.altera.com/literature/hb/nios2/n2sw_nii52015.pdf" target="_blank"&gt;Nios II Software Build Tools&lt;/a&gt;&lt;br&gt;[2] &lt;a href="http://www.altera.com/literature/hb/nios2/n2sw_nii52016.pdf" target="_blank"&gt;Nios II Software Build Tools Reference&lt;/a&gt;&lt;/p&gt; &lt;p&gt;全文完。&lt;/p&gt;&lt;img src="http://www.cnblogs.com/oomusou/aggbug/2311309.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/oomusou/archive/2012/01/03/revising_bsp.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/oomusou/archive/2011/12/21/nios2_project_moved.html</id><title type="text">(原創) 如何解決目錄改變時，Nios II project無法編譯的問題? (SOC) (Nios II) (DE2-70)</title><summary type="text">若我們從網路上下載範例程式，或者從書上的光碟將範例程式複製到硬碟時，只要是Quartus II版本正確，都可以正常地開啟Quartus II project並且編譯之，但Nios II project卻常常雖然能開啟，卻無法正常編譯，本文討論其root cause並提出解決方式。</summary><published>2011-12-21T13:48:00Z</published><updated>2011-12-21T13:48:00Z</updated><author><name>真 OO无双</name><uri>http://www.cnblogs.com/oomusou/</uri></author><link rel="alternate" href="http://www.cnblogs.com/oomusou/archive/2011/12/21/nios2_project_moved.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/oomusou/archive/2011/12/21/nios2_project_moved.html"/><content type="html">&lt;p&gt;&lt;strong&gt;Abstract&lt;/strong&gt;&lt;br&gt;若我們從網路上下載範例程式，或者從書上的光碟將範例程式複製到硬碟時，只要是Quartus II版本正確，都可以正常地開啟Quartus II project並且編譯之，但Nios II project卻常常雖然能開啟，卻無法正常編譯，本文討論其root cause並提出解決方式。&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Introduction&lt;br&gt;使用環境：Windows XP SP3 + VirtualBox 4.1.2 + Quartus II 11.0&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;本文將討論以下主題：&lt;/p&gt; &lt;p&gt;1.為什麼會需要改變目錄名稱或目錄位置?&lt;/p&gt; &lt;p&gt;2.改變目錄名稱或目錄位置，在Nios II project會遇到什麼問題?&lt;/p&gt; &lt;p&gt;3.Root cause與解決方法。&lt;/p&gt; &lt;p&gt;4.使用Blank Project方法所面臨的問題。&lt;/p&gt; &lt;p&gt;&lt;strong&gt;1.為什麼會需要改變目錄名稱或目錄位置?&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;我們常會有各種理由會改變原來project的目錄名稱或目錄位置：&lt;/p&gt; &lt;p&gt;1.為了管理方便，可能將原來在d:\project\的所有project移到e:\project\下&lt;/p&gt; &lt;p&gt;2.同事將project整個目錄壓縮給我，因為我並不知道該project放在同事電腦什麼工作目錄下，所以我將壓縮檔解壓縮到我自己的工作目錄下&lt;/p&gt; &lt;p&gt;3.從網路上下載整包範例程式的壓縮檔後，因為我並不知道原本範例程式所存放的目錄，所以我將壓縮檔解壓縮到我自己的工作目錄下&lt;/p&gt; &lt;p&gt;4.從書上光碟複製範例程式到硬碟，因為我並不知道原本範例程式所存放的目錄，所以我將範例程式複製到我自己的工作目錄下&lt;/p&gt; &lt;p&gt;5.新的project與舊的project類似，想從舊的project去做修改即可，開了一個新的目錄，將舊的project所有檔案複製到新的目錄下&lt;/p&gt; &lt;p&gt;6.為了管理方便，想改變原本project的目錄名稱&lt;/p&gt; &lt;p&gt;以上的情形，若是純粹Quartus II project，只要Quartus II版本正確，開啟*.qpf檔即可順利開啟，並且正常編譯，唯一有問題的是Programmer的*.cdf檔可能因為路徑不對無法寫入，只是重新指定*.sof與*.pof的位置即可。&lt;/p&gt; &lt;p&gt;Quartus II的*.qpf project概念類似Visual C++ 6的*.dsw或者Visual Studio的*.sln，整個project內的檔案是相對路徑，所以改變專案名稱或者改變目錄位置都沒有關係，只要檔案相對位置沒有改變，整個project就可以正常運作。&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;span style="color: #008080"&gt;但是Nios II project就沒這麼單純了!!&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Nios II SBT是用Eclipse去改的，用的是Eclipse的workspace概念，很類似Visual Studio的*.sln概念，但又不完全一樣。Eclipse允許你在一個workspace下，去管理多個project，workspace記住的是project的&lt;strong&gt;&lt;span style="color: #008080"&gt;絕對路徑&lt;/span&gt;&lt;/strong&gt;，所以當你Nios II project目錄名稱改變，或者目錄位置改變，該workspace自然就找不到了。&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;span style="color: #008080"&gt;依照Eclipse workspace哲學的正規解法，此時你應該將目錄改變的Nios II project重新Import到你的workspace下&lt;/span&gt;&lt;/strong&gt;。但事實上真的如此嗎?&lt;/p&gt; &lt;p&gt;&lt;strong&gt;2.改變目錄名稱或目錄位置，在Nios II project會遇到什麼問題?&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;我們實際做個實驗，如下圖所示，原本&lt;strong&gt;&lt;span style="color: #008080"&gt;DE2_70_SOPC_golden_mini&lt;/span&gt;&lt;/strong&gt;是一個在Quartus II與Nios II SBT都完全正常的project。&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_00" border="0" alt="nios2_path_00" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112272230338713.gif" width="807" height="620"&gt;&lt;/p&gt; &lt;p&gt;我們現在將目錄名稱改變，從&lt;strong&gt;&lt;span style="color: #008080"&gt;DE2_70_SOPC_golden_mini&lt;/span&gt;&lt;/strong&gt;改成&lt;strong&gt;&lt;span style="color: #008080"&gt;DE2_70_SOPC_golden_mini2&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_01" border="0" alt="nios2_path_01" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112272230386667.gif" width="807" height="620"&gt;&lt;/p&gt; &lt;p&gt;大家都知到改變目錄名稱或目錄位置不會影響Quartus II project的開啟與編譯，所以就略過不討論，現在將焦點放在Nois II project部分。&lt;/p&gt; &lt;p&gt;開啟Nios II SBT，將workspace目錄切換到新的目錄&lt;strong&gt;&lt;span style="color: #008080"&gt;DE2_70_SOPC_golden_mini2&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_02" border="0" alt="nios2_path_02" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112272315362919.gif" width="613" height="227"&gt;&lt;/p&gt; &lt;p&gt;開啟後顯示更改目錄名稱之前的2個project：&lt;strong&gt;&lt;span style="color: #008080"&gt;hello_world&lt;/span&gt;&lt;/strong&gt;與&lt;span style="color: #008080"&gt;&lt;strong&gt;hello_world_bsp&lt;/strong&gt;&lt;/span&gt;，由於現在目錄名稱已經改變，所以只能看到project名稱，卻完全無法開啟。&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_03" border="0" alt="nios2_path_03" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112272315472896.gif" width="807" height="620"&gt;&lt;/p&gt; &lt;p&gt;由於2個project已經無法開啟，我們將這2個project從workspace中刪除之。&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_04" border="0" alt="nios2_path_04" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/20111227231552459.gif" width="807" height="620"&gt;&lt;/p&gt; &lt;p&gt;刪除完後，整個workspace完全沒有任何project。&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_05" border="0" alt="nios2_path_05" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112272315564759.gif" width="807" height="620"&gt;&lt;/p&gt; &lt;p&gt;根據Eclipse哲學的&lt;strong&gt;&lt;span style="color: #008040"&gt;&lt;span style="color: #008080"&gt;正規解法&lt;/span&gt;&lt;/span&gt;&lt;/strong&gt;：&lt;strong&gt;&lt;span style="color: #008080"&gt;因為目錄已經改變，我們必須將2個project使用import的方式載入到新的workspace。&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_06" border="0" alt="nios2_path_06" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112272316067485.gif" width="807" height="620"&gt;&lt;/p&gt; &lt;p&gt;由於之前是使用Nios II SBT產生makefile的project，所以在此選擇&lt;strong&gt;&lt;span style="color: #008080"&gt;Import Nios II Software Build Tools Project&lt;/span&gt;&lt;/strong&gt;。&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_07" border="0" alt="nios2_path_07" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112272316111012.gif" width="525" height="547"&gt;&lt;/p&gt; &lt;p&gt;首先將hello_world project import進來，值得注意的是：要將&lt;strong&gt;&lt;span style="color: #008080"&gt;Clean project when importing&lt;/span&gt;&lt;/strong&gt;打勾，也就是在import project完時，馬上執行make clean，將之前所留下的object file全部清除。&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_08" border="0" alt="nios2_path_08" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112272316128112.gif" width="548" height="549"&gt;&lt;/p&gt; &lt;p&gt;import完成後，顯示了以下的warning，告訴我們project想include舊的專案路徑的目錄，但因為目錄名稱改變，所以include失敗。這是第1個問題。&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_09" border="0" alt="nios2_path_09" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112272316182610.gif" width="807" height="620"&gt;&lt;/p&gt; &lt;p&gt;接下繼續import hello_world_bsp，也記得將&lt;span style="color: #008080"&gt;&lt;strong&gt;Clean project when importing&lt;/strong&gt;&lt;/span&gt;打勾。&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_10" border="0" alt="nios2_path_10" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112272316196679.gif" width="640" height="550"&gt;&lt;/p&gt; &lt;p&gt;import完成後，並沒有顯示任何錯誤訊息與warning。&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_11" border="0" alt="nios2_path_11" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112272316327717.gif" width="807" height="620"&gt;&lt;/p&gt; &lt;p&gt;若在hello_world_bsp執行Nios II –&amp;gt; Generate BSP，會出現以下錯誤訊息，表示找不到*.sopcinfo。這是第2個問題。&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_12" border="0" alt="nios2_path_12" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112272316387298.gif" width="370" height="317"&gt;&lt;/p&gt; &lt;p&gt;若在hello_world_bsp執行Nios II –&amp;gt; BSP Editor會出現以下錯誤訊息，表示找不到*.sopcinfo。這是第3個問題。&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_13" border="0" alt="nios2_path_13" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112272316439712.gif" width="807" height="620"&gt;&lt;/p&gt; &lt;p&gt;若實際Build App project(hello_world)與BSP project，雖然仍可出現*.elf與*.a，但會出現以下錯誤訊息，表示Makefile找不到*.sopcinfo。這是第4個問題。&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_005" border="0" alt="nios2_path_005" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011314156355.gif" width="807" height="620"&gt;&lt;/p&gt; &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_006" border="0" alt="nios2_path_006" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011314209415.gif" width="807" height="620"&gt;&lt;/p&gt; &lt;p&gt;總結以上實驗，我們發現若改變目錄名稱或者目錄位置，在Nios II project會出現以下4個問題：&lt;/p&gt; &lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;1.App project(hello_world)會出現include path not found warning。&lt;br&gt;2.BSP project(hello_world_bsp)會無法執行Generate BSP。&lt;br&gt;3.BSP project(hello_world_bsp)會無法執行BSP Editor。&lt;br&gt;4.Build App project(hello_world)與BSP project(hello_world_bsp)會出現Makefile找不到SOPC File的warning。&lt;/div&gt;&lt;p&gt;&lt;strong&gt;3.Root cause與解決方法。&lt;/strong&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;App project (hello_world) ：include path not found warning的Root cause&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;在hello_world的Properties的C/C++ General –&amp;gt; Paths and Symbols的GNC C與GNC C++，我們可以發現Include directories的目錄錯了，抓的都是修改前的目錄名稱，且這些目錄名稱還無法刪除或者修改，因為這是Eclipse自動抓的，這是第1個問題的root cause。&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_003" border="0" alt="nios2_path_003" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011314211824.gif" width="1010" height="543"&gt;&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_004" border="0" alt="nios2_path_004" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011314492789.gif" width="1010" height="543"&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;App project (hello_world) ：include path not found warning的Solution&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Step 1：&lt;/strong&gt;&lt;strong&gt;App project (hello_world)的properties&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_007" border="0" alt="nios2_path_007" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011341162351.gif" width="808" height="776"&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Step 2：C/C++ Build –&amp;gt; Discovery Options的Cygwin C Compier，按下Clear清除目前所抓的include path。&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_008" border="0" alt="nios2_path_008" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011341251327.gif" width="1010" height="650"&gt;&lt;/p&gt;&lt;p&gt;提示include path即將清除，將在Build時重新抓取include path，按『OK』繼續。&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_009" border="0" alt="nios2_path_009" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011341261751.gif" width="522" height="131"&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Step 4：C/C++ Build –&amp;gt; Discovery Options的Cygwin C++ Compier，按下Clear清除目前所抓的include path。&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_010" border="0" alt="nios2_path_010" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011341303830.gif" width="1010" height="650"&gt; &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;提示include path即將清除，將在Build時重新抓取include path，按『OK』繼續。&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_011" border="0" alt="nios2_path_011" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011341319097.gif" width="522" height="131"&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Step 5：檢查目前App project (hello_world)的include path&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;C/C++ General –&amp;gt; Paths and Symbols的Includes的GNC C，確定已經清除所有的include paths&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_012" border="0" alt="nios2_path_012" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011341412118.gif" width="1010" height="650"&gt;&lt;/p&gt;&lt;p&gt;C/C++ General –&amp;gt; Paths and Symbols的Includes的GNC C++，確定已經清除所有的include paths&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_013" border="0" alt="nios2_path_013" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011341428988.gif" width="1010" height="650"&gt;&lt;/p&gt;&lt;p&gt;原本App project (hello_world)的warnings也都不見了。&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_014" border="0" alt="nios2_path_014" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/20120101134143317.gif" width="807" height="620"&gt;&lt;/p&gt;&lt;p&gt;看到這裡，或許你會說：『不是應該要include新目錄的BSP project路徑才對嗎?』沒錯，如同Step 3與Step 4的提示所言，最後只要重新Build App project (hello_world)，就會重新discover include path，這我們等BSP project也解決後再一起重新Build。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;BSP project (hello_world_bsp)無法Generate BSP的Root cause&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;首先了解Nios II SBT的Generate BSP到底做了什麼事情，根據[3] &lt;a href="http://www.altera.com/literature/hb/nios2/n2sw_nii52016.pdf" target="_blank"&gt;Nios II Software Build Tools Reference&lt;/a&gt;的p.15-8的&lt;strong&gt;&lt;span style="color: #008080"&gt;nios2-bsp-generate-files&lt;/span&gt;&lt;/strong&gt;與[4] &lt;a href="http://www.altera.com/literature/hb/nios2/n2sw_nii52015.pdf" target="_blank"&gt;Nios II Software Build Tools&lt;/a&gt;的p.4-30的&lt;strong&gt;&lt;span style="color: #008080"&gt;Regenerating Your BSP&lt;/span&gt;&lt;/strong&gt;所述，Generate BSP會根據BSP project的settings.bsp去尋找*.sopcinfo，再根據目前的*.sopcinfo去產生最新的drivers與HAL目錄所需要的檔案以及重新產生Makefile。&lt;/p&gt;&lt;p&gt;打開settings.bsp，會發現BspGeneratedLocation與SopcDesignFile所記錄的路徑都是改變目錄名稱之前的路徑，因此在Generate BSP時會找不到*.sopcinfo而導致執行錯誤。這是第2個問題的root cause。&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_015" border="0" alt="nios2_path_015" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/20120101144038265.gif" width="807" height="620"&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;BSP project (hello_world_bsp)無法Generate BSP的Solution&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Step 1：修改BspGeneratedLocation與SopcDesignFile的路徑&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_016" border="0" alt="nios2_path_016" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011440495762.gif" width="807" height="620"&gt;&lt;/p&gt;&lt;p&gt;修改完存檔時會出現以下錯誤訊息，主要是BspGeneratedTimStamp使用了中文時間，按下『Save as UTF-8』格式存檔即可。&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_26" border="0" alt="nios2_path_26" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112300011154126.gif" width="541" height="178"&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Step 2：重新Generate BSP&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;可順利執行Generate BSP沒有任何錯誤訊息。&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_018" border="0" alt="nios2_path_018" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011441004606.gif" width="806" height="619"&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;BSP project (hello_world_bsp)無法執行BSP Editor的Root cause&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;BSP Editor主要的目的在於修改settings.bsp一些只與BSP project相關的設定，BSP Editor無法執行，主要原因也是因為找不到*.sopcinfo。這是第3個問題的root cause。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;BSP project (hello_world_bsp)無法執行BSP Editor的Solution&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;在之前的步驟已經修改過settings.bsp的*.sopcinfo路徑，所以也一併解決了這個問題，不必再做其他修改。&lt;/p&gt;&lt;p&gt;重新執行BSP Editor，可正常執行沒有任何錯誤訊息。&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_019" border="0" alt="nios2_path_019" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011441017016.gif" width="807" height="618"&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Build App project (hello_world)與BSP project (hello_world_bsp)會出現Makefile找不到SOPC File的warning的Root cause&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;App project (hello_world)與BSP project (hello_world_bsp)在Build時，事實上就是執行Make動作，所以需要參考Makefile，會出現warning主要是因為Makefile找不到*.sopcinfo。這是第4個問題的root cause。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Build App project (hello_world)與BSP project (hello_world_bsp)會出現Makefile找不到SOPC File的warning的Solution&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;理論上應該要去修改Makefile，不過由於執行Generate BSP時，&lt;strong&gt;&lt;span style="color: #008080"&gt;nios2-bsp-generate-files&lt;/span&gt;&lt;/strong&gt;已經根據修改過的settings.bsp更新過Makefile，所以我們不須再手動修改Makefile了。&lt;/p&gt;&lt;p&gt;重新Build BSP project (hello_world_bsp)，可順利Build沒有任何錯誤訊息。&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_020" border="0" alt="nios2_path_020" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011441132663.gif" width="807" height="620"&gt;&lt;/p&gt;&lt;p&gt;最後重新Build App project (hello_world)，也可順利Build沒有任何錯誤訊息。&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_021" border="0" alt="nios2_path_021" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011441177217.gif" width="807" height="620"&gt;&lt;/p&gt;&lt;p&gt;還記得在App project (hello_world) ：include path not found warning的Solution時，我們只清除了App project所include的錯誤路徑，但卻還沒有將正確地修正include路徑。&lt;/p&gt;&lt;p&gt;在Build完後App project (hello_world)後，馬上觀察hello_world的Properties的C/C++ General –&amp;gt; Paths and Symbols的GNC C與GNC C++，會發現正確的include路徑都回來了。&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_022" border="0" alt="nios2_path_022" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011441181786.gif" width="1010" height="650"&gt;&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_023" border="0" alt="nios2_path_023" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011441229721.gif" width="1010" height="650"&gt;&lt;/p&gt;&lt;p&gt;之前的洋洋灑灑，只是因為要邊解釋root cause邊介紹solution，其實整個步驟很簡單，只要5個步驟即可。&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_36" border="0" alt="nios2_path_36" src="http://images.cnblogs.com/cnblogs_com/oomusou/201201/201201011526311017.gif" width="674" height="482"&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;4.使用Blank Project方法所面臨的問題&lt;/strong&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;Nios II project只要改變路徑就無法編譯的問題，其實從我使用Quartus II 6.0時就已經發現了，應該說Nios II要使用Eclipse，就注定會有這個workspace的問題，很多人的解法(包括我自己)都與[2] &lt;a href="http://www.cnblogs.com/yuphone/" target="_blank"&gt;张亚峰&lt;/a&gt;的&lt;a href="http://www.cnblogs.com/yuphone/archive/2011/10/24/2222207.html"&gt;[笔记].为何在Nios II SBTE中，直接拖放到工程文件夹的文件，编译会出错？&lt;/a&gt;一樣，都是重新開1個blank project，然後重新將所需要的*.c, *.cpp拖放進新建的blank project，最後在手動去修改Makefile，這樣的解法的確是可以避開include paths錯誤與*.sopcinfo路徑錯誤的問題，不過settings.bsp與Makefile都是新的，所以必須手動去檢查Makefile是否與原project一樣，然後手動修改Makefile。&lt;/p&gt;&lt;p&gt;在[1] &lt;a href="http://www.altera.com/literature/hb/nios2/n2sw_nii52017.pdf" target="_blank"&gt;Getting Started with the Graphical User Interface&lt;/a&gt;的p.2-11的User Source Management有以下一段文字：&lt;/p&gt;&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="nios2_path_35" border="0" alt="nios2_path_35" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112300012068179.gif" width="552" height="59"&gt;&lt;/p&gt;&lt;p&gt;簡單的說，就是既然你用了Nios II SBT，就不建議手動去改Makefile，應該採用GUI或者Command line script的方式，由tools去更改你的Makefile，而不該手動去更改你的Makefile。本文採用的方式，雖然知道Makefile有問題，但依照Nios II SBT的邏輯，使用了Nios II SBT的GUI方式與流程去修改Makefile，完全沒有手動去修改Makefile，這樣可確定Makefile的正確性，也省去了一一比對原本Makefile的功夫。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;完整程式碼下載&lt;/strong&gt;&lt;br&gt;&lt;a href="http://files.cnblogs.com/oomusou/DE2_70_SOPC_golden_mini2.7z"&gt;DE2_70_SOPC_golden_mini2.7z&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Conclusion&lt;br&gt;&lt;/strong&gt;這篇博文中的解法，是參考了Altera的官方資料所做出的總結，並經過無數次的實驗所歸納的心得， 因為這是我困擾好幾年的問題，假如你也為Nios II project路徑問題而困擾，可以參考本文的解法。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;See Also&lt;br&gt;&lt;/strong&gt;&lt;a href="http://www.cnblogs.com/oomusou/archive/2012/01/03/revising_bsp.html"&gt;(原創) Qsys或RTL做修改後，Nios II SBT該如何面對新的硬體? (SOC) (Nios II) (Qsys)&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Reference&lt;/strong&gt;&lt;br&gt;[1] &lt;a href="http://www.altera.com/literature/hb/nios2/n2sw_nii52017.pdf" target="_blank"&gt;Getting Started with the Graphical User Interface&lt;/a&gt;&lt;br&gt;[2] &lt;a href="http://www.cnblogs.com/yuphone/" target="_blank"&gt;张亚峰&lt;/a&gt;的&lt;a href="http://www.cnblogs.com/yuphone/archive/2011/10/24/2222207.html"&gt;[笔记].为何在Nios II SBTE中，直接拖放到工程文件夹的文件，编译会出错？&lt;/a&gt;&lt;br&gt;[3] &lt;a href="http://www.altera.com/literature/hb/nios2/n2sw_nii52016.pdf" target="_blank"&gt;Nios II Software Build Tools Reference&lt;/a&gt;&lt;br&gt;[4] &lt;a href="http://www.altera.com/literature/hb/nios2/n2sw_nii52015.pdf" target="_blank"&gt;Nios II Software Build Tools&lt;/a&gt;&lt;/p&gt;&lt;p&gt;全文完。&lt;/p&gt;&lt;img src="http://www.cnblogs.com/oomusou/aggbug/2296317.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/oomusou/archive/2011/12/21/nios2_project_moved.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/oomusou/archive/2011/12/20/nios2_qsys_generate.html</id><title type="text">(原創) Qsys Generation Tab的Simulation設定的意義 (SOC) (Nios II) (Qsys)</title><summary type="text">若要對含有Nios II的SOPC系統進行simulation，在Qsys的Generation Tab有新的設定，本文討論其設定的意義。 </summary><published>2011-12-20T15:38:00Z</published><updated>2011-12-20T15:38:00Z</updated><author><name>真 OO无双</name><uri>http://www.cnblogs.com/oomusou/</uri></author><link rel="alternate" href="http://www.cnblogs.com/oomusou/archive/2011/12/20/nios2_qsys_generate.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/oomusou/archive/2011/12/20/nios2_qsys_generate.html"/><content type="html">&lt;p&gt;&lt;strong&gt;Abstract&lt;/strong&gt;&lt;br /&gt;若要對含有Nios II的SOPC系統進行simulation，在Qsys的Generation Tab有新的設定，本文討論其設定的意義。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Introduction&lt;br /&gt;使用環境：Windows XP SP3 + VirtualBox 4.1.2 + Quartus II 11.0&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;在Qsys的Generation tab的Simulation部分，有以下的設定：&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="nios2_sim00" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/2011122023381078.gif" alt="nios2_sim00" width="581" height="503" border="0" /&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;根據[1] &lt;strong&gt;Quartus II Handbook 11.0 Volumn 1：Section II Chapter 5&lt;/strong&gt;的&lt;strong&gt;P.5-12&lt;/strong&gt;，對各選項的設定解釋如下：&lt;/p&gt;&lt;p&gt;&lt;img src="http://pic002.cnblogs.com/images/2011/17820/2011122110360828.gif" alt="" /&gt;&lt;/p&gt;&lt;p&gt;我個人是對以上的解釋有看沒有懂，以下是我實驗後的總結：&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Create simulation model&lt;/strong&gt;：Qsys會產生simulation model與testbench，但不會產生命名為xxx_tb.sys的testbench Qsys system，這種就類似FPGA不使用SOPC時，在top module自己將所有module手動接起來一樣。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Create testbench Qsys system&lt;/strong&gt;：Qsys會幫你產生命名為xxx_tb.sys的testbench Qsys system，而這個testbench Qsys system就相當於simulation的top module。值得注意的是：這個選項只會幫你建立1個testbench Qsys system，並不會建立testbench與simulation model。&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;Standard, BFMs for standard Avalon interfaces&lt;/strong&gt;：所建立的testbench Qsys system會掛上所有的simulation model，包含各種記憶體與其他周邊，這是最完整的。&lt;/p&gt;&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;Simple, BFMs for clocks and resets&lt;/strong&gt;：根據Altera的官方解釋，看起來所建立的testbench Qsys system只會掛上clocks與reset simulation model，&lt;strong&gt;&lt;span style="color: #008080;"&gt;事實上還會掛上所有記憶體的simulation model&lt;/span&gt;&lt;/strong&gt;，其實這是合理的，因為Nios II要跑C code，一定要有記憶體才能跑，而且在Nios II SBT的Run As ModelSim時，會根據Linker Script的定義，產生要載入各種記憶體的*.hex或*.dat然後載入到各記憶體的simulation model。值得注意的是：&lt;strong&gt;&lt;span style="color: #008080;"&gt;這個選項不會掛上記憶體以外周邊的simulation model&lt;/span&gt;&lt;/strong&gt;，所以Qsys在Generate時會快一些。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Create testbench simulation model：&lt;/strong&gt;此選項會根據之前所產生的testbench Qsys system去產生testbench與simulation model。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br /&gt;實務上該如何設定這些選項呢?根據[2] &lt;a href="http://www.altera.com/support/examples/nios2/exm-simulating-niosii.html" target="_blank"&gt;Simulating Nios II Embedded Processor Designs&lt;/a&gt;，建議使用以下設定：&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;span style="color: #008080;"&gt;Create simulation model：None&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;span style="color: #008080;"&gt;Create testbench Qsys system：Standard, BFMs for standard Avalon interfaces&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;span style="color: #008080;"&gt;Create testbench simulation model：Verilog&lt;/span&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;理由是既然選擇使用testbench Qsys system，就不用再選擇使用『Create simulation model』，而且Standard, BFMs for standard Avalon interfaces可以產生最完整的testbench與simulation model，包含系統所有的記憶體與周邊。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Reference&lt;/strong&gt;&lt;br /&gt;[1] Quartus II Handbook 11.0 Volumn 1：Section II Chapter 5的P.5-12&lt;br /&gt;[2] &lt;a href="http://www.altera.com/support/examples/nios2/exm-simulating-niosii.html" target="_blank"&gt;Simulating Nios II Embedded Processor Designs&lt;/a&gt;&lt;/p&gt;&lt;p&gt;全文完。&lt;/p&gt;&lt;img src="http://www.cnblogs.com/oomusou/aggbug/2295287.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/oomusou/archive/2011/12/20/nios2_qsys_generate.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/oomusou/archive/2011/12/08/nios2_spi.html</id><title type="text">(原創) 如何在Nios II使用SPI Core? (SOC) (Nios II) (Qsys) (DE2-70)</title><summary type="text">SPI為IC之間溝通中常見的介面，Qsys已經提供SPI Core，Nios II可以直接使用，本文詳細探討SPI core的使用方式以及實務上該如何開發使用SPI介面的韌體。</summary><published>2011-12-08T14:04:00Z</published><updated>2011-12-08T14:04:00Z</updated><author><name>真 OO无双</name><uri>http://www.cnblogs.com/oomusou/</uri></author><link rel="alternate" href="http://www.cnblogs.com/oomusou/archive/2011/12/08/nios2_spi.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/oomusou/archive/2011/12/08/nios2_spi.html"/><content type="html">&lt;p&gt;&lt;strong&gt;Abstract&lt;/strong&gt;&lt;br /&gt;SPI為IC之間溝通中常見的介面，Qsys已經提供SPI Core，Nios II可以直接使用，本文詳細探討SPI core的使用方式以及實務上該如何開發使用SPI介面的韌體。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Introduction&lt;br /&gt;使用環境：Windows XP SP3 + VirtualBox 4.1.2 + Quartus II 11.0 +&amp;nbsp; + DE2-70&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;實務上IC最常見的3大通訊介面：I2C、SPI與UART，其中Qsys已經內建SPI core，Nios II可以直接使用。&lt;/p&gt;&lt;p&gt;本文將討論以下主題：&lt;/p&gt;&lt;p&gt;1.SPI Bus簡介&lt;/p&gt;&lt;p&gt;2.Qsys的SPI Core簡介&lt;/p&gt;&lt;p&gt;3.詳細探討alt_avalon_spi_command()的內部運作機制&lt;/p&gt;&lt;p&gt;4.實際使用SPI Core存取SPI Flash&lt;/p&gt;&lt;p&gt;5.實際開發存取SPI Flash的SDK&lt;/p&gt;&lt;p&gt;&lt;strong&gt;1.SPI Bus簡介&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;在DE2-70平台上的IC，唯有TRDB-LTM這個Touch Panel的觸控IC使用SPI將觸控的座標傳回，使用上較為複雜，若初學者只想單純的學習SPI，恐怕會模糊焦點，因此我特地找了一顆使用SPI的Flash，透過GPIO與DE2-70連接，如此就可在Nios II使用SPI core對Flash的register做read/write，也能實際對Flash做讀寫。&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="spi000" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112090803532212.gif" alt="spi000" width="585" height="388" border="0" /&gt;&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border-width: 0px;" title="spi001" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112090804154033.gif" alt="spi001" width="585" height="403" border="0" /&gt;&lt;/p&gt;&lt;p&gt;SPI為&lt;strong&gt;&lt;span style="color: #008080;"&gt;Serial Peripheral Interface&lt;/span&gt;&lt;/strong&gt;縮寫，為Motorola所制定的標準，總共只有4根1 bit的信號，如下圖所示：&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border: 0px;" title="spi002" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112181616401092.gif" alt="spi002" width="572" height="191" border="0" /&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;SCLK：為&lt;strong&gt;&lt;span style="color: #008080;"&gt;Serial Clock&lt;/span&gt;&lt;/strong&gt;縮寫，由master所產生的clock給slave使用。&lt;/p&gt;&lt;p&gt;MOSI：為&lt;strong&gt;&lt;span style="color: #008080;"&gt;Master Output Slave Input&lt;/span&gt;&lt;/strong&gt;縮寫，由master output至slave input。&lt;/p&gt;&lt;p&gt;MISO：為&lt;strong&gt;&lt;span style="color: #008080;"&gt;Master Input Slave Output&lt;/span&gt;&lt;/strong&gt;縮寫，由slave output至master input。&lt;/p&gt;&lt;p&gt;SS_N：為&lt;strong&gt;&lt;span style="color: #008080;"&gt;Slave Select&lt;/span&gt;&lt;/strong&gt;縮寫，由master告知slave是否enable。(active low)。&lt;/p&gt;&lt;p&gt;在多SPI slave的環境下，會以如下圖的方式連接：&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border: 0px;" title="spi003" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112181635406015.gif" alt="spi003" width="581" height="414" border="0" /&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;其中SPI master會有n個SS_N port對應SPI slave 0, SPI slave 1&amp;hellip;的SS_N，當該SPI slave的SS_N為low時，表示master選定了該slave。&lt;/p&gt;&lt;p&gt;其中較特殊的是master與slave的MISO port連接時，須加上一個tristate buffer，使該slave沒有被master選取時，MISO為Hi-Z，如此才能避免兩個slave同時drive一個MISO。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Clock polarity and phase&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;SPI根據其clock polarity與clock phase的排列組合，總共有以下4種可能：&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Clock Polarity = 0, Clock Phase = 0&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border: 0px;" title="spi004" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112181715413167.gif" alt="spi004" width="584" height="162" border="0" /&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;1.SCLK在idle時為low。&lt;/p&gt;&lt;p&gt;2.MOSI在SCLK的rising edge時被latch，在SCLK的falling edge時做transition。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Clock Polarity = 0, Clock Phase = 1&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border: 0px;" title="spi005" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112181733365300.gif" alt="spi005" width="587" height="165" border="0" /&gt;&lt;/p&gt;&lt;p&gt;1.SCLK在idle時為low。&lt;/p&gt;&lt;p&gt;2.MOSI在SCLK的falling edge時被latch，在SCLK的rising edge時做transition。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Clock Polarity = 1, Clock Phase = 0&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;&lt;img style="display: inline; border: 0px;" title="spi006" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112181733365890.gif" alt="spi006" width="587" height="165" border="0" /&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;1.SCLK在idle時為high。&lt;/p&gt;&lt;p&gt;2.MOSI在SCLK的falling edge時被latch，在SCLK的rising edge時做transition。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Clock Polarity = 1, Clock Phase = 1&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border: 0px;" title="spi007" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/20111218173337939.gif" alt="spi007" width="587" height="165" border="0" /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;1.SCLK在idle時為high。&lt;/p&gt;&lt;p&gt;2.MOSI在SCLK的rising edge時被latch，在SCLK的falling edge時做transition。&lt;/p&gt;&lt;p&gt;看到這裡，或許你會問：『在實務上，我們是如何利用SPI介面與其他IC做溝通呢?』&lt;/p&gt;&lt;p&gt;我們以[3] &lt;a href="http://www.winbond.com/NR/rdonlyres/AEC6E481-6616-4555-B339-6E79D17810AC/0/W25X10BV_W25X20BV_W25X40BV.pdf" target="_blank"&gt;Winbond SPI Flash datasheet&lt;/a&gt;的Read Status Register為例：&lt;/p&gt;&lt;p&gt;&lt;img style="display: inline; border: 0px;" title="spi008" src="http://images.cnblogs.com/cnblogs_com/oomusou/201112/201112182218226973.gif" alt="spi008" width="621" height="238" border="0" /&gt;&lt;/p&gt;&lt;p&gt;其中DIO就是MOSI，DO就是MISO(naming convention不同而已)，在前8個clk由master從MOSI送出instruction：05h，在下8個clk由slave從MISO傳回status register的值，由於status register可以連續的讀取，因此MISO可以連續的傳回status register值。&lt;/p&gt;&lt;p&gt;其中Mode 0就相當於clock polarity = 0，clock phase = 0，而Mode 3就相當於clock polarity = 1，clock phase = 1。&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Reference&lt;/strong&gt;&lt;br /&gt;[1] &lt;a href="http://www.altera.com/literature/ug/ug_embedded_ip.pdf" target="_blank"&gt;Embedded Peripherals IP User Guide&lt;/a&gt;&lt;br /&gt;[2] &lt;a href="http://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus" target="_blank"&gt;Serial Peripheral Interface Bus Wiki&lt;/a&gt;&lt;br /&gt;[3] &lt;a href="http://www.winbond.com/NR/rdonlyres/AEC6E481-6616-4555-B339-6E79D17810AC/0/W25X10BV_W25X20BV_W25X40BV.pdf" target="_blank"&gt;Winbond SPI Flash datasheet&lt;/a&gt;&lt;/p&gt;&lt;p&gt;未完，待續。&lt;/p&gt;&lt;img src="http://www.cnblogs.com/oomusou/aggbug/2281314.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/oomusou/archive/2011/12/08/nios2_spi.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry><entry><id>http://www.cnblogs.com/oomusou/archive/2011/10/31/write_n_byte.html</id><title type="text">(筆記) 如何寫入binary file某個byte連續n byte的值? (C/C++) (C)</title><summary type="text">通常公司為了保護其智慧財產權，會自己定義檔案格式，其header區會定義每個byte各代表某項資訊，所以常常需要直接對binary檔的某byte直接進行寫入，且連續寫入幾個byte表示某一數值資訊。</summary><published>2011-10-31T15:56:00Z</published><updated>2011-10-31T15:56:00Z</updated><author><name>真 OO无双</name><uri>http://www.cnblogs.com/oomusou/</uri></author><link rel="alternate" href="http://www.cnblogs.com/oomusou/archive/2011/10/31/write_n_byte.html"/><link rel="alternate" type="text/html" href="http://www.cnblogs.com/oomusou/archive/2011/10/31/write_n_byte.html"/><content type="html">&lt;p&gt;&lt;strong&gt;Abstract&lt;/strong&gt;&lt;br /&gt;通常公司為了保護其智慧財產權，會自己定義檔案格式，其header區會定義每個byte各代表某項資訊，所以常常需要直接對binary檔的某byte直接進行寫入，且連續寫入幾個byte表示某一數值資訊。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Introduction&lt;br /&gt;使用環境：Windows XP SP3 + Visual C++ 6.0 SP6&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;將寫入wf.bin的0x33 byte處的連續4 byte值。&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;&lt;span style="color: #008080;"&gt; 1&lt;/span&gt; &lt;span style="color: #008000;"&gt;/*&lt;/span&gt;&lt;span style="color: #008000;"&gt; &lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 2&lt;/span&gt; &lt;span style="color: #008000;"&gt;(C) OOMusou 2011 &lt;/span&gt;&lt;span style="color: #008000; text-decoration: underline;"&gt;http://oomusou.cnblogs.com&lt;/span&gt;&lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 3&lt;/span&gt; &lt;span style="color: #008000;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 4&lt;/span&gt; &lt;span style="color: #008000;"&gt;Filename    : WriteNByte.c&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 5&lt;/span&gt; &lt;span style="color: #008000;"&gt;Compiler    : Visual C++ 6.0&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 6&lt;/span&gt; &lt;span style="color: #008000;"&gt;Description : how to write n byte value with n-byte position?&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 7&lt;/span&gt; &lt;span style="color: #008000;"&gt;Release     : oct.31,2011 1.0&lt;br /&gt;&lt;/span&gt;&lt;span style="color: #008080;"&gt; 8&lt;/span&gt; &lt;span style="color: #008000;"&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt; 9&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;10&lt;/span&gt; #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;&lt;span style="color: #008080;"&gt;11&lt;/span&gt; &lt;br /&gt;&lt;span style="color: #008080;"&gt;12&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; main() {&lt;br /&gt;&lt;span style="color: #008080;"&gt;13&lt;/span&gt;   FILE *fp;&lt;br /&gt;&lt;span style="color: #008080;"&gt;14&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;int&lt;/span&gt; filesize;&lt;br /&gt;&lt;span style="color: #008080;"&gt;15&lt;/span&gt;   unsigned &lt;span style="color: #0000ff;"&gt;char&lt;/span&gt; buff[&lt;span style="color: #800080;"&gt;4&lt;/span&gt;];&lt;br /&gt;&lt;span style="color: #008080;"&gt;16&lt;/span&gt;     &lt;br /&gt;&lt;span style="color: #008080;"&gt;17&lt;/span&gt;   fp = fopen(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;./wf.bin&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;, &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;rb+&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #008080;"&gt;18&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;if&lt;/span&gt; (!fp) {&lt;br /&gt;&lt;span style="color: #008080;"&gt;19&lt;/span&gt;     fclose(fp);&lt;br /&gt;&lt;span style="color: #008080;"&gt;20&lt;/span&gt;     &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; -&lt;span style="color: #800080;"&gt;1&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;21&lt;/span&gt;   }&lt;br /&gt;&lt;span style="color: #008080;"&gt;22&lt;/span&gt;      &lt;br /&gt;&lt;span style="color: #008080;"&gt;23&lt;/span&gt;   buff[&lt;span style="color: #800080;"&gt;0&lt;/span&gt;] = &lt;span style="color: #800080;"&gt;0xAC&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;24&lt;/span&gt;   buff[&lt;span style="color: #800080;"&gt;1&lt;/span&gt;] = &lt;span style="color: #800080;"&gt;0xFF&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;25&lt;/span&gt;   buff[&lt;span style="color: #800080;"&gt;2&lt;/span&gt;] = &lt;span style="color: #800080;"&gt;0x1B&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;26&lt;/span&gt;   buff[&lt;span style="color: #800080;"&gt;3&lt;/span&gt;] = &lt;span style="color: #800080;"&gt;0xAA&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;27&lt;/span&gt;     &lt;br /&gt;&lt;span style="color: #008080;"&gt;28&lt;/span&gt;   fseek(fp, &lt;span style="color: #800080;"&gt;0x33&lt;/span&gt;, SEEK_SET);&lt;br /&gt;&lt;span style="color: #008080;"&gt;29&lt;/span&gt;   fwrite(buff, &lt;span style="color: #0000ff;"&gt;sizeof&lt;/span&gt;(unsigned &lt;span style="color: #0000ff;"&gt;char&lt;/span&gt;), &lt;span style="color: #800080;"&gt;4&lt;/span&gt;, fp);&lt;br /&gt;&lt;span style="color: #008080;"&gt;30&lt;/span&gt;     &lt;br /&gt;&lt;span style="color: #008080;"&gt;31&lt;/span&gt;   fclose(fp);&lt;br /&gt;&lt;span style="color: #008080;"&gt;32&lt;/span&gt;         &lt;br /&gt;&lt;span style="color: #008080;"&gt;33&lt;/span&gt;   &lt;span style="color: #0000ff;"&gt;return&lt;/span&gt; &lt;span style="color: #800080;"&gt;0&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: #008080;"&gt;34&lt;/span&gt; }&lt;/div&gt;&lt;p&gt;15行&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;unsigned &lt;span style="color: #0000ff;"&gt;char&lt;/span&gt; buff[&lt;span style="color: #800080;"&gt;4&lt;/span&gt;];&lt;/div&gt;&lt;p&gt;宣告4 byte char array。&lt;/p&gt;&lt;p&gt;17行&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;fp = fopen(&lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;./wf.bin&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;, &lt;span style="color: #800000;"&gt;"&lt;/span&gt;&lt;span style="color: #800000;"&gt;rb+&lt;/span&gt;&lt;span style="color: #800000;"&gt;"&lt;/span&gt;);&lt;/div&gt;&lt;p&gt;由於要修改目前開啟的binary f ile，所以使用rb+，詳細請參考&lt;a href="http://www.cnblogs.com/oomusou/archive/2011/10/31/writebyte.html" target="_blank"&gt;(筆記) 如何寫入binary file某個byte的值? (C/C++) (C)&lt;/a&gt;。&lt;/p&gt;&lt;p&gt;23行&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;buff[&lt;span style="color: #800080;"&gt;0&lt;/span&gt;] = &lt;span style="color: #800080;"&gt;0xAC&lt;/span&gt;;&lt;br /&gt;buff[&lt;span style="color: #800080;"&gt;1&lt;/span&gt;] = &lt;span style="color: #800080;"&gt;0xFF&lt;/span&gt;;&lt;br /&gt;buff[&lt;span style="color: #800080;"&gt;2&lt;/span&gt;] = &lt;span style="color: #800080;"&gt;0x1B&lt;/span&gt;;&lt;br /&gt;buff[&lt;span style="color: #800080;"&gt;3&lt;/span&gt;] = &lt;span style="color: #800080;"&gt;0xAA&lt;/span&gt;;&lt;/div&gt;&lt;p&gt;分別設定每個byte的值。&lt;/p&gt;&lt;p&gt;28行&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;fseek(fp, &lt;span style="color: #800080;"&gt;0x33&lt;/span&gt;, SEEK_SET);&lt;/div&gt;&lt;p&gt;使用fseek將binary file的檔案位置移到0x33處，其中SEEK_SET表示offset是從檔頭開始。&lt;/p&gt;&lt;p&gt;29行&lt;/p&gt;&lt;div style="background-color: #F5F5F5;border: 1px solid #CCCCCC;padding:10px;"&gt;fwrite(buff, &lt;span style="color: #0000ff;"&gt;sizeof&lt;/span&gt;(unsigned &lt;span style="color: #0000ff;"&gt;char&lt;/span&gt;), &lt;span style="color: #800080;"&gt;4&lt;/span&gt;, fp);&lt;/div&gt;&lt;p&gt;正式使用fwrite將buff寫入檔案，由於單位是unsigned char，所以size為4。&lt;/p&gt;&lt;p&gt;&lt;strong&gt;See Also&lt;/strong&gt;&lt;br /&gt;&lt;a href="http://www.cnblogs.com/oomusou/archive/2011/10/31/writebyte.html" target="_blank"&gt;(筆記) 如何寫入binary file某個byte的值? (C/C++) (C)&lt;/a&gt;&lt;/p&gt;&lt;img src="http://www.cnblogs.com/oomusou/aggbug/2230902.html?type=1" width="1" height="1" alt=""/&gt;&lt;p&gt;&lt;a href="http://www.cnblogs.com/oomusou/archive/2011/10/31/write_n_byte.html" target="_blank"&gt;本文链接&lt;/a&gt;&lt;/p&gt;</content></entry></feed>
