Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

<stdbit.h> — bit operations and byte order

The C23 header <stdbit.h> provides bit operations and macros describing the execution environment's byte order.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <stdbit.h>

<stdbit.h> is a C23 header providing bit utilities, including stdc_count_ones, which counts 1 bits in an unsigned value. For 13u, the result is 3.

__STDC_ENDIAN_LITTLE__ means the least significant byte occupies the lowest address; __STDC_ENDIAN_BIG__ means the most significant does. __STDC_ENDIAN_NATIVE__ equals the corresponding macro, or differs from both for another implementation-defined order. These are integer constant expressions; compare the macros rather than assuming numeric values.

The description covers bit-precise, standard, and extended integer types without padding bits; it does not establish floating-point byte order. The macros describe ordering but do not convert data. The repository's C23 flag does not attest production runtime library support.

Examples

#include <stdio.h>

#if defined(__has_include)
#if __has_include(<stdbit.h>)
#include <stdbit.h>
#define COUNT_ONES(value) stdc_count_ones(value)
#endif
#endif

#ifndef COUNT_ONES
static unsigned count_ones(unsigned value)
{
    unsigned result = 0;
    while (value != 0) {
        result += value & 1u;
        value >>= 1;
    }
    return result;
}
#define COUNT_ONES(value) count_ones(value)
#endif

int main(void)
{
    printf("ones: %u\n", COUNT_ONES(13u));
    return 0;
}

Result

ones: 3

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources