C Reference ยท ctype.h, math.h and others
C stdbit.h: bit utilities (C23)
Reference for the C23 header stdbit.h: stdc_count_ones, stdc_leading_zeros, stdc_bit_width, stdc_bit_ceil and a portable fallback for older libraries.
<stdbit.h> is a C23 header that standardizes the small bit tricks every systems programmer ends up writing: count the set bits, find the highest set bit, test whether a number is a power of two, round up to the next power of two, and detect the byte order. Before C23 you used compiler builtins such as __builtin_popcount or hand-written loops.
The functions are type-generic macros named stdc_* that work on any unsigned integer type, plus suffixed functions for each type (_uc for unsigned char, _us, _ui, _ul, _ull).
Runner note: the compiler here (GCC 15.2) accepts C23, but <stdbit.h> comes from the C library, and the sandbox's glibc 2.28 does not have it, so #include <stdbit.h> fails. The examples therefore use __has_include to detect it and fall back to portable code. The stdc_* calls themselves are shown read-only.
#include <stdbit.h> unsigned n = stdc_count_ones(0xF0u); /* 4 */ unsigned w = stdc_bit_width(1000u); /* 10 */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
Read-only, as the runner cannot compile these:
#include <stdbit.h> unsigned int stdc_leading_zeros(generic_value_type value); unsigned int stdc_leading_ones(generic_value_type value); unsigned int stdc_trailing_zeros(generic_value_type value); unsigned int stdc_trailing_ones(generic_value_type value); unsigned int stdc_first_leading_zero(generic_value_type value); unsigned int stdc_first_leading_one(generic_value_type value); unsigned int stdc_first_trailing_zero(generic_value_type value); unsigned int stdc_first_trailing_one(generic_value_type value); unsigned int stdc_count_zeros(generic_value_type value); unsigned int stdc_count_ones(generic_value_type value); bool stdc_has_single_bit(generic_value_type value); unsigned int stdc_bit_width(generic_value_type value); generic_value_type stdc_bit_floor(generic_value_type value); generic_value_type stdc_bit_ceil(generic_value_type value);
| Function | Result |
|---|---|
stdc_leading_zeros, stdc_leading_ones |
Number of consecutive 0 (or 1) bits from the most significant end. |
stdc_trailing_zeros, stdc_trailing_ones |
The same from the least significant end. |
stdc_first_leading_zero, stdc_first_leading_one |
1-based position of the first 0 (or 1) from the top, or 0 if there is none. |
stdc_first_trailing_zero, stdc_first_trailing_one |
The same from the bottom (like POSIX ffs). |
stdc_count_zeros, stdc_count_ones |
Population count of 0 bits or 1 bits. |
stdc_has_single_bit |
True if exactly one bit is set (a power of two). |
stdc_bit_width |
Smallest number of bits needed to represent the value; 0 for 0. |
stdc_bit_floor, stdc_bit_ceil |
Largest power of two not greater than the value (0 for 0), smallest power of two not less than it. |
Each name has typed variants: stdc_count_ones_uc, _us, _ui, _ul, _ull. The generic macro chooses by the type of its argument, which must be an unsigned standard or extended integer type (not bool, plain char or a signed type).
The header also defines __STDC_ENDIAN_LITTLE__, __STDC_ENDIAN_BIG__ and __STDC_ENDIAN_NATIVE__; compare the native one with the others to learn the byte order at compile time.
Returns: results are unsigned int, except stdc_has_single_bit (bool) and the floor/ceil pair (same type as the argument).
Errors / undefined behavior: none for the counting functions. stdc_bit_ceil of a value whose next power of two does not fit in the type has no meaningful result, so treat such input as a bug and check the range first.
Usage notes
- Check for the header before using it.
#if __has_include(<stdbit.h>)lets you fall back to builtins or portable code, as the first example does. It is provided by recent C libraries, not by older ones like the runner's. stdc_count_onesreplaces__builtin_popcount(GCC and Clang) and__popcnt(MSVC);stdc_leading_zerosreplaces__builtin_clz, which was undefined for 0, while the C23 function returns the full bit width.- Signed types are not accepted. Cast to the unsigned type first, and mind sign extension:
stdc_count_ones((unsigned)-1)counts the bits ofunsigned int, not of a wider value. - Fast on real hardware. Compilers turn these into single instructions (
popcnt,lzcnt,tzcnt) when the target supports them. - Kernighan's loop (
x &= x - 1) in the fallback runs once per set bit, so it is fast for sparse values and slower than the instruction for dense ones. - Bit-width of
size_t: usestdc_bit_widthon asize_tto compute log2 of table sizes, andstdc_bit_ceilto size hash tables to a power of two. - Standard version: C23. In C++ the equivalents are
std::popcount,std::bit_widthand friends in<bit>(C++20).