.\" $NetBSD: ffs32.3,v 1.6 2011/04/08 08:47:50 wiz Exp $
.\"
.\" Copyright (c) 2010 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Jukka Ruohonen.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd April 8, 2011
.Dt FFS32 3
.Os
.Sh NAME
.Nm ffs32 ,
.Nm ffs64 ,
.Nm fls32 ,
.Nm fls64
.Nd find first or last bit set
.Sh SYNOPSIS
.In sys/bitops.h
.Ft int
.Fn ffs32 "uint32_t n"
.Ft int
.Fn ffs64 "uint64_t n"
.Ft int
.Fn fls32 "uint32_t n"
.Ft int
.Fn fls64 "uint64_t n"
.Sh DESCRIPTION
The
.Fn ffs32
and
.Fn ffs64
functions find the first bit set in
.Fa n
and return the index of that bit.
Conversely,
the
.Fn fls32
and
.Fn fls64
functions find the last bit set in
.Fa n ,
returning the index of the bit.
.Pp
The search always starts from the bit 1 (the least significant bit).
If the argument
.Fa n
is zero, each function returns zero.
.Sh IMPLEMENTATION NOTES
The described functions are implemented as
.Em static inline
functions in the
.In sys/bitops.h
header.
The standard C library includes a more portable
.Xr ffs 3
for user applications.
.\"
.\" XXX: It is noted in the CVS history of <sys/bitops.h> that MD-optimized
.\" <machine/bitops.h> is a TODO. If those start to appear, note it here.
.\"
.Sh EXAMPLES
In the following example
.Va f = 3
and
.Va l = 7 :
.Bd -literal -offset indent
uint32_t n = 0x44; /* 01000100 */
int f, l;
f = ffs32(n);
l = fls32(n);
.Ed
.Sh SEE ALSO
.Xr bitops 3 ,
.Xr bits 3 ,
.Xr bitstring 3 ,
.Xr ffs 3 ,
.Xr setbit 9
.Sh HISTORY
These functions first appeared in
.Nx 5.0 .