三个月算法进阶--day43
目录
python //=
python math.log2() math.trunc(3.14)
位运算
leetcode第231题2的幂
获取二进制最右边的1:x & (-x)
-x 是 x 按位取反再加1,算 -x 先 x - 1 再按位取反
def isPowerOfTwo(self, n):
return n != 0 and n & (-n) == n
将二进制最右边的1设为0:x & (x - 1)
def isPowerOfTwo(self, n):
return n != 0 and not n & (n - 1)