Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Floor log2 n in O(1) for C++

Правка en2, от MohammadParsaElahimanesh, 2021-03-31 23:05:08

Hello, many times you need to calculate lg2(n) and usually you need just to calculate floor of that. may these functions help you.

/// there are in O(1) and they work for int and long long numbers that is greater than 0
int lg2(const int &x){return 31 - __builtin_clz(x);}
long long int lg2(const long long int &x){return 63 - __builtin_clzll(x);}
Теги log2(), compiler, c++ compilers

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский MohammadParsaElahimanesh 2021-03-31 23:05:08 22
en1 Английский MohammadParsaElahimanesh 2021-03-31 21:48:02 378 Initial revision (published)