| STDC_BIT_WIDTH(3C) | Standard C Library Functions | STDC_BIT_WIDTH(3C) |
stdc_bit_width,
stdc_bit_width_uc,
stdc_bit_width_us,
stdc_bit_width_ui,
stdc_bit_width_ul,
stdc_bit_width_ull —
determine minimum bit width
Standard C Library (libc, -lc)
#include
<stdbit.h>
unsigned int
stdc_bit_width(generic_value_type
value);
unsigned int
stdc_bit_width_uc(unsigned char
value);
unsigned int
stdc_bit_width_us(unsigned short
value);
unsigned int
stdc_bit_width_ui(unsigned int
value);
unsigned int
stdc_bit_width_ul(unsigned long
value);
unsigned int
stdc_bit_width_ull(unsigned long long
value);
The
stdc_bit_width()
family of functions returns the minimum number of bits required to store
value. This is calculated by taking the log base 2 of
value and adding 1; however, if
value is 0, then the function returns 0. The
stdc_bit_width() function is generic and will
operate on all 8, 16, 32, and 64-bit unsigned integers; however, it is only
available in C23. The other functions all operate on a specific integer
type, but otherwise behave the same and are available regardless of the C
language version.
The functions in the stdc_bit_width()
family returns the minimum number of bits required to hold
value. These functions cannot fail.
Example 1 Printing the minimum bit width.
#include <stdbit.h>
#include <stdio.h>
#include <limits.h>
int
main(void)
{
printf("0x%x 0x%x 0x%x 0x%x\n",
stdc_bit_width_uc(0x95),
stdc_bit_width_us(0xff6),
stdc_bit_width_ui(UINT32_MAX),
stdc_bit_width_ull(0));
return (0);
}
When compiled and run, this produces:
$ ./a.out 0x8 0xc 0x20 0x0
stdc_bit_ceil(3C), stdc_bit_floor(3C), stdc_count_ones(3C), stdc_count_zero(3C), stdc_first_leading_one(3C), stdc_first_leading_zero(3C), stdc_first_trailing_one(3C), stdc_first_trailing_zero(3C), stdc_has_single_bit(3C), stdc_leading_ones(3C), stdc_leading_zeros(3C), stdc_trailing_ones(3C), stdc_trailing_zeros(3C), stdbit.h(3HEAD)
| October 27, 2024 | OmniOS |