layout: post
title: “算法”
date: 2018-06-20 09:12
comments: true
tags:
- C/C++
- 算法
- 随笔
判断是否是2的幂
https://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/
https://blog.youkuaiyun.com/K346K346/article/details/53316953
Shift Right
int isPowerOfTwo (unsigned int x)
{
while (((x & 1) == 0) && x > 1) /* While x is even and > 1 */
x >>= 1;
return (x == 1);
}
Decrement and Compare
int isPowerOfTwo (unsigned int x)
{
return ((x != 0) && !(x & (x - 1)));
}
Complement and Compare
int isPowerOfTwo (unsigned int x)
{
return ((x != 0) && ((x & (~x + 1)) == x));

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



