a*x
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;
【在 x**d 的大作中提到】![](/moin_static193/solenoid/img/up.png)
: 如何用一句语句判断一个数是否是power of 2?(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;
【在 x**d 的大作中提到】
![](/moin_static193/solenoid/img/up.png)
: 如何用一句语句判断一个数是否是power of 2?(2的指数幂)
: 从网上看到的,想了很久,没招。
: 哪个牛人给说说?
相关阅读
你软是不是又干蠢事了javafx命运如何ribbon怎么样?菜鸟问个python+http问题使用注解方式的 ORM 框架的性能不行吧Netflix又出问题了?对分布式软件的Jepsen测试For single node persistence/storage大家看了最近xcodeghost植入后门时间吗?连微信都被搞了急招湾区Android高手加盟startup哪位大牛 试过d3用在angular里[python] raise another exception in exception construct支付宝和手机银行什么的没看到中xcodeghostspring 是一个event loop一直在跑么?然后处理各种event?有没有谁做 text mining 的?BEA的庄思浩MyEclipse有人用吗?scala.js请问有没有大牛知道怎么绕过对虚拟打印机的限制?我来说说为什么女人大部分不适合CS吧