ArticleslgStudy

computer science

Find first set

Find first set is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Find first set rather than just read about it. In short: 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.

Key takeaways

  • Find first set belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Find first set to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Find first set from memory before moving on to harder problems.

Reference excerpt

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.

Worked examples

Example 1 — a first encounter with Find first set

Start with the simplest possible case. Write down what Find first set claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Find first set before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Find first set ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Find first set

In research
Find first set appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Find first set in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Find first set is common in secondary-school and first-year university syllabi. It links to neighbouring topics Binary arithmetic, Computer arithmetic, so understanding it makes those chapters shorter.
In everyday life
Look for Find first set outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Find first set” →

How to study Find first set in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Find first set means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Find first set out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Find first set in simple terms?

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…

Why does Find first set matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Find first set?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Find first set.

Tags

  • Binary arithmetic
  • Computer arithmetic

Keep exploring