In computer software and hardware, find first set (ffs) or find first one is a bit operation which returns the index or position of the least significant 'one' bit in its argument, counting from the least significant bit. A related 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. There are two common variants of find first set, the POSIX definition which starts indexing of bits at 1, herein labelled ffs, and the variant which starts indexing of bits at zero, which is equivalent to ctz. The operation which finds the index of the most significant set bit is log base 2, so called because it computes the binary logarithm ⌊log2(x)⌋. 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. Most modern CPU instruction set architectures provide one or more of these as hardware operators; software emulation is usually provided for the others, either as compiler intrinsics or in system libraries.
Examples Given the following 32-bit word:
00000000000000001000000000001000 The count trailing zeros operation would return 3, while the count leading zeros operation would return 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 logarithm of this number is log2 32776 = 15.000352, so the truncated base-2 logarithm is 15. Similarly, given 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 instruction set architectures include find first set or related operations. The most common operation is count leading zeros (clz); other operations can be implemented efficiently in terms of it (see Properties and relations).
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:
Properties and relations If bits are labeled starting at 1 (which is the convention used in this article), then count trailing zeros and find first set operations are related by ctz(x) = ffs(x) − 1 (except when the input is zero). If bits are labeled starting at 0, then count trailing zeros and find first set are exactly equivalent operations. Given w bits per word, the log2 is easily computed from the clz and vice versa by log2(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 log2 operation such as M68000, ctz can be computed by:
ctz(x) = log2(x & −x) where & denotes bitwise AND and −x denotes the two's complement of x. 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, on machines without log2 or clz operators, clz can be computed using ctz, albeit inefficiently:
clz = w − ctz(2⌈log2(x)⌉) (which depends on ctz returning w for the zero input) On platforms with an efficient Hamming weight (population count) operation such as SPARC's POPC or Blackfin's ONES, there is:
ctz(x) = popcount((x & −x) − 1), or ctz(x) = popcount(~(x | −x)), ffs(x) = popcount(x ^ ~−x) clz = 32 − popcount(2⌈log2(x)⌉ − 1) where ^ denotes bitwise exclusive-OR, | denotes bitwise OR and ~ denotes bitwise negation. 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 arrays 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.
Software emulation Most CPUs dating from the late 1980s onward have bit operators for ffs or equivalent, but a few modern ones like some of the ARM Cortex-M processors do not. In lieu of hardware operators for ffs, clz and ctz, software can emulate them with shifts, integer arithmetic and bitwise operators. There are several approaches depending on architecture of the CPU and to a lesser extent, the programming language semantics and compiler code generation quality. The approaches may be loosely described as linear search, binary search, search+table lookup, de Bruijn multiplication, floating point conversion/exponent extract, and bit operator (branchless) methods. There are tradeoffs between execution time and storage space as well as portability and efficiency. Software emulations are usually deterministic. They return a defined result for all input values; in particular, the result for an input of all zero bits is usually 0 for ffs, and the bit length of the operand for the other operations. If one has a hardware clz or equivalent, ctz can be efficiently computed with bit operations, but the converse is not true: clz is not efficient to compute in the absence of a hardware operator.
2n The function 2⌈log2(x)⌉ (round up to the nearest power of two) using shifts and bitwise ORs is not efficient to compute as in this 32-bit example and even more inefficient if we have a 64-bit or 128-bit operand:
function pow2(x): if x = 0 return invalid // invalid is implementation defined (not in [0,63]) x ← x - 1 for each y in {1, 2, 4, 8, 16}: x ← x | (x >> y) return x + 1
… excerpt ends here. Continue reading the full article.