bit_util: Add CountLeadingZeros
This commit is contained in:
parent
5d93e6e580
commit
aa225a7dc4
1 changed files with 11 additions and 0 deletions
|
@ -150,6 +150,17 @@ inline size_t BitCount(Integral value) {
|
||||||
return std::bitset<BitSize<Integral>()>(value).count();
|
return std::bitset<BitSize<Integral>()>(value).count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
constexpr size_t CountLeadingZeros(T value) {
|
||||||
|
auto x = static_cast<std::make_unsigned_t<T>>(value);
|
||||||
|
size_t result = BitSize<T>();
|
||||||
|
while (x != 0) {
|
||||||
|
x >>= 1;
|
||||||
|
result--;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
constexpr int HighestSetBit(T value) {
|
constexpr int HighestSetBit(T value) {
|
||||||
auto x = static_cast<std::make_unsigned_t<T>>(value);
|
auto x = static_cast<std::make_unsigned_t<T>>(value);
|
||||||
|
|
Loading…
Reference in a new issue