当前位置: C语言 -- 标准库 -- <stdbit.h> -- stdc_bit_width_ul

stdc_bit_width_ul函数


概要:
#include <stdbit.h>
unsigned int stdc_bit_width_ul(unsigned long int value) [[unsequenced]];

描述:

该函数计算存储参数value所需的最小位数(即表示参数value所需的二进制位数。);例如:如果参数value值为0X5CU,位宽(bit width)是7。

unsequenced类型属性表示:stdc_bit_width_ul函数是无状态、无副作用、幂等、并且是独立的。


参数:
unsigned long int value

参数为unsigned long int类型对象。


返回值:

如果参数value值为0,该函数返回值为0;否则函数返回 1 + ⌊ log 2 (value) ⌋ 。


范例:
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
/*函数stdc_bit_width_ul范例*/

#include <stdbit.h>
#include <stdio.h>

int main(void)
{
    unsigned long int value = 0X5CU;

    printf("The smallest number of bits needed to store value: %u\n", \
    stdc_bit_width_ul(value));

    return 0;
}

可能输出:

The smallest number of bits needed to store value: 7

(注:编译时实现未支持<stdbit.h>头文件。)


相关内容:
stdc_bit_width_uc 计算unsigned char类型对象位宽度的函数。
stdc_bit_width_ui 计算unsigned int类型对象位宽度的函数。
stdc_bit_width_us 计算unsigned short类型对象位宽度的函数。
stdc_bit_width_ull 计算unsigned long long int类型对象位宽度的函数。
stdc_bit_width 计算位宽度的泛型函数。


主要参考资料:

1、ISO/IEC 9899:2024