ImageVerifierCode 换一换
格式:DOCX , 页数:60 ,大小:46.57KB ,
资源ID:2125591      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bingdoc.com/d-2125591.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Bit Twiddling Hacks.docx)为本站会员(b****1)主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(发送邮件至service@bingdoc.com或直接QQ联系客服),我们立即给予删除!

Bit Twiddling Hacks.docx

1、Bit Twiddling HacksBit Twiddling HacksBy Sean Eron Andersonseandercs.stanford.edu Individually, the code snippets here are in the public domain (unless otherwise noted) feel free to use them however you please. The aggregate collection and descriptions are 1997-2005 Sean Eron Anderson. The code and

2、descriptions are distributed in the hope that they will be useful, but WITHOUT ANY WARRANTY 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, Professor

3、Randal Bryant, the Dean of Computer Science at Carnegie Mellon University, has personally tested almost everything with his Uclid code verification system. What he hasnt tested, I have checked against all possible inputs on a 32-bit machine. To the first person to inform me of a legitimate bug in th

4、e code, Ill pay a bounty of US$10 (by check or Paypal). If directed to a charity, Ill pay US$20. Contents About the operation counting methodology Compute the sign of an integer Detect if two integers have opposite signs Compute the integer absolute value (abs) without branching Compute the minimum

5、(min) or maximum (max) of two integers without branching Determining if an integer is a power of 2 Sign extending Sign extending from a constant bit-width Sign extending from a variable bit-width Sign extending from a variable bit-width in 3 operations Conditionally set or clear bits without branchi

6、ng Conditionally negate a value without branching Merge bits from two values according to a mask Counting bits set Counting bits set, naive way Counting bits set by lookup table Counting bits set, Brian Kernighans way Counting bits set in 12, 24, or 32-bit words using 64-bit instructions Counting bi

7、ts set, in parallel Count bits set (rank) from the most-significant bit upto a given position Select the bit position (from the most-significant bit) with the given count (rank) Computing parity (1 if an odd number of bits set, 0 otherwise) Compute parity of a word the naive way Compute parity by lo

8、okup table Compute parity of a byte using 64-bit multiply and modulus division Compute parity of word with a multiply Compute parity in parallel Swapping Values Swapping values with subtraction and addition Swapping values with XOR Swapping individual bits with XOR Reversing bit sequences Reverse bi

9、ts the obvious way Reverse bits in word by lookup table Reverse the bits in a byte with 3 operations (64-bit multiply and modulus division) Reverse the bits in a byte with 4 operations (64-bit multiply, no division) Reverse the bits in a byte with 7 operations (no 64-bit, only 32) Reverse an N-bit q

10、uantity in parallel with 5 * lg(N) operations Modulus division (aka computing remainders) Computing modulus division by 1 s without a division operation (obvious) Computing modulus division by (1 s) - 1 without a division operation Computing modulus division by (1 s) - 1 in parallel without a divisi

11、on operation Finding integer log base 2 of an integer (aka the position of the highest bit set) Find the log base 2 of an integer with the MSB N set in O(N) operations (the obvious way) Find the integer log base 2 of an integer with an 64-bit IEEE float Find the log base 2 of an integer with a looku

12、p table Find the log base 2 of an N-bit integer in O(lg(N) operations Find the log base 2 of an N-bit integer in O(lg(N) operations with multiply and lookup Find integer log base 10 of an integer Find integer log base 10 of an integer the obvious way Find integer log base 2 of a 32-bit IEEE float Fi

13、nd integer log base 2 of the pow(2, r)-root of a 32-bit IEEE float (for unsigned integer r) Counting consecutive trailing zero bits (or finding bit indices) Count the consecutive zero bits (trailing) on the right linearly Count the consecutive zero bits (trailing) on the right in parallel Count the

14、consecutive zero bits (trailing) on the right by binary search Count the consecutive zero bits (trailing) on the right by casting to a float Count the consecutive zero bits (trailing) on the right with modulus division and lookup Count the consecutive zero bits (trailing) on the right with multiply

15、and lookup Round up to the next highest power of 2 by float casting Round up to the next highest power of 2 Interleaving bits (aka computing Morton Numbers) Interleave bits the obvious way Interleave bits by table lookup Interleave bits with 64-bit multiply Interleave bits by Binary Magic Numbers Te

16、sting for ranges of bytes in a word (and counting occurances found) Determine if a word has a zero byte Determine if a word has a byte equal to n Determine if a word has byte less than n Determine if a word has a byte greater than n Determine if a word has a byte between m and n Compute the lexicogr

17、aphically next bit permutation -About the operation counting methodology 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 on

18、ly 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 sys

19、tem 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. -Compute

20、the sign of an integer int v; / we want to find the sign of vint sign; / the result goes here / CHAR_BIT is the number of bits per byte (normally 8).sign = -(v 0); / if v (sizeof(int) * CHAR_BIT - 1);/ or, for one less instruction (but not portable):sign = v (sizeof(int) * CHAR_BIT - 1); The last ex

21、pression above evaluates to sign = v 31 for 32-bit integers. This is one operation faster than the obvious way, sign = -(v (sizeof(int) * CHAR_BIT - 1); / if v (sizeof(int) * CHAR_BIT - 1);/ Or, for more speed but less portability:sign = (v != 0) | (v (sizeof(int) * CHAR_BIT - 1); / -1, 0, or +1/ Or

22、, for portability, brevity, and (perhaps) speed:sign = (v 0) - (v (sizeof(int) * CHAR_BIT - 1); / if v 0 then 0, else 1Caveat: 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 mi

23、ght 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. Rohit Garg suggested the version for non-

24、negative integers on September 12, 2009. -Detect if two integers have opposite signs int x, y; / input values to compare signsbool f = (x y) sizeof(int) * CHAR_BIT - 1;r = (v + mask) mask;Patented variation: r = (v mask) - mask;Some CPUs dont have an integer absolute value instruction (or the compil

25、er fails to use them). On machines where branching is expensive, the above expression can be faster than the obvious approach, r = (v (sizeof(int)*CHAR_BIT-1)*v, because a multiply is not used. Unfortunately, this method has been patented in the USA on June 6, 2000 by Vladimir Yu Volkonsky and assigned to Sun Microsystems. On August 13, 2006, Yuriy Kaminskiy told me that the patent is likely invalid because the method was pub

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2