如何用一句语句判断一个数是否是power of 2?(2的指数幂)
从网上看到的,想了很久,没招。
哪个牛人给说说?
a***x 发帖数: 26368
2
Determining if an integer is a power of 2
unsigned int v; // we want to see if v is a power of 2
bool f; // the result goes here
f = (v & (v - 1)) == 0;
Note that 0 is incorrectly considered a power of 2 here. To remedy this, use:
f = !(v & (v - 1)) && v;