aboutsummaryrefslogtreecommitdiff
path: root/snippets/min_pow_of_two.h
blob: ef92b4117a5b0fedf52d2f6886aa5c5c83f6d5f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once

#include <bit>
#include <concepts>
#include <limits>

template <std::integral Int>
static inline constexpr int log_min_pow_of_two(Int n) {
  using UInt = std::make_unsigned_t<Int>;
  return std::numeric_limits<UInt>::digits -
         std::countl_zero(static_cast<UInt>(n));
}

template <std::integral Int> static inline constexpr Int min_pow_of_two(Int n) {
  return std::make_unsigned_t<Int>(1) << log_min_pow_of_two(n);
}