Skip to content

Instantly share code, notes, and snippets.

@jiixyj
Created April 27, 2012 10:41
Show Gist options
  • Select an option

  • Save jiixyj/2508296 to your computer and use it in GitHub Desktop.

Select an option

Save jiixyj/2508296 to your computer and use it in GitHub Desktop.
calculate integer binary log star on compile time - C++11
#include <cinttypes>
#include <climits>
namespace {
typedef uintmax_t log_number_t;
constexpr unsigned count_bits(log_number_t n) {
return n == 0 ? 0 : count_bits(n >> 1) + 1;
}
constexpr unsigned lb_floor(log_number_t n) {
return n == 0 ? 0 : count_bits(n) - 1;
}
constexpr unsigned lb_ceil(log_number_t n) {
return n <= 1 ? 0 : lb_floor(n - 1) + 1;
}
constexpr unsigned log_star(log_number_t n) {
return n <= 2 ? 0 : log_star(lb_ceil(n)) + 1;
}
}
int main()
{
constexpr auto r = log_star(sizeof(unsigned short) * CHAR_BIT);
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment