如何在python2.5(严格)中取2加的倒数

Tau*_*uir 0 python mathematical-optimization

我有数说37。37 位于 之间2**5 and 2**6。我想编写一个函数,它将数字作为参数并返回其下界的幂。

def foo(x=37)
{
 result =//Here calculation
 return result
}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,回报应该是5因为它的下限2**5 and 5是它的力量。

注意:一定要关心python 2.5

提前致谢。

pay*_*yne 6

import math
def hi_bit(num):
        return int(math.log(num, 2))
Run Code Online (Sandbox Code Playgroud)

整数截断 base-2 log 函数将为您提供最高位集的索引。此索引N是数字的基数为 2 的指数下限,因为它必须大于或等于2**N