“cpp/numeric/countl zero”的版本间的差异
来自cppreference.com
(+) |
YexuanXiao(讨论 | 贡献) 小 |
||
(未显示1个用户的6个中间版本) | |||
第1行: | 第1行: | ||
{{cpp/title|countl_zero}} | {{cpp/title|countl_zero}} | ||
− | {{cpp/ | + | {{cpp//navbar}} |
− | {{ | + | {{headerbit|since=c++20| |
− | + | template< class T > | |
− | + | constexpr int countl_zero( T x ) noexcept; | |
− | template<class T> | + | |
− | constexpr int countl_zero(T x) noexcept; | + | |
}} | }} | ||
− | |||
− | 返回 {{ | + | 返回 {{|x}} 的值中从最高位(“左”)起连续的为 0的位的数量。 |
− | {{cpp/enable_if| {{tt|T}} 为无符号整数类型(即 {{c|unsigned char}} | + | {{cpp/enable_if| {{tt|T}} 为无符号整数类型(即 {{c|unsigned char}}、{{c|unsigned short}}、{{c|unsigned int}}、{{c|unsigned long}}、{{c|unsigned long long}} 或扩展无符号整数类型)}}。 |
===参数=== | ===参数=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | x | | + | {{par|x|无符号整数}} |
{{par end}} | {{par end}} | ||
===返回值=== | ===返回值=== | ||
− | {{ | + | {{|x}} 的值中从最高位起连续的为 0的位的数量。 |
− | === | + | === |
+ | |||
+ | |||
+ | 示例=== | ||
{{example | {{example | ||
|code= | |code= | ||
第27行: | 第27行: | ||
#include <bitset> | #include <bitset> | ||
#include <cstdint> | #include <cstdint> | ||
− | |||
#include <iostream> | #include <iostream> | ||
int main() | int main() | ||
{ | { | ||
− | for (std::uint8_t i : { 0, 0b11111111, | + | for (std::uint8_t i : {0, 0b11111111, }) |
− | std::cout << "countl_zero( | + | std::cout << "countl_zero( " << std::bitset<8>(i) << " ) = " |
<< std::countl_zero(i) << '\n'; | << std::countl_zero(i) << '\n'; | ||
− | |||
} | } | ||
|output= | |output= | ||
− | countl_zero( | + | countl_zero( ) = 8 |
− | countl_zero( | + | countl_zero( ) = 0 |
− | countl_zero( | + | countl_zero( |
+ | ) = 3 | ||
}} | }} | ||
− | === | + | ===参阅=== |
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/numeric/dsc countl_one}} | + | {{dsc inc|cpp/numeric/dsc countl_one}} |
− | {{dsc inc | cpp/numeric/dsc countr_zero}} | + | {{dsc inc|cpp/numeric/dsc countr_zero}} |
− | {{dsc inc | cpp/numeric/dsc countr_one}} | + | {{dsc inc|cpp/numeric/dsc countr_one}} |
− | {{dsc inc | cpp/numeric/dsc popcount}} | + | {{dsc inc|cpp/numeric/dsc popcount |
+ | |||
+ | }} | ||
{{dsc end}} | {{dsc end}} | ||
− | {{langlinks|en|ja}} | + | {{langlinks|en|ja}} |
2025年5月1日 (四) 13:42的最后版本
在标头 <bit> 定义
|
||
template< class T > constexpr int countl_zero( T x ) noexcept; |
(C++20 起) | |
返回 x 的值中从最高位(“左”)起连续的为 0 的位的数量。
此重载只有在 T
为无符号整数类型(即 unsigned char、unsigned short、unsigned int、unsigned long、unsigned long long 或扩展无符号整数类型)时才会参与重载决议。
目录 |
[编辑] 参数
x | - | 无符号整数 |
[编辑] 返回值
x 的值中从最高位起连续的为 0 的位的数量。
[编辑] 注解
功能特性测试宏 | 值 | 标准 | 功能特性 |
---|---|---|---|
__cpp_lib_bitops |
201907L |
(C++20) | 位运算 |
[编辑] 示例
运行此代码
#include <bit> #include <bitset> #include <cstdint> #include <iostream> int main() { for (const std::uint8_t i : {0, 0b11111111, 0b11110000, 0b00011110}) std::cout << "countl_zero( " << std::bitset<8>(i) << " ) = " << std::countl_zero(i) << '\n'; }
输出:
countl_zero( 00000000 ) = 8 countl_zero( 11111111 ) = 0 countl_zero( 11110000 ) = 0 countl_zero( 00011110 ) = 3
[编辑] 参阅
(C++20) |
从最高位起计量连续的 1 位的数量 (函数模板) |
(C++20) |
从最低位起计量连续的 0 位的数量 (函数模板) |
(C++20) |
从最低位起计量连续的 1 位的数量 (函数模板) |
(C++20) |
计量无符号整数中为 1 的位的数量 (函数模板) |
检查是否所有位,有任何位或没有位被设为 true ( std::bitset<N> 的公开成员函数)
| |
stdc_leading_zeros 的 C 文档
|