Site Tools


wiki: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;
}
wiki/h-stdbit.md · Last modified: by 127.0.0.1