Alpha centrality: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
en>QCRI
No edit summary
en>Lababidi
m show the vectors as vectors
 
Line 1: Line 1:
In [[software]], '''find first set''' ('''ffs''') or '''find first one''' is a [[bit operation]] that, given an unsigned [[Word (computer architecture)|machine word]], identifies the least significant index or position of the bits set to one in the word. A nearly equivalent operation is '''count trailing zeros''' ('''ctz''') or '''number of trailing zeros''' ('''ntz'''), which counts the number of zero bits following the least significant one bit. The complementary operation that finds the index or position of the most significant set bit is ''log base 2'', so called because it computes the [[binary logarithm]] <math>\lfloor \log_2 x\rfloor</math>.<ref>Anderson, [http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious Find the log base 2 of an integer with the MSB N set in O(N) operations (the obvious way)]</ref> This is closely related to '''count leading zeros''' ('''clz''') or '''number of leading zeros''' ('''nlz'''), which counts the number of zero bits preceding the most significant one bit. These four operations also have negated versions:
There is nothing to say about myself I think.<br>Lovely to be a part of wmflabs.org.<br>I just hope I am useful at all<br>xunjie 興繊維衣服株式会社に、
* '''find first zero''' ('''ffz'''), which identifies the index of the least significant zero bit;
エレガントなドレスとメタリックスーツのロベルトカヴァッリ2011秋のシリーズシーンが少しでも魔法の手段。
* '''count trailing ones''', which counts the number of one bits following the least significant zero bit.
双方は物品の調達の分野で協力する。 [http://www.almondpress.org/hot/fashion/chanel.html ����ͥ� ؔ��] わずかに小さなピンクの露出人気の今シーズンの焦点は非常に暖かいです革手袋、
* '''count leading ones''', which counts the number of one bits preceding the most significant zero bit;
ショーよりも髪多くを作るために、
* The operation that finds the index of the most significant zero bit, which does not have a common name.
摂取watanabe猟鴨靴の経典スタイルと荒い質感、 [http://www.almondpress.org/hot/fashion/chanel.html ����ͥ� ؔ�� ��ʽ] 登録商標を偽造の罪で有罪と5年の刑を宣告し、
There are two common variants of find first set, the POSIX definition which starts indexing of bits at 1,<ref name="ffsmanpage">{{cite web|title=FFS(3)|url=http://www.kernel.org/doc/man-pages/online/pages/man3/ffs.3.html|work=Linux Programmer's Manual|publisher=The Linux Kernel Archives|accessdate=2 January 2012}}</ref> herein labelled ffs, and the variant which starts indexing of bits at zero, which is equivalent to ctz and so will be called by that name.
伝統と都会の女性のイメージのない伝統的なロマンチックでエレガントな新しいタイプの尊重であるである。
提示会議キャットウォークの2013年の春と夏の衣類ラインのビデオ。[http://www.schochauer.ch/_js/r/e/mall/watch/gaga/ �����ߥ�� �rӋ ��ǥ��`��] ダークグリーンの着色の色調:色を使用することができます風が衣類や布の質感の背景色は、
彼女自身の非常に鋭いファッションセンスを持っていません。
「インヤンバレーは「常に出発点として「プロフェッショナル、
体のどの部分の温度であると感じていることが報告され、 [http://www.steadfast-hawaii.org/img/p/hot/chloe.php ���� ��<br><br> ���꥽��]


== Examples ==
my web blog [http://www.swiss-equity-fair.ch/pagemedia/js/li/bag/coach/ クロエ 財布]
Given the following 32-bit word:
:00000000000000001000000000001000
The count trailing zeros operation would return 3, while the count leading zeros operation returns 16. The count leading zeros operation depends on the word size: if this 32-bit word were truncated to a 16-bit word, count leading zeros would return zero. The find first set operation would return 4, indicating the 4th position from the right. The log base 2 is 15.
 
Similarly, given the following 32-bit word, the bitwise negation of the above word:
:11111111111111110111111111110111
The count trailing ones operation would return 3, the count leading ones operation would return 16, and the find first zero operation ffz would return 4.
 
If the word is zero (no bits set), count leading zeros and count trailing zeros both return the number of bits in the word, while ffs returns zero. Both log base 2 and zero-based implementations of find first set generally return an undefined result for the zero word.
 
== Hardware support ==
Many architectures include [[instruction set|instructions]] to rapidly perform find first set and/or related operations, listed below. The most common operation is count leading zeros (clz), likely because all other operations can be implemented efficiently in terms of it (see [[#Properties and relations|Properties and relations]]).
 
{|class="wikitable"
! Platform !! Mnemonic !! Name !! Word sizes !! Description !! Result on zero input
|-
| [[Intel 80386|Intel 386]] and later || bsf<ref name="intel dev manual">{{cite book|title=Intel 64 and IA-32 Architectures Software Developer Manual|publisher=Intel|location=Volume 2A|pages=3-92&ndash;3-97|url=http://www.intel.com/content/www/us/en/processors/architectures-software-developer-manuals.html}} Order number 325383.</ref> || Bit Scan Forward || 16, 32, 64 || ctz || Undefined, sets zero flag
|-
| [[Intel 80386|Intel 386]] and later || bsr<ref name="intel dev manual"/> || Bit Scan Reverse || 16, 32, 64 || log base 2 || Undefined, sets zero flag
|-
| [[x86]] supporting [[Bit Manipulation Instruction Sets|ABM]] || lzcnt<ref>{{cite book|title=AMD64 Architecture Programmer's Manual Volume 3: General Purpose and System Instructions3|year=2011|publisher=AMD|pages=204&ndash;5|url=http://support.amd.com/us/Processor_TechDocs/APM_V3_24594.pdf}}</ref> || Count Leading Zeros || 16, 32, 64 || clz || input size, sets carry flag
|-
| [[x86]] supporting [[Bit Manipulation Instruction Sets|BMI1]] || tzcnt<ref>{{cite web|url=http://support.amd.com/TechDocs/24594.pdf|title=AMD64 Architecture Programmer's Manual, Volume 3: General-Purpose and System Instructions|date=October 2013 |accessdate=2014-01-02 |publisher=[[AMD]] |work=amd.com|format=PDF}}</ref>|| Count Trailing Zeros || 16, 32, 64 || ctz || input size, sets carry flag
|-
| [[Itanium]] || clz<ref>{{cite book|title=Intel Itanium Architecture Software Developer's Manual. Volume 3: Intel Itanium Instruction Set|year=2010|publisher=Intel|pages=3:38|url=http://www.intel.com/content/www/us/en/processors/itanium/itanium-architecture-vol-3-manual.html}}</ref> || Count Leading Zeros || 64 || clz || 64
|-
| [[ARM architecture|ARM]] 5 or later || clz<ref>{{cite web|title=ARM Instruction Reference > ARM general data processing instructions > CLZ|url=http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0068b/CIHJGJED.html|work=ARM Developer Suite Assembler Guide|publisher=ARM|accessdate=3 January 2012}}</ref>  || Count Leading Zeros || 32 || clz || 32
|-
| [[IBM POWER Instruction Set Architecture|POWER]]/[[PowerPC]]/[[Power Architecture]] || cntlz/cntlzw/cntlzd<ref>{{cite book|last=Frey|first=Brad|title=PowerPC Architecture Book|publisher=IBM|location=3.3.11 Fixed-Point Logical Instructions |pages=70|edition=Version 2.02|url=http://www.ibm.com/developerworks/systems/library/es-archguide-v2.html}}</ref> || Count Leading Zeros || 32, 64 || clz || input size
|-
| [[SPARC|Oracle SPARC Architecture 2011 and later]] || lzcnt (synonym: lzd) <ref>{{cite book|title=Oracle SPARC Architecture 2011|publisher=Oracle|url=http://www.oracle.com/technetwork/server-storage/sun-sparc-enterprise/documentation/index.html}}</ref> || Leading Zero Count || 64 || clz || 64
|-
| [[MIPS architecture|MIPS]] || clz<ref name="mips32">{{cite book|title=MIPS Architecture For Programmers. Volume II-A: The MIPS32 Instruction Set|year=2011|publisher=MIPS Technologies|edition=Revision 3.02|pages=101–102|url=http://www.mips.com/products/architectures/mips32/}}</ref><ref name="mips64">{{cite book|title=MIPS Architecture For Programmers. Volume II-A: The MIPS64 Instruction Set|year=2011|publisher=MIPS Technologies|edition=Revision 3.02|pages=105, 107, 122, 123|url=http://www.mips.com/products/architectures/mips64/}}</ref>  || Count Leading Zeros in Word || 32, 64 || clz || input size
|-
| [[MIPS architecture|MIPS]] || clo<ref name="mips32"/><ref name="mips64"/> || Count Leading Ones in Word || 32, 64 || clo || input size
|-
| [[DEC Alpha]] || ctlz<ref name=alpha>{{cite book|title=Alpha Architecture Reference Manual|year=2002|publisher=Compaq|pages=4-32, 4-34|url=http://download.majix.org/dec/alpha_arch_ref.pdf}}</ref> || Count Leading Zeros || 64 || clz || 64
|-
| [[DEC Alpha]] || cttz<ref name=alpha/> || Count Trailing Zeros || 64 || ctz || 64
|-
| [[Motorola 68020]] and later || bfffo<ref>{{cite book|title=M68000 Family Programmer's Reference Manual|year=1992|publisher=Motorola|pages=4-43&ndash;4-45|url=http://www.freescale.com/files/archives/doc/ref_manual/M68000PRM.pdf}}</ref> || Find First One in Bit Field || arbitrary || log base 2 || field offset + field width
|}
 
'''Notes:''' On some Alpha platforms CTLZ and CTTZ are emulated in software.
 
== Tool and library support ==
 
A number of compiler and library vendors supply compiler intrinsics or library functions to perform find first set and/or related operations, which are frequently implemented in terms of the hardware instructions above:
{|class="wikitable"
! Tool/library !! Name !! Type !! Input type(s) !! Notes !! Result for zero input
|-
| [[POSIX]].1 compliant libc<br/>[[4.3BSD]] libc<br/>OS X 10.3 libc<ref name="ffsmanpage"/><ref>{{cite web|title=FFS(3)|url=http://developer.apple.com/library/mac/#documentation/darwin/reference/manpages/10.3/man3/ffs.3.html|work=Mac OS X Developer Library|publisher=Apple, Inc.|date=1994-04-19|accessdate=4 January 2012}}</ref> || <code>ffs</code> || Library function || int || Includes [[glibc]].<br/>POSIX does not supply the complementary log base 2 / clz. || 0
|-
| [[FreeBSD]] 5.3 libc<br/>OS X 10.4 libc<ref>{{cite web|title=FFS(3)|url=http://developer.apple.com/library/mac/#documentation/darwin/reference/manpages/man3/ffs.3.html|work=Mac OS X Developer Library|publisher=Apple|date=2004-01-13|accessdate=4 January 2012}}</ref> || <code>ffsl<br/>fls, flsl</code> || Library function || int, long || fls ("find last set") computes (log base 2) + 1. || 0
|-
| [[FreeBSD]] 7.1 libc<ref>{{cite web|title=FFS(3)|url=http://www.freebsd.org/cgi/man.cgi?query=ffs&apropos=0&sektion=0&manpath=FreeBSD+8.2-RELEASE&arch=default&format=html|work=FreeBSD Library Functions Manual|publisher=The FreeBSD Project|accessdate=4 January 2012}}</ref> || <code>ffsll, flsll</code> || Library function || long long || || 0
|-
| rowspan="3" | [[GNU Compiler Collection|GCC]] 3.2.2<ref>{{cite web|title=5.46 Other built-in functions provided by GCC|url=http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Other-Builtins.html|work=Using the GNU Compiler Collection (GCC)|publisher=Free Software Foundation, Inc.|accessdate=3 January 2012}}</ref><ref>{{cite web|title=GCC 3.2 Release Series Changes, New Features, and Fixes|url=http://gcc.gnu.org/gcc-3.2/changes.html|work=GCC 3.2 Release Series|publisher=Free Software Foundation, Inc.|accessdate=3 January 2012}}</ref> || <code>__builtin_ffs</code>
| rowspan="3" | Built-in functions
| rowspan="3" | unsigned int,<br/>unsigned long,<br/>unsigned long long
| rowspan="3" | Possibly available before 3.2.2. || 0
|-
| <code>__builtin_ctz</code> || undefined
|-
| <code>__builtin_clz</code> || undefined
|-
|[[Visual Studio]] 2005 || <code>_BitScanForward</code><ref>{{cite web|title=_BitScanForward, _BitScanForward64|url=http://msdn.microsoft.com/en-us/library/wfd9z0bb%28VS.80%29.aspx|work=Visual Studio 2005: Visual C++: Compiler Intrinsics|publisher=Microsoft|accessdate=3 January 2012}}</ref><br/><code>_BitScanReverse</code><ref>{{cite web|title=_BitScanReverse, _BitScanReverse64|url=http://msdn.microsoft.com/en-US/library/fbxyd7zd(v=VS.80).aspx|work=Visual Studio 2005: Visual C++: Compiler Intrinsics|publisher=Microsoft|accessdate=3 January 2012}}</ref> ||  Compiler intrinsics || unsigned long,<br/>unsigned __int64 || Separate return value to indicate zero input || 0
|-
|[[Visual Studio]] 2008 || <code>__lzcnt</code><ref>{{cite web|title=__lzcnt16, __lzcnt, __lzcnt64|url=http://msdn.microsoft.com/en-us/library/bb384809(v=VS.90).aspx|work=Visual Studio 2008: Visual C++: Compiler Intrinsics|publisher=Microsoft|accessdate=3 January 2012}}</ref> || Compiler intrinsic || unsigned short,<br/>unsigned int,<br/>unsigned __int64 || Relies on x64-only lzcnt instruction || Input size in bits
|-
|[[Intel C++ Compiler]] || <code>_bit_scan_forward<br/>_bit_scan_reverse</code><ref>{{cite book|title=Intel C++ Compiler for Linux Intrinsics Reference|year=2006|publisher=Intel|pages=21|url=http://software.intel.com/file/6373}}</ref> || Compiler intrinsics || int || || undefined
|-
| rowspan="2" | NVIDIA [[CUDA]]<ref>{{cite book|title=NVIDIA CUDA Programming Guide|year=2010|publisher=NVIDIA|pages=92|edition=Version 3.0|url=http://developer.download.nvidia.com/compute/cuda/3_0/toolkit/docs/NVIDIA_CUDA_ProgrammingGuide.pdf}}</ref> || <code>__clz</code>
| rowspan="2" | Functions
| rowspan="2" | 32-bit, 64-bit
| rowspan="2" | Compiles to fewer instructions on the [[GeForce 400 Series]] || 32
|-
| <code>__ffs</code> || 0
|-
|[[LLVM]] || <code>llvm.ctlz.*</code><br/><code>llvm.cttz.*</code><ref>{{cite web|title='llvm.ctlz.*' Intrinsic, 'llvm.cttz.*' Intrinsic|url=http://llvm.org/docs/LangRef.html#int_ctlz|work=LLVM Language Reference Manual|publisher=The LLVM Compiler Infrastructure|accessdate=4 January 2012}}</ref>  || Intrinsic || 8, 16, 32, 64, 256 || LLVM assembly language || Input size if arg 2<br/>is 0, else undefined
|}
 
== Properties and relations ==
The count trailing zeros and find first set operations are related by ctz(x) = ffs(x) − 1 (except for the zero input). Given w bits per word, the log base 2 is easily computed from the clz and vice versa by lg(x) = w − 1 − clz(x).
 
As demonstrated in the example above, the find first zero, count leading ones, and count trailing ones operations can be implemented by negating the input and using find first set, count leading zeros, and count trailing zeros. The reverse is also true.
 
On platforms with an efficient log base 2 operation such as M68000, ctz can be computed by:
:ctz(x) = lg(x & (−x))
where "&" denotes bitwise AND and "−x" denotes the negative of x treating x as a signed integer in [[twos complement]] arithmetic. The expression x & (−x) clears all but the least-significant 1 bit, so that the most- and least-significant 1 bit are the same.
 
On platforms with an efficient count leading zeros operation such as ARM and PowerPC, ffs can be computed by:
:ffs(x) = w − clz(x & (−x)).
 
Conversely, clz can be computed using ctz by first rounding up to the nearest power of two using shifts and bitwise ORs,<ref>Anderson, [http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 Round up to the next highest power of 2].</ref> as in this 32-bit example (note that this example depends on ctz returning 32 for the zero input):
'''function''' clz(x):
    '''for each''' y '''in''' {1, 2, 4, 8, 16}: x ← x | (x >> y)
    '''return''' 32 − ctz(x + 1)
 
On platforms with an efficient [[Hamming weight]] (population count) operation such as [[SPARC]]'s POPC or [[Blackfin]]'s ONES,<ref>{{cite book|title=Blackfin Instruction Set Reference|year=2001|publisher=Analog Devices|edition=Preliminary|pages=8–24}} Part Number 82-000410-14.</ref>  ctz can be computed using the identity:<ref>{{cite web|last=Dietz|first=Henry Gordon|title=The Aggregate Magic Algorithms|url=http://aggregate.org/MAGIC/#Trailing%20Zero%20Count|publisher=University of Kentucky}}</ref><ref>{{cite web|last=GerdIsenberg|title=BitScanProtected|url=http://chessprogramming.wikispaces.com/BitScan#Bitscan forward-Index of LS1B by Popcount|work=Chess Programming Wiki|accessdate=3 January 2012}}</ref>
:ctz(x) = pop((x & (−x)) − 1),
ffs can be computed using:<ref>{{cite book|last=SPARC International, Inc.|title=The SPARC architecture manual : version 8|year=1992|publisher=Prentice Hall|location=Englewood Cliffs, N.J.|isbn=0-13-825001-4|pages=231|url=http://www.sparc.org/standards/SPARCV9.pdf|edition=Version 8.}} A.41: Population Count. Programming Note.</ref>
:ffs(x) = pop(x ^ (~(−x)))
where "^" denotes bitwise xor, and clz can be computed by:
'''function''' clz(x):
    '''for each''' y '''in''' {1, 2, 4, 8, 16}: x ← x | (x >> y)
    '''return''' 32 − pop(x)
 
The inverse problem (given i, produce an x such that ctz(x)=i) can be computed with a left-shift (1 << i).
 
Find first set and related operations can be extended to arbitrarily large [[bit array]]s in a straightforward manner by starting at one end and proceeding until a word that is not all-zero (for ffs/ctz/clz) or not all-one (for ffz/clo/cto) is encountered. A tree data structure that recursively uses bitmaps to track which words are nonzero can accelerate this.
 
== Algorithms ==
Where find first set or a related function is not available in hardware, it must be implemented in software. The simplest implementation of ffs uses a loop:
 
'''function''' ffs (x)
    '''if''' x = 0 '''return''' 0
    t ← 1
    r ← 1
    '''while''' (x & t) = 0
        t ← t << 1
        r ← r + 1
    '''return''' r
 
where "<<" denotes left-shift. Similar loops can be used to implement all the related operations. On modern architectures this loop is inefficient due to a large number of conditional branches. A lookup table can eliminate most of these:
 
table[0..2<sup>n</sup>-1] = ffs(i) '''for''' i '''in''' 0..2<sup>n</sup>-1
'''function''' ffs_table (x)
    '''if''' x = 0 '''return''' 0
    r ← 0
    '''loop'''
        '''if''' (x & (2<sup>n</sup>-1)) ≠ 0
            '''return''' r + table[x & (2<sup>n</sup>-1)]
        x ← x >> n
        r ← r + n
 
The parameter ''n'' is fixed (typically 8) and represents a [[time-space tradeoff]]. The loop may also be fully [[loop unrolling|unrolled]].
 
An algorithm for 32-bit ctz by Leiserson, Prokop, and Randall uses [[de Bruijn sequence]]s to construct a [[minimal perfect hash function]] that eliminates all branches:<ref>{{Citation
  | last1 = Leiserson  | first1 = Charles E.
  | author1-link = Charles E. Leiserson
  | last2 = Prokop | first2 = Harald
  | author2-link = Harald Prokop
  | last3 = Randall | first3 = Keith H.
  | title = Using de Bruijn Sequences to Index a 1 in a Computer Word
  | year = 1998
  | url = http://supertech.csail.mit.edu/papers/debruijn.pdf
}}</ref>
table[0..31] initialized by: '''for''' i '''from''' 0 '''to''' 31: table[ ( 0x077CB531 * ( 1 << i ) ) >> 27 ] ← i
'''function''' ctz_debruijn (x)
    '''return''' table[((x & (-x)) &times; 0x077CB531) >> 27]
 
The expression (x & (-x)) again isolates the least-significant 1 bit. There are then only 32 possible words, which the unsigned multiplication and shift hash to the correct position in the table. (Note: this algorithm does not handle the zero input.) A similar algorithm works for log base 2, but rather than isolate the most-significant bit, it rounds up to the nearest integer of the form 2<sup>''n''</sup>&minus;1 using shifts and bitwise ORs:<ref>Anderson, [http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn Find the log base 2 of an N-bit integer in O(lg(N)) operations with multiply and lookup]</ref>
table[0..31] = {0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
                8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31}
'''function''' lg_debruijn (x)
    '''for each''' y '''in''' {1, 2, 4, 8, 16}: x ← x | (x >> y)
    '''return''' table[(x &times; 0x07C4ACDD) >> 27]
 
Both the count leading zeros and count trailing zeros operations admit [[binary search]] implementations which take a logarithmic number of operations and branches, as in these 32-bit versions:<ref name="hackersdelight-clz">Warren, Section 5-3: Counting Leading 0's.</ref><ref name="hackersdelight-ctz">Warren, Section 5-4: Counting Trailing 0's.</ref>
 
'''function''' clz (x)
    '''if''' x = 0 '''return''' 32
    n ← 0
    '''if''' (x & 0xFFFF0000) = 0: n ← n + 16, x ← x << 16
    '''if''' (x & 0xFF000000) = 0: n ← n +  8, x ← x <<  8
    '''if''' (x & 0xF0000000) = 0: n ← n +  4, x ← x <<  4
    '''if''' (x & 0xC0000000) = 0: n ← n +  2, x ← x <<  2
    '''if''' (x & 0x80000000) = 0: n ← n +  1
    '''return''' n
 
'''function''' ctz (x)
    '''if''' x = 0 '''return''' 32
    n ← 0
    '''if''' (x & 0x0000FFFF) = 0: n ← n + 16, x ← x >> 16
    '''if''' (x & 0x000000FF) = 0: n ← n +  8, x ← x >>  8
    '''if''' (x & 0x0000000F) = 0: n ← n +  4, x ← x >>  4
    '''if''' (x & 0x00000003) = 0: n ← n +  2, x ← x >>  2
    '''if''' (x & 0x00000001) = 0: n ← n +  1
    '''return''' n
 
These can be assisted by a table as well, replacing the last four lines of each with a table lookup on the high/low byte.
 
Just as count leading zeros is useful for software [[floating point]] implementations, conversely, on platforms that provide hardware conversion of integers to floating point, the exponent field can be extracted and subtracted from a constant to compute the count of leading zeros. Corrections are needed to account for rounding errors.<ref name="hackersdelight-clz"/><ref>Anderson, [http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogIEEE64Float Find the integer log base 2 of an integer with an 64-bit IEEE float].</ref>
 
== Applications ==
The count leading zeros (clz) operation can be used to efficiently implement ''normalization'', which encodes an integer as ''m''&nbsp;&times;&nbsp;2<sup>''e''</sup>, where ''m'' has its most significant bit in a known position (such as the highest position). This can in turn be used to implement [[Newton-Raphson division]], perform integer to [[floating point]] conversion in software, and other applications.<ref name="hackersdelight-clz"/><ref>{{cite book|last=Sloss|first=Andrew N.|last2=Symes|first2=Dominic|last3=Wright|first3=Chris|title=ARM system developer's guide designing and optimizing system software|year=2004|publisher=Morgan Kaufman|location=San Francisco, CA|isbn=1-55860-874-5|pages=212–213|edition=1st}}</ref>
 
Count leading zeros (clz) can be used to compute the 32-bit predicate "x = y" (zero if true, one if false) via the identity clz(x − y) >> 5, where ">>" is unsigned right shift.<ref>Warren, Section 2-11: Comparison Predicates</ref> It can be used to perform more sophisticated bit operations like finding the first string of ''n'' 1 bits.<ref>Warren, Section 6-2. Find First String of 1-Bits of a Given Length.</ref> The expression 16&nbsp;−&nbsp;clz(x&nbsp;−&nbsp;1)/2 is an effective initial guess for computing the square root of a 32-bit integer using [[Newton's method]].<ref>Warren, 11-1: Integer Square Root.</ref> CLZ can efficiently implement ''null suppression'', a fast [[data compression]] technique that encodes an integer as the number of leading zero bytes together with the nonzero bytes.<ref>{{cite journal|last=Schlegel|first=Benjamin|coauthors=Rainer Gemulla, Wolfgang Lehner|title=Fast integer compression using SIMD instructions|journal=Proceedings of the Sixth International Workshop on Data Management on New Hardware (DaMoN 2010)|date=June 2010|pages=34–40|doi=10.1145/1869389.1869394|accessdate=4 January 2012}}</ref> It can also efficiently generate [[exponential distribution|exponentially distributed]] integers by taking the clz of [[uniform distribution (discrete)|uniformly random]] integers.<ref name="hackersdelight-clz"/>
 
The log base 2 can be used to anticipate whether a multiplication will overflow, since <math>\lceil\log_2 xy\rceil \leq \lceil\log_2 x\rceil + \lceil\log_2 y\rceil</math>.<ref>Warren, Section 2-12. Overflow Detection.</ref>
 
Count leading zeros and count trailing zeros can be used together to implement [[Gosper's loop-detection algorithm]],<ref>{{cite journal|last=Gosper|first=Bill|title=Loop detector|journal=HAKMEM|year=1972|issue=239|pages=Item 132|url=http://www.inwap.com/pdp10/hbaker/hakmem/flows.html#item132}}</ref> which can find the period of a function of finite range using limited resources.<ref name="hackersdelight-ctz"/>
 
A bottleneck in the [[binary GCD algorithm]] is a loop removing trailing zeros, which can be replaced by a count trailing zeros (ctz) followed by a shift. A similar loop appears in computations of the [[hailstone sequence]].
 
A [[bit array]] can be used to implement a [[priority queue]]. In this context, find first set (ffs) is useful in implementing the "pop" or "pull highest priority element" operation efficiently. The [[Linux kernel]] real-time scheduler internally uses <code>sched_find_first_bit()</code> for this purpose.<ref>{{cite book|last=Aas|first=Josh|title=Understanding the Linux 2.6.8.1 CPU Scheduler|year=2005|publisher=Silicon Graphics, Inc.|pages=19|url=http://www.inf.ed.ac.uk/teaching/courses/os/prac/lcpusched-fullpage-2x1.pdf}}</ref>
 
The count trailing zeros operation gives a simple optimal solution to the [[Tower of Hanoi]] problem: the disks are numbered from zero, and at move ''k'', disk number ctz(''k'') is moved the minimum possible distance to the right (circling back around to the left as needed). It can also generate a [[Gray code]] by taking an arbitrary word and flipping bit ctz(''k'') at step ''k''.<ref name="hackersdelight-ctz"/>
 
== Notes ==
{{reflist|30em}}
 
== References ==
* {{cite book|last=Warren|first=Henry S.|title=Hacker's Delight|year=2003|publisher=Addison-Wesley|location=Boston, Mass.|isbn=0-201-91465-4|edition=1st}}
* {{cite web|last=Anderson|first=Sean Eron|title=Bit Twiddling Hacks|url=http://graphics.stanford.edu/~seander/bithacks.html|work=Sean Eron Anderson student homepage|publisher=Stanford University|accessdate=3 January 2012}}
 
== External links ==
* [http://graphics.stanford.edu/~seander/bithacks.html Bit Twiddling Hacks], Sean Eron Anderson, Stanford University. Lists several efficient public domain C implementations for [http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightLinear count trailing zeros] and [http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious log base 2].
* [http://chessprogramming.wikispaces.com/BitScan Chess Programming Wiki: BitScan]: A detailed explanation of a number of implementation methods for ffs (called LS1B) and log base 2 (called MS1B).
 
[[Category:Binary arithmetic]]
[[Category:Computer arithmetic]]

Latest revision as of 20:43, 23 April 2014

There is nothing to say about myself I think.
Lovely to be a part of wmflabs.org.
I just hope I am useful at all
xunjie 興繊維衣服株式会社に、 エレガントなドレスとメタリックスーツのロベルトカヴァッリ2011秋のシリーズシーンが少しでも魔法の手段。 双方は物品の調達の分野で協力する。 [http://www.almondpress.org/hot/fashion/chanel.html ����ͥ� ؔ��] わずかに小さなピンクの露出人気の今シーズンの焦点は非常に暖かいです革手袋、 ショーよりも髪多くを作るために、 摂取watanabe猟鴨靴の経典スタイルと荒い質感、 [http://www.almondpress.org/hot/fashion/chanel.html ����ͥ� ؔ�� ��ʽ] 登録商標を偽造の罪で有罪と5年の刑を宣告し、 伝統と都会の女性のイメージのない伝統的なロマンチックでエレガントな新しいタイプの尊重であるである。 提示会議キャットウォークの2013年の春と夏の衣類ラインのビデオ。[http://www.schochauer.ch/_js/r/e/mall/watch/gaga/ �����ߥ�� �rӋ ��ǥ��`��] ダークグリーンの着色の色調:色を使用することができます風が衣類や布の質感の背景色は、 彼女自身の非常に鋭いファッションセンスを持っていません。 「インヤンバレーは「常に出発点として「プロフェッショナル、 体のどの部分の温度であると感じていることが報告され、 [http://www.steadfast-hawaii.org/img/p/hot/chloe.php ���� ��

���꥽��]

my web blog クロエ 財布