skip to content
Ivan's wiki
Site Tools
Search
Tools
Show pagesource
Backlinks
Recent Changes
Media Manager
Sitemap
>
Recent Changes
Media Manager
Sitemap
h-stdbit
This page is read only. You can view the source, but not change it.
# <stdbit.h> **[<stdbit.h>](https://en.cppreference.com/w/c/header/stdbit)** 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. ```c // 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 bits - `stdc_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 bits - `stdc_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)
Page Tools
Show pagesource
Backlinks
Back to top