<s>
In	O
computer	O
software	O
and	O
hardware	O
,	O
find	B-Algorithm
first	I-Algorithm
set	I-Algorithm
(	O
ffs	O
)	O
or	O
find	B-Algorithm
first	I-Algorithm
one	I-Algorithm
is	O
a	O
bit	O
operation	O
that	O
,	O
given	O
an	O
unsigned	O
machine	O
word	O
,	O
designates	O
the	O
index	O
or	O
position	O
of	O
the	O
least	O
significant	O
bit	O
set	O
to	O
one	O
in	O
the	O
word	O
counting	O
from	O
the	O
least	O
significant	O
bit	O
position	O
.	O
</s>
<s>
A	O
nearly	O
equivalent	O
operation	O
is	O
count	O
trailing	O
zeros	O
(	O
ctz	O
)	O
or	O
number	B-Algorithm
of	I-Algorithm
trailing	I-Algorithm
zeros	I-Algorithm
(	O
ntz	O
)	O
,	O
which	O
counts	O
the	O
number	O
of	O
zero	O
bits	O
following	O
the	O
least	B-Algorithm
significant	I-Algorithm
one	I-Algorithm
bit	I-Algorithm
.	O
</s>
<s>
This	O
is	O
closely	O
related	O
to	O
count	O
leading	O
zeros	O
(	O
clz	O
)	O
or	O
number	B-Algorithm
of	I-Algorithm
leading	I-Algorithm
zeros	I-Algorithm
(	O
nlz	O
)	O
,	O
which	O
counts	O
the	O
number	O
of	O
zero	O
bits	O
preceding	O
the	O
most	B-Algorithm
significant	I-Algorithm
one	I-Algorithm
bit	I-Algorithm
.	O
</s>
<s>
There	O
are	O
two	O
common	O
variants	O
of	O
find	B-Algorithm
first	I-Algorithm
set	I-Algorithm
,	O
the	O
POSIX	O
definition	O
which	O
starts	O
indexing	O
of	O
bits	O
at	O
1	O
,	O
herein	O
labelled	O
ffs	O
,	O
and	O
the	O
variant	O
which	O
starts	O
indexing	O
of	O
bits	O
at	O
zero	O
,	O
which	O
is	O
equivalent	O
to	O
ctz	O
and	O
so	O
will	O
be	O
called	O
by	O
that	O
name	O
.	O
</s>
<s>
Most	O
modern	O
CPU	O
instruction	B-General_Concept
set	I-General_Concept
architectures	I-General_Concept
provide	O
one	O
or	O
more	O
of	O
these	O
as	O
hardware	O
operators	O
;	O
software	O
emulation	O
is	O
usually	O
provided	O
for	O
any	O
that	O
are	O
n't	O
available	O
,	O
either	O
as	O
compiler	O
intrinsics	O
or	O
in	O
system	O
libraries	O
.	O
</s>
<s>
The	O
find	B-Algorithm
first	I-Algorithm
set	I-Algorithm
operation	O
would	O
return	O
4	O
,	O
indicating	O
the	O
4th	O
position	O
from	O
the	O
right	O
.	O
</s>
<s>
The	O
count	O
trailing	O
ones	O
operation	O
would	O
return	O
3	O
,	O
the	O
count	O
leading	O
ones	O
operation	O
would	O
return	O
16	O
,	O
and	O
the	O
find	B-Algorithm
first	I-Algorithm
zero	I-Algorithm
operation	O
ffz	O
would	O
return	O
4	O
.	O
</s>
<s>
Both	O
log	O
base	O
2	O
and	O
zero-based	O
implementations	O
of	O
find	B-Algorithm
first	I-Algorithm
set	I-Algorithm
generally	O
return	O
an	O
undefined	O
result	O
for	O
the	O
zero	O
word	O
.	O
</s>
<s>
Many	O
architectures	O
include	O
instructions	O
to	O
rapidly	O
perform	O
find	B-Algorithm
first	I-Algorithm
set	I-Algorithm
and/or	O
related	O
operations	O
,	O
listed	O
below	O
.	O
</s>
<s>
On	O
some	O
Alpha	B-Device
platforms	O
CTLZ	O
and	O
CTTZ	O
are	O
emulated	O
in	O
software	O
.	O
</s>
<s>
A	O
number	O
of	O
compiler	O
and	O
library	O
vendors	O
supply	O
compiler	O
intrinsics	O
or	O
library	O
functions	O
to	O
perform	O
find	B-Algorithm
first	I-Algorithm
set	I-Algorithm
and/or	O
related	O
operations	O
,	O
which	O
are	O
frequently	O
implemented	O
in	O
terms	O
of	O
the	O
hardware	O
instructions	O
above	O
:	O
</s>
<s>
Tool/libraryNameTypeInput	O
type(s )	O
NotesOn	O
application	O
to	O
0POSIX.1	O
compliant	O
libc4.3BSD	O
libcOS	O
X	O
10.3	O
libcffsLibrary	O
functionintIncludes	O
glibc	B-Language
.	O
</s>
<s>
POSIX	O
does	O
not	O
supply	O
the	O
complementary	O
log	O
base	O
2	O
/	O
clz.0FreeBSD	O
5.3	O
libcOS	O
X	O
10.4	O
libcffslflsflslLibrary	O
functionint	O
,	O
longfls("find last set" )	O
computes	O
(	O
log	O
base	O
2	O
)	O
+	O
1.0FreeBSD	O
7.1	O
libcffsllflsllLibrary	O
functionlong	O
long	O
0GCC	O
3.4.0Clang	O
5.x__builtin_ffs[l,ll,imax]__builtin_clz[l,ll,imax]__builtin_ctz[l,ll,imax]Built-in	O
functionsunsigned	O
int	O
,	O
unsigned	O
long	O
,	O
unsigned	O
long	O
long	O
,	O
uintmax_tGCC	O
documentation	O
considers	O
result	O
undefined	O
clz	O
and	O
ctz	O
on	O
0.0	O
(	O
ffs	O
)	O
Visual	B-Application
Studio	I-Application
2005_BitScanForward_BitScanReverseCompiler	O
intrinsicsunsigned	O
long	O
,	O
unsigned	O
__int64Separate	O
return	O
value	O
to	O
indicate	O
zero	O
inputUndefinedVisual	O
Studio	O
2008__lzcntCompiler	O
intrinsicunsigned	O
short	O
,	O
unsigned	O
int	O
,	O
unsigned	O
__int64Relies	O
on	O
hardware	O
support	O
for	O
the	O
lzcnt	B-Algorithm
instruction	B-General_Concept
introduced	O
in	O
BMI1	O
or	O
ABM.Operand	O
widthVisual	O
Studio	O
2012_arm_clzCompiler	O
intrinsicunsigned	O
intRelies	O
on	O
hardware	O
support	O
for	O
the	O
clz	O
instruction	B-General_Concept
introduced	O
in	O
the	O
ARMv5T	O
architecture	O
and	O
later	O
.	O
</s>
<s>
?	O
Intel	B-Language
C++	I-Language
Compiler_bit_scan_forward_bit_scan_reverseCompiler	O
intrinsicsint	O
UndefinedNVIDIA	O
CUDA__clzFunctions32-bit	O
,	O
64-bitCompiles	O
to	O
fewer	O
instructions	O
on	O
the	O
GeForce	O
400	O
Series32__ffs0LLVMllvm.ctlz.*	O
llvm.cttz	O
.	O
</s>
<s>
If	O
bits	O
are	O
labeled	O
starting	O
at	O
1	O
(	O
which	O
is	O
the	O
convention	O
used	O
in	O
this	O
article	O
)	O
,	O
then	O
count	O
trailing	O
zeros	O
and	O
find	B-Algorithm
first	I-Algorithm
set	I-Algorithm
operations	O
are	O
related	O
by	O
(	O
except	O
when	O
the	O
input	O
is	O
zero	O
)	O
.	O
</s>
<s>
If	O
bits	O
are	O
labeled	O
starting	O
at	O
,	O
then	O
count	O
trailing	O
zeros	O
and	O
find	B-Algorithm
first	I-Algorithm
set	I-Algorithm
are	O
exactly	O
equivalent	O
operations	O
.	O
</s>
<s>
As	O
demonstrated	O
in	O
the	O
example	O
above	O
,	O
the	O
find	B-Algorithm
first	I-Algorithm
zero	I-Algorithm
,	O
count	O
leading	O
ones	O
,	O
and	O
count	O
trailing	O
ones	O
operations	O
can	O
be	O
implemented	O
by	O
negating	O
the	O
input	O
and	O
using	O
find	B-Algorithm
first	I-Algorithm
set	I-Algorithm
,	O
count	O
leading	O
zeros	O
,	O
and	O
count	O
trailing	O
zeros	O
.	O
</s>
<s>
where	O
denotes	O
bitwise	O
AND	O
and	O
denotes	O
the	O
two	B-General_Concept
's	I-General_Concept
complement	I-General_Concept
of	O
.	O
</s>
<s>
On	O
platforms	O
with	O
an	O
efficient	O
count	O
leading	O
zeros	O
operation	O
such	O
as	O
ARM	B-Architecture
and	O
PowerPC	B-Architecture
,	O
can	O
be	O
computed	O
by	O
:	O
</s>
<s>
On	O
platforms	O
with	O
an	O
efficient	O
Hamming	O
weight	O
(	O
population	O
count	O
)	O
operation	O
such	O
as	O
SPARC	B-Architecture
's	O
POPC	O
or	O
Blackfin	B-General_Concept
's	O
ONES	O
,	O
there	O
is	O
:	O
</s>
<s>
Find	B-Algorithm
first	I-Algorithm
set	I-Algorithm
and	O
related	O
operations	O
can	O
be	O
extended	O
to	O
arbitrarily	O
large	O
bit	B-Data_Structure
arrays	I-Data_Structure
in	O
a	O
straightforward	O
manner	O
by	O
starting	O
at	O
one	O
end	O
and	O
proceeding	O
until	O
a	O
word	O
that	O
is	O
not	O
all-zero	O
(	O
for	O
,	O
,	O
)	O
or	O
not	O
all-one	O
(	O
for	O
,	O
,	O
)	O
is	O
encountered	O
.	O
</s>
<s>
Most	O
CPUs	O
dating	O
from	O
the	O
late	O
1980s	O
onward	O
have	O
bit	O
operators	O
for	O
ffs	O
or	O
equivalent	O
,	O
but	O
a	O
few	O
modern	O
ones	O
like	O
some	O
of	O
the	O
ARM-Mx	O
series	O
do	O
not	O
.	O
</s>
<s>
The	O
approaches	O
may	O
be	O
loosely	O
described	O
as	O
linear	B-Algorithm
search	I-Algorithm
,	O
binary	O
search	O
,	O
search+table	O
lookup	O
,	O
de	O
Bruijn	O
multiplication	O
,	O
floating	B-Algorithm
point	I-Algorithm
conversion/exponent	O
extract	O
,	O
and	O
bit	O
operator	O
(	O
branchless	O
)	O
methods	O
.	O
</s>
<s>
The	O
function	O
(	O
round	O
up	O
to	O
the	O
nearest	O
power	B-Architecture
of	O
two	O
)	O
using	O
shifts	O
and	O
bitwise	O
ORs	O
is	O
not	O
efficient	O
to	O
compute	O
as	O
in	O
this	O
32-bit	O
example	O
and	O
even	O
more	O
inefficient	O
if	O
we	O
have	O
a	O
64-bit	O
or	O
128-bit	O
operand	O
:	O
</s>
<s>
The	O
loop	O
may	O
also	O
be	O
fully	O
unrolled	B-Operating_System
.	O
</s>
<s>
An	O
algorithm	O
for	O
32-bit	O
ctz	O
uses	O
de	B-Language
Bruijn	I-Language
sequences	I-Language
to	O
construct	O
a	O
minimal	O
perfect	O
hash	O
function	O
that	O
eliminates	O
all	O
branches	O
.	O
</s>
<s>
On	O
platforms	O
that	O
provide	O
hardware	O
conversion	O
of	O
integers	O
to	O
floating	B-Algorithm
point	I-Algorithm
,	O
the	O
exponent	O
field	O
can	O
be	O
extracted	O
and	O
subtracted	O
from	O
a	O
constant	O
to	O
compute	O
the	O
count	O
of	O
leading	O
zeros	O
.	O
</s>
<s>
Floating	B-Algorithm
point	I-Algorithm
conversion	O
can	O
have	O
substantial	O
latency	O
.	O
</s>
<s>
This	O
can	O
in	O
turn	O
be	O
used	O
to	O
implement	O
Newton	O
–	O
Raphson	O
division	O
,	O
perform	O
integer	O
to	O
floating	B-Algorithm
point	I-Algorithm
conversion	O
in	O
software	O
,	O
and	O
other	O
applications	O
.	O
</s>
<s>
CLZ	O
can	O
efficiently	O
implement	O
null	O
suppression	O
,	O
a	O
fast	O
data	B-General_Concept
compression	I-General_Concept
technique	O
that	O
encodes	O
an	O
integer	O
as	O
the	O
number	O
of	O
leading	O
zero	O
bytes	O
together	O
with	O
the	O
nonzero	O
bytes	O
.	O
</s>
<s>
A	O
bit	B-Data_Structure
array	I-Data_Structure
can	O
be	O
used	O
to	O
implement	O
a	O
priority	B-Application
queue	I-Application
.	O
</s>
<s>
In	O
this	O
context	O
,	O
find	B-Algorithm
first	I-Algorithm
set	I-Algorithm
(	O
ffs	O
)	O
is	O
useful	O
in	O
implementing	O
the	O
"	O
pop	O
"	O
or	O
"	O
pull	O
highest	O
priority	O
element	O
"	O
operation	O
efficiently	O
.	O
</s>
<s>
The	O
Linux	B-Operating_System
kernel	I-Operating_System
real-time	O
scheduler	O
internally	O
uses	O
sched_find_first_bit( )	O
for	O
this	O
purpose	O
.	O
</s>
<s>
The	O
count	O
trailing	O
zeros	O
operation	O
gives	O
a	O
simple	O
optimal	O
solution	O
to	O
the	O
Tower	B-Language
of	I-Language
Hanoi	I-Language
problem	O
:	O
the	O
disks	O
are	O
numbered	O
from	O
zero	O
,	O
and	O
at	O
move	O
k	O
,	O
disk	O
number	O
ctz(k )	O
is	O
moved	O
the	O
minimum	O
possible	O
distance	O
to	O
the	O
right	O
(	O
circling	O
back	O
around	O
to	O
the	O
left	O
as	O
needed	O
)	O
.	O
</s>
<s>
It	O
can	O
also	O
generate	O
a	O
Gray	B-Device
code	I-Device
by	O
taking	O
an	O
arbitrary	O
word	O
and	O
flipping	O
bit	O
ctz(k )	O
at	O
step	O
k	O
.	O
</s>
