| 在标头 <bit> 定义
|
||
| |
(C++29 起) | |
将 x 中长度为 l 的位模式重复填充至结果所能容纳的最大次数(最后一次重复可能会被截断)。
如果 l 小于或等于 0,则行为未定义。仅在不会发生未定义行为的情况下,才允许在常量求值中调用此函数。
此重载只有在T 是无符号整数类型(即 unsigned char、unsigned short、unsigned int、unsigned long、unsigned long long,或扩展的无符号整数类型)时才会参与重载决议。
参数
| x | - | 无符号整数类型的值 |
返回值
重复后的位模式。
异常
不抛出异常。
注解
| 功能特性测试宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__cpp_lib_bitops |
202606L |
(C++29) | 位排列 |
可能的实现
template<typename T, typename ... U>
concept neither = (!std::same_as<T, U> && ...);
// 这是有效但效率很差的 bit_repeat 版本。
template<std::unsigned_integral T>
requires neither<T, bool, char, char8_t, char16_t, char32_t, wchar_t>
constexpr T bit_repeat_naive(T x, int l) noexcept
{
T result = 0;
for (int i = 0; i != std::numeric_limits<T>::digits; ++i)
result |= ((x >> (i % length)) & 1) << i;
return result;
}
// 常数时间实现(推荐)。
template<std::unsigned_integral T>
requires neither<T, bool, char, char8_t, char16_t, char32_t, wchar_t>
constexpr T bit_repeat(T x, int l) noexcept
{
static constexpr auto lookup = []
{
std::array<T, std::numeric_limits<T>::digits> result{};
for (std::size_t i = 1; i != result.size(); ++i)
result[i] = bit_repeat_naive(one, l);
return result;
}();
if (l >= std::numeric_limits<T>::digits)
return x;
T mask = (T{1} << l) - T{1};
return lookup[l] * (mask & x);
}
|
示例
运行此代码
#include <bit>
#include <cstdint>
static_assert(
std::bit_repeat(
std::uint8_t{1}, 1) ==
std::uint8_t{0b1111'1111} and
std::bit_repeat(
std::uint8_t{0b1110}, 2) ==
std::uint8_t{0b1010'1010} and
std::bit_repeat(
std::uint8_t{0b101}, 3) ==
std::uint8_t{0b1'101'101} and
std::bit_repeat(
std::uint16_t{0b1100}, 4) ==
std::uint16_t{0b1100'1100'1100'1100}
);
int main() {}
参阅
(C++29) |
反转整数中的位 (函数模板) |
(C++29) |
使用掩码压缩操作数的位(PEXT) (函数模板) |
(C++29) |
使用掩码从操作数扩展位(PDEP) (函数模板) |