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

stdc_trailing_ones函数


概要:
#include <stdbit.h>
generic_return_type stdc_trailing_ones(generic_value_type value) [[unsequenced]];

描述:

该函数计算从最低有效位开始,参数value中连续1位的个数;例如:如果参数value类型为unsigned char类型,值为0X07U,尾随1的个数是3。

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


stdc_trailing_ones泛型函数根据参数value的类型返回相应值,参数value的类型应为以下类型之一:

-- 标准无符号整数类型(不包括bool类型)。

-- 扩展无符号整数类型。

-- 位精确无符号整数类型,其宽度与标准整数类型或者扩展整数类型相匹配,但不包括bool类型。

泛型返回类型(generic_return_type)应是合适的、足够大的无符号整数类型,应能表示函数返回结果。


ISO/IEC 9899标准未指定stdc_trailing_ones是宏还是具有外部链接的标识符。如果为访问实际函数而抑制宏定义,或者程序定义了与stdc_trailing_ones同名的外部标识符,其行为是未定义的。


参数:
generic_value_type value

参数为generic_value_type类型对象。


返回值:

该函数返回从最低有效位开始,参数value中连续1位的个数。


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

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

int main(void)
{
    unsigned char value = 0X07U;

    printf("The number of consecutive 1 bits: %u\n", stdc_trailing_ones(value));

    return 0;
}

可能输出:

The number of consecutive 1 bits: 3

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


相关内容:
stdc_trailing_ones_us 计算unsigned short类型对象尾随1的函数。
stdc_trailing_ones_ui 计算unsigned int类型对象尾随1的函数。
stdc_trailing_ones_ul 计算unsigned long int类型对象尾随1的函数。
stdc_trailing_ones_ull 计算unsigned long long int类型对象尾随1的函数。
stdc_trailing_ones_uc 计算unsigned char类型对象尾随1的函数。


主要参考资料:

1、ISO/IEC 9899:2024