diff options
author | Tim Schumacher <timschumi@gmx.de> | 2023-05-13 20:04:00 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-05-17 09:08:53 +0200 |
commit | d194011570cca93324137e3c697e7f95f2638fe6 (patch) | |
tree | a342be523b1219245148a4f4aeeb719726bdee9d /AK | |
parent | 8434d7602786dd7752124f4f2649ff4990759177 (diff) | |
download | serenity-d194011570cca93324137e3c697e7f95f2638fe6.zip |
AK: Add `count_required_bits`
Diffstat (limited to 'AK')
-rw-r--r-- | AK/BuiltinWrappers.h | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/AK/BuiltinWrappers.h b/AK/BuiltinWrappers.h index 1bfdaa5c97..bd0620d1a8 100644 --- a/AK/BuiltinWrappers.h +++ b/AK/BuiltinWrappers.h @@ -150,12 +150,23 @@ inline constexpr int bit_scan_forward(IntType value) #endif } +// Counts the minimum number of bits required to represent the value (i.e. ignoring leading null bits). +template<Unsigned IntType> +inline constexpr size_t count_required_bits(IntType value) +{ + if (value == 0) + return 1; + + return 8 * sizeof(value) - count_leading_zeroes(value); +} + } #if USING_AK_GLOBALLY using AK::bit_scan_forward; using AK::count_leading_zeroes; using AK::count_leading_zeroes_safe; +using AK::count_required_bits; using AK::count_trailing_zeroes; using AK::count_trailing_zeroes_safe; using AK::popcount; |