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

stdc_has_single_bit函数


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

描述:

当且仅当参数value中只有一个二进制位为1时,该函数返回真(true);否则该函数返回假(false);例如:如果参数value值为0X80U,函数返回值为真(true)。

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


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

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

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

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


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


参数:
generic_value_type value

参数为generic_value_type类型对象。


返回值:

当且仅当参数value中只有一个二进制位为1时,该函数返回真(true);否则该函数返回假(false)。


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

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

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

    if(stdc_has_single_bit(value))
      puts("There is a single 1 bit in value.");
    else
      puts("The number of 1 bits is not equal to 1.");

    return 0;
}

可能输出:

There is a single 1 bit in value.

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


相关内容:
stdc_has_single_bit_uc unsigned char类型对象单个1位检查的函数。
stdc_has_single_bit_us unsigned short类型对象单个1位检查的函数。
stdc_has_single_bit_ui unsigned int类型对象单个1位检查的函数。
stdc_has_single_bit_ul unsigned long int类型对象单个1位检查的函数。
stdc_has_single_bit_ull unsigned long long int类型对象单个1位检查的函数。


主要参考资料:

1、ISO/IEC 9899:2024