1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-07-09 07:20:46 +00:00

AK: Fix log2 calculation for Integer

For a `N` bit integer X, the log2 calculation should be as follows:
(N - 1) - ctz(X)
This commit is contained in:
Pankaj Raghav 2022-01-29 15:26:43 +05:30 committed by Idan Horowitz
parent 0f010cee02
commit 24c66b8442

View File

@ -298,7 +298,7 @@ constexpr T log2(T x)
template<Integral T>
constexpr T log2(T x)
{
return x ? 8 * sizeof(T) - count_leading_zeroes(static_cast<MakeUnsigned<T>>(x)) : 0;
return x ? (8 * sizeof(T) - 1) - count_leading_zeroes(static_cast<MakeUnsigned<T>>(x)) : 0;
}
template<FloatingPoint T>