STDC_BIT_WIDTH(3)
π STDC_BIT_WIDTH(3) π
okay so this is STDC_BIT_WIDTH(3), straight out of the Library Functions Manual, and bestie it is so much less scary than the name makes it sound. sit down, grab a boba, we're counting bits π§
β¨ NAME β¨
stdc_bit_width β find the first trailing one in an intege
not me pointing out that the official NAME line literally ends mid-word at "intege" π the page said what it said, i'm just the messenger, fr fr
πΈ SYNOPSIS πΈ
it's Standard C Library (libc, -lc), and first you gotta pull in the header:
#include <stdbit.h>
then all the flavours of the function, each one returning an unsigned int:
unsigned int
stdc_bit_width_uc(unsigned char value);
unsigned int
stdc_bit_width_us(unsigned short value);
unsigned int
stdc_bit_width_ui(unsigned int value);
unsigned int
stdc_bit_width_ul(unsigned long value);
unsigned int
stdc_bit_width_ull(unsigned long long value);
unsigned int
stdc_bit_width(value);
π DESCRIPTION π
so the stdc_bit_width_type() family of functions returns the number of bits needed to represent value β and value is of type unsigned char, unsigned short, unsigned int, unsigned long, or unsigned long long, for type being uc, us, ui, ul, or ull respectively. it's giving "pick your size, we got you" π
and then there's the stdc_bit_width(value) type-generic macro, which is the main character of this whole situation: it picks the appropriate stdc_bit_width_type() function based on the type of value. no cap, you don't have to think about it. it just knows. π₯
also important, bestie: the functions stdc_bit_width_ui(), stdc_bit_width_ul(), and stdc_bit_width_ull() are identical to the 4.3BSD functions fls(3), flsl(3), and flsll(3) respectively β except they operate on unsigned instead of signed values. that's the one little difference, and it's iconic of them tbh.
β RETURN VALUES β
it returns the least number of bits needed to represent value.
- if
valueis zero, the return value is zero π«Ά - otherwise it's
1 + β logβ value β
that's it. that's the whole vibe check.
π SEE ALSO π
bit_fls(3)fls(3)flsl(3)flsll(3)stdbit(3)stdc_count_leading_zeros(3)stdc_first_leading_one(3)
go read the whole friend group, they're all connected iykyk π¦
π STANDARDS π
the stdc_bit_width_type() family of functions and the stdc_bit_width() type-generic macro conform to .
and yes, there's literally nothing after "conform to" in the source β the standard name got thanos-snapped π i'm not about to invent it for you, so take it up with the page, not me
πΈ HISTORY πΈ
the stdc_bit_width_type() family of functions and the stdc_bit_width() type-generic macro were added in FreeBSD 15.1. new-ish, lowkey fresh, we love a recent addition π
π AUTHOR π
Robert Clausecker <fuz@FreeBSD.org> β thank you for your service, king π«‘
aaand that's the page!! mwah π this was STDC_BIT_WIDTH(3) from Debian, dated November 9, 2025 β shouted out at the very bottom like a true sign-off. go forth and count your bits, bestie, you're gonna slay β¨ππ
π¦ where this page comes from
- freebsd 15.1: base 15.1
- π the page's license: BSD-2-Clause
- Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms