FFS(3)

๐Ÿ“ฆ
version 15.1 ยท 2 versions
, in freebsd 15.1

๐Ÿ’… FFS(3) โ€” find first or last bit set in a bit string

okay bestie, welcome to the Library Functions Manual corner of the internet, where we learn how to find your first and/or last bit that is set. yes. we're doing bit math. and honestly? it's kinda iconic. ๐ŸŽ€


โœจ NAME โœจ

ffs, ffsl, ffsll, fls, flsl, flsll โ€” find first or last bit set in a bit string ๐Ÿฆ‹

๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C Library โ€” that's libc, and you link it with -lc. slay.

๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <strings.h>

int
ffs(int value);

int
ffsl(long value);

int
ffsll(long long value);

int
fls(int value);

int
flsl(long value);

int
flsll(long long value);

๐Ÿ’– DESCRIPTION ๐Ÿ’–

okay so here's the whole vibe, and it's genuinely not that deep:

  • ffs(), ffsl(), ffsll() โ€” these find the first (aka the least significant) bit that's set in value, and they hand you back the index of that bit. ๐Ÿฅ‡
  • fls(), flsl(), flsll() โ€” same energy but from the other end: they find the last (aka the most significant) bit that's set in value, and return its index. ๐Ÿ

and the counting system? bits are numbered starting at 1, and 1 is the least significant bit. no zero-indexing here, we're quirky like that.

โš ๏ธ one thing fr fr: if any of these functions return zero, it means your argument was zero. like, zero bits set. empty. no bits. that's the whole tell. ๐Ÿ’€

๐Ÿ” SEE ALSO ๐Ÿ”

๐Ÿ“œ STANDARDS ๐Ÿ“œ

  • ffs() conforms to IEEE Std 1003.1-2008 (โ€œPOSIX.1โ€). periodt. โœ…
  • ffsl() and ffsll() conform to . โ€” yes, that's literally how it's written, the standard reference is blank in the original, so please don't @ me, I'm just the messenger ๐Ÿซถ

๐Ÿงš HISTORY ๐Ÿงš

  • ffs() appeared in 4.3BSD. and get this โ€” its prototype lived its best life in <string.h> before it was moved to <strings.h> for IEEE Std 1003.1-2001 (โ€œPOSIX.1โ€) compliance. glow-up tbh โœจ
  • ffsl(), fls(), and flsl() appeared in FreeBSD 5.3. ๐Ÿ‘‘
  • ffsll() and flsll() appeared in FreeBSD 7.1. later bloomers, still icons ๐Ÿ’…

okay that's the whole page bestie, go set some bits ๐Ÿ’œ โ€” from the Debian crew, page dated October 25, 2025, filed under FFS(3), mwah ๐Ÿ“

๐Ÿ“ฆ where this page comes from
  • freebsd 15.1: base 15.1
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms