小编Ant*_*Ant的帖子

在C++中测试一个数字是2的幂是最简单的方法是什么?

我需要一个这样的函数:

// return true iff 'n' is a power of 2, e.g.
// is_power_of_2(16) => true  is_power_of_2(3) => false
bool is_power_of_2(int n);
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议我怎么写这个?你能告诉我一个可以找到这种算法的好网站吗?

c++ algorithm bit-manipulation

86
推荐指数
8
解决办法
6万
查看次数

标签 统计

algorithm ×1

bit-manipulation ×1

c++ ×1