<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.
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; }