xref: /freebsd/lib/libc/string/ffs.3 (revision fbf05d2147b1add8b760be166c4b1fd4499ebce8)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek.
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.\"
30.Dd October 25, 2025
31.Dt FFS 3
32.Os
33.Sh NAME
34.Nm ffs ,
35.Nm ffsl ,
36.Nm ffsll ,
37.Nm fls ,
38.Nm flsl ,
39.Nm flsll
40.Nd find first or last bit set in a bit string
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In strings.h
45.Ft int
46.Fn ffs "int value"
47.Ft int
48.Fn ffsl "long value"
49.Ft int
50.Fn ffsll "long long value"
51.Ft int
52.Fn fls "int value"
53.Ft int
54.Fn flsl "long value"
55.Ft int
56.Fn flsll "long long value"
57.Sh DESCRIPTION
58The
59.Fn ffs ,
60.Fn ffsl
61and
62.Fn ffsll
63functions find the first (least significant) bit set
64in
65.Fa value
66and return the index of that bit.
67.Pp
68The
69.Fn fls ,
70.Fn flsl
71and
72.Fn flsll
73functions find the last (most significant) bit set in
74.Fa value
75and return the index of that bit.
76.Pp
77Bits are numbered starting at 1, the least significant bit.
78A return value of zero from any of these functions means that the
79argument was zero.
80.Sh SEE ALSO
81.Xr bitstring 3 ,
82.Xr stdc_first_trailing_one 3 ,
83.Xr stdc_trailing_zeros 3 ,
84.Xr bitset 9
85.Sh STANDARDS
86The
87.Fn ffs
88function conforms to
89.St -p1003.1-2008 .
90The
91.Fn ffsl
92and
93.Fn ffsll
94functions conform to
95.St -p1003.1-2024 .
96.Sh HISTORY
97The
98.Fn ffs
99function appeared in
100.Bx 4.3 .
101Its prototype existed previously in
102.In string.h
103before it was moved to
104.In strings.h
105for
106.St -p1003.1-2001
107compliance.
108.Pp
109The
110.Fn ffsl ,
111.Fn fls
112and
113.Fn flsl
114functions appeared in
115.Fx 5.3 .
116The
117.Fn ffsll
118and
119.Fn flsll
120functions appeared in
121.Fx 7.1 .
122