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);}
ll lg2(const ll &x){return 63 - __builtin_clzll(x);}