Created
April 27, 2012 10:41
-
-
Save jiixyj/2508296 to your computer and use it in GitHub Desktop.
calculate integer binary log star on compile time - C++11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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