函数
08/28/2020
本文内容
标头包括下列非成员模板函数:
非成员函数
说明
重新解释从一种类型到另一种类型的对象表示形式。
查找两个大于或等于某个值的最小幂。
查找两个 not 大于值的最大幂。
查找表示一个值所需的最小位数。
计算从最高有效位开始,将连续位数设置为零。
从最高有效位开始,计算设置为1的连续位的数量。
计算从最小有效位开始,将连续位数设置为零。
从最小有效位开始,计算设置为1的连续位的数量。
检查某个值是否只有一位设置为1位。 这与测试值是否为2的幂相同。
计算设置为1的位数。
计算按位左旋转的结果。
计算按位右旋转的结果。
bit_cast
将位模式从类型的对象复制 From 到类型的新对象 To 。
template
[[nodiscard]] constexpr To bit_cast(const From& from) noexcept;
参数
自
输出的类型。
从
要转换的值的类型。
从
要转换的值。
返回值
一个 To 类型的对象。
结果中的每个位都与中的相应位匹配 from ,除非中有填充位 To ,在这种情况下,结果中的位是未指定的。
示例
#include
#include
int main()
{
float f = std::numeric_limits::infinity();
int i = std::bit_cast(f);
std::cout << "float f = " << std::hex << f
<< "\nstd::bit_cat(f) = " << std::hex << i << '\n';
return 0;
}
float f = inf
std::bit_cat(f) = 7f800000
注解
低级别代码通常需要将一种类型的对象解释为另一种类型。 重新解释对象与原始对象具有相同的位表示形式,但它是不同的类型。
不使用 reinterpret_cast 或 memcpy() , bit_cast() 是进行这些转换的更好方法。 更好的原因是:
bit_cast() 是 constexpr
bit_cast() 要求完全复制和大小相同的类型。 这会阻止你使用和可能遇到的潜在问题, reinterpret_cast memcpy 因为这些问题可能会被错误地用于转换非完全复制类型。 此外,还可 memcpy() 用于在不相同大小的类型之间进行意外复制。 例如,double (8 字节) 为无符号 int (4 个字节) ,或其他方法。
此重载仅在以下情况下参与重载决策:
sizeof(To) == sizeof(From)
constexpr当且仅当 To 、和子其子 From 类型的类型为时,此函数模板为:
不是联合或指针类型
不是指向成员类型的指针
非易失性
不具有作为引用类型的非静态数据成员
bit_ceil
查找两个大于或等于某个值的最小幂。 例如,给定 3 ,返回 4 。
template
[[nodiscard]] constexpr T bit_ceil(T value);
参数
负值
要测试的无符号整数值。
返回值
两个大于或等于的最小幂 value 。
示例
#include
#include
#include
int main()
{
for (auto i = 0u; i < 6u; ++i) // bit_ceil() takes an unsigned integer type
{
auto nextClosestPowerOf2 = std::bit_ceil(i);
std::cout << "\nbit_ceil(0b" << std::bitset<4>(i) << ") = "
<< "0b" << std::bitset<4>(nextClosestPowerOf2);
}
return 0;
}
bit_ceil(0b0000) = 0b0001
bit_ceil(0b0001) = 0b0001
bit_ceil(0b0010) = 0b0010
bit_ceil(0b0011) = 0b0100
bit_ceil(0b0100) = 0b0100
bit_ceil(0b0101) = 0b1000
注解
如果 T 是无符号整数类型,则此模板函数仅参与重载决策。 例如: unsigned int 、、 unsigned long 、 unsigned short 等 unsigned char 。
bit_floor
查找两个 not 大于值的最大幂。 例如,给定 5 ,返回 4 。
template< class T >
[[nodiscard]] constexpr T bit_floor(T value) noexcept;
参数
负值
要测试的无符号整数值。
返回值
2的最大幂,不大于 value 。
如果 value 为零,则返回零。
示例
#include
#include
#include
int main()
{
for (auto i = 0u; i < 6u; ++i) // bit_floor() takes an unsigned integer type
{
auto previousPowerOf2 = std::bit_floor(i);
std::cout << "\nbit_floor(0b" << std::bitset<4>(i) << ") = 0b"
<< std::bitset<4>(previousPowerOf2);
}
return 0;
}
bit_floor(0b0000) = 0b0000
bit_floor(0b0001) = 0b0001
bit_floor(0b0010) = 0b0010
bit_floor(0b0011) = 0b0010
bit_floor(0b0100) = 0b0100
bit_floor(0b0101) = 0b0100
注解
如果 T 是无符号整数类型,则此模板函数仅参与重载决策。 例如: unsigned int 、、 unsigned long 、 unsigned short 等 unsigned char 。
bit_width
查找表示一个值所需的最小位数。
例如,给定 5 (0b101) ,返回3,因为它使用3个二进制位表示值5。
template
[[nodiscard]] constexpr T bit_width(T value) noexcept;
参数
负值
要测试的无符号整数值。
返回值
要表示的所需的位数 value 。
如果 value 为零,则返回零。
示例
#include
#include
int main()
{
for (unsigned i=0u; i <= 8u; ++i)
{
std::cout << "\nbit_width(" << i << ") = "
<< std::bit_width(i);
}
return 0;
}
bit_width(0) = 0
bit_width(1) = 1
bit_width(2) = 2
bit_width(3) = 2
bit_width(4) = 3
bit_width(5) = 3
bit_width(6) = 3
bit_width(7) = 3
bit_width(8) = 4
注解
如果 T 是无符号整数类型,则此模板函数仅参与重载决策。 例如: unsigned int 、、 unsigned long 、 unsigned short 等 unsigned char 。
countl_zero
计算从最高有效位开始,将连续位数设置为零。
template
[[nodiscard]] constexpr int countl_zero(T value) noexcept;
参数
负值
要测试的无符号整数值。
返回值
从最高有效位开始的连续零位位数。
如果 value 为零,则为类型中的位数 value 。
示例
#include
#include
#include
int main()
{
for (unsigned char result = 0, i = 0; i < 9; i++)
{
std::cout << "\ncountl_zero(0b" << std::bitset<8>(result) << ") = " << std::countl_zero(result);
result = result == 0 ? 1 : result * 2;
}
return 0;
}
countl_zero(0b00000000) = 8
countl_zero(0b00000001) = 7
countl_zero(0b00000010) = 6
countl_zero(0b00000100) = 5
countl_zero(0b00001000) = 4
countl_zero(0b00010000) = 3
countl_zero(0b00100000) = 2
countl_zero(0b01000000) = 1
countl_zero(0b10000000) = 0
注解
如果 T 是无符号整数类型,则此模板函数仅参与重载决策。 例如: unsigned int 、、 unsigned long 、 unsigned short 等 unsigned char 。
countl_one
从最高有效位开始,计算设置为1的连续位的数量。
template
[[nodiscard]] constexpr int countl_one(T value) noexcept;
参数
负值
要测试的无符号整数值。
返回值
从最高有效位开始,设置为1的连续位的数目。
示例
#include
#include
#include
int main()
{
unsigned char value = 0;
for (unsigned char bit = 128; bit > 0; bit /= 2)
{
value |= bit;
std::cout << "\ncountl_one(0b" << std::bitset<8>(value) << ") = "
<< std::countl_one(value);
}
return 0;
}
countl_one(0b10000000) = 1
countl_one(0b11000000) = 2
countl_one(0b11100000) = 3
countl_one(0b11110000) = 4
countl_one(0b11111000) = 5
countl_one(0b11111100) = 6
countl_one(0b11111110) = 7
countl_one(0b11111111) = 8
注解
如果 T 是无符号整数类型,则此模板函数仅参与重载决策。 例如: unsigned int 、、 unsigned long 、 unsigned short 等 unsigned char 。
countr_zero
计算从最小有效位开始,将连续位数设置为零。
template
[[nodiscard]] constexpr int countr_zero(T value) noexcept;
参数
负值
要测试的无符号整数值。
返回值
从最小有效位开始的连续零位的数量。
如果 value 为零,则为类型中的位数 value 。
示例
#include
#include
#include
int main()
{
for (unsigned char result = 0, i = 0; i < 9; i++)
{
std::cout << "\ncountr_zero(0b" << std::bitset<8>(result) << ") = "
<< std::countr_zero(result);
result = result == 0 ? 1 : result * 2;
}
return 0;
}
countr_zero(0b00000000) = 8
countr_zero(0b00000001) = 0
countr_zero(0b00000010) = 1
countr_zero(0b00000100) = 2
countr_zero(0b00001000) = 3
countr_zero(0b00010000) = 4
countr_zero(0b00100000) = 5
countr_zero(0b01000000) = 6
countr_zero(0b10000000) = 7
注解
如果 T 是无符号整数类型,则此模板函数仅参与重载决策。 例如: unsigned int 、、 unsigned long 、 unsigned short 等 unsigned char 。
countr_one
从最小有效位开始,计算设置为1的连续位的数量。
template
[[nodiscard]] constexpr int countr_one(T value) noexcept;
参数
负值
要测试的无符号整数值。
返回值
从最小有效位开始,设置为1的连续位的数量。
示例
#include
#include
#include
int main()
{
unsigned char value = 0;
for (int bit = 1; bit <= 128; bit *= 2)
{
value |= bit;
std::cout << "\ncountr_one(0b" << std::bitset<8>(value) << ") = "
<< std::countr_one(value);
}
return 0;
}
countr_one(0b00000001) = 1
countr_one(0b00000011) = 2
countr_one(0b00000111) = 3
countr_one(0b00001111) = 4
countr_one(0b00011111) = 5
countr_one(0b00111111) = 6
countr_one(0b01111111) = 7
countr_one(0b11111111) = 8
注解
如果 T 是无符号整数类型,则此模板函数仅参与重载决策。 例如: unsigned int 、、 unsigned long 、 unsigned short 等 unsigned char 。
has_single_bit
检查某个值是否仅设置了一个位。这与测试值是否为2的幂相同。
template
[[nodiscard]] constexpr bool has_single_bit(T value) noexcept;
参数
负值
要测试的无符号整数值。
返回值
true 如果 value 只有一个位集,这也意味着 value 是2的幂。 否则为 false。
示例
#include
#include
#include
#include
int main()
{
for (auto i = 0u; i < 10u; ++i)
{
std::cout << "has_single_bit(0b" << std::bitset<4>(i) << ") = "
<< std::boolalpha << std::has_single_bit(i) << '\n';
}
return 0;
}
has_single_bit(0b0000) = false
has_single_bit(0b0001) = true
has_single_bit(0b0010) = true
has_single_bit(0b0011) = false
has_single_bit(0b0100) = true
has_single_bit(0b0101) = false
has_single_bit(0b0110) = false
has_single_bit(0b0111) = false
has_single_bit(0b1000) = true
has_single_bit(0b1001) = false
注解
如果 T 是无符号整数类型,则此模板函数仅参与重载决策。 例如: unsigned int 、、 unsigned long 、 unsigned short 等 unsigned char 。
popcount
计算在无符号整数值中设置为1的位的数目。
template
[[nodiscard]] constexpr int popcount(T value) noexcept;
参数
负值
要测试的无符号整数值。
返回值
在中设置为1的位数 value 。
示例
#include
#include
#include
int main()
{
for (unsigned char value = 0; value < 16; value++)
{
std::cout << "\npopcount(0b" << std::bitset<4>(value) << ") = "
<< std::popcount(value);
}
return 0;
}
popcount(0b0000) = 0
popcount(0b0001) = 1
popcount(0b0010) = 1
popcount(0b0011) = 2
popcount(0b0100) = 1
popcount(0b0101) = 2
popcount(0b0110) = 2
popcount(0b0111) = 3
popcount(0b1000) = 1
popcount(0b1001) = 2
popcount(0b1010) = 2
popcount(0b1011) = 3
popcount(0b1100) = 2
popcount(0b1101) = 3
popcount(0b1110) = 3
popcount(0b1111) = 4
注解
如果 T 是无符号整数类型,则此模板函数仅参与重载决策。 例如: unsigned int 、、 unsigned long 、 unsigned short 等 unsigned char 。
rotl
将无符号整数值的位数向左旋转指定的次数。 最左侧的位的 "过时" 位会旋转到最右侧。
template
[[nodiscard]] constexpr T rotl(T value, int s) noexcept;
参数
负值
要旋转的无符号整数值。
些
要执行的左旋转的数目。
返回值
value左转、时间的结果 s 。
如果 s 为零,则返回 value 。
如果 s 为负,则为 rotr(value, -s) 。 最右边的位的 "过时" 位会旋转到最左边的位。
示例
#include
#include
#include
int main()
{
unsigned char bits = 1;
for (int i = 0; i < 8; ++i)
{
std::cout << "rotl(0b" << std::bitset<8>(bits) << ", 1) = ";
bits = std::rotl(bits, 1);
std::cout << "0b" << std::bitset<8>(bits) << '\n';
}
std::cout << "rotl(0b" << std::bitset<8>(bits) << ",-1) = ";
bits = std::rotl(bits, -1);
std::cout << "0b" << std::bitset<8>(bits);
return 0;
}
rotl(0b00000001, 1) = 0b00000010
rotl(0b00000010, 1) = 0b00000100
rotl(0b00000100, 1) = 0b00001000
rotl(0b00001000, 1) = 0b00010000
rotl(0b00010000, 1) = 0b00100000
rotl(0b00100000, 1) = 0b01000000
rotl(0b01000000, 1) = 0b10000000
rotl(0b10000000, 1) = 0b00000001
rotl(0b00000001,-1) = 0b10000000
注解
如果 T 是无符号整数类型,则此模板函数仅参与重载决策。 例如: unsigned int 、、 unsigned long 、 unsigned short 等 unsigned char 。
rotr
将右侧的位数旋转 value 指定的次数。 最右边的位的 "过时" 位会旋转回最左端的位。
template
[[nodiscard]] constexpr T rotr(T value, int s) noexcept;
参数
负值
要旋转的无符号整数值。
些
要执行的右旋转的数目。
返回值
向右旋转的结果 value , s
如果 s 为零,则返回 value 。
如果 s 为负,则为 rotl(value, -s) 。 最左侧的位的 "过时" 位会旋转回最右端。
示例
#include
#include
#include
int main()
{
unsigned char bits = 128;
for (int i = 0; i < 8; ++i)
{
std::cout << "rotr(0b" << std::bitset<8>(bits) << ", 1) = ";
bits = std::rotr(bits, 1);
std::cout << "0b" << std::bitset<8>(bits) << '\n';
}
std::cout << "rotr(0b" << std::bitset<8>(bits) << ",-1) = ";
bits = std::rotr(bits, -1);
std::cout << "0b" << std::bitset<8>(bits);
return 0;
}
rotr(0b10000000, 1) = 0b01000000
rotr(0b01000000, 1) = 0b00100000
rotr(0b00100000, 1) = 0b00010000
rotr(0b00010000, 1) = 0b00001000
rotr(0b00001000, 1) = 0b00000100
rotr(0b00000100, 1) = 0b00000010
rotr(0b00000010, 1) = 0b00000001
rotr(0b00000001, 1) = 0b10000000
rotr(0b10000000,-1) = 0b00000001
注解
如果 T 是无符号整数类型,则此模板函数仅参与重载决策。 例如: unsigned int 、、 unsigned long 、 unsigned short 等 unsigned char 。
要求
标头:
命名空间: std
请参阅