h-stdbit
Table of Contents
<stdbit.h>
<stdbit.h> provides standard bit manipulation (C23): stdc_popcount (population count), stdc_leading_zeros, stdc_trailing_zeros, stdc_rotl, stdc_rotr. Previously you needed GCC builtins or platform-specific code.
Use it for portable bit operations without compiler-specific extensions.
Example
This example demonstrates bit counting and rotation on integers.
// compile: gcc -std=c2x -o stdbitexample stdbitexample.c // run: ./stdbitexample // description: bit manipulation functions #include <stdbit.h> #include <stdio.h> int main() { unsigned int x = 0b11010011; printf("popcount(0b11010011) = %u\n", stdc_popcount(x)); printf("leading zeros = %u\n", stdc_leading_zeros(x)); printf("trailing zeros = %u\n", stdc_trailing_zeros(x)); printf("rotl(x, 2) = 0x%x\n", stdc_rotl(x, 2)); return 0; }
Common functions
Bit counting:
stdc_popcount(x): count set bits (population count)stdc_popcount_uc(x),stdc_popcount_us(x),stdc_popcount_ui(x),stdc_popcount_ul(x),stdc_popcount_ull(x): type-specific variants
Leading/trailing zeros:
stdc_leading_zeros(x): count leading zero bitsstdc_trailing_zeros(x): count trailing zero bits- Type-specific variants:
_uc,_us,_ui,_ul,_ull
Bit rotation:
stdc_rotl(x, n): rotate left by n bitsstdc_rotr(x, n): rotate right by n bits- Type-specific variants:
_uc,_us,_ui,_ul,_ull
Bit flag checks:
stdc_has_single_bit(x): check if exactly one bit is set (is power of 2)
h-stdbit.md · Last modified: by 127.0.0.1
