首页 > 其他分享 >Power of Two

Power of Two

时间:2022-12-13 16:06:47浏览次数:51  
标签:false Power int Two two bool isPowerOfTwo return


​https://leetcode.com/problems/power-of-two/​

Given an integer, write a function to determine if it is a power of two.

数字 2^n 是大于0的,而且等于1左移n位得到的数字,所以2^n与2^n-1 相与运算得到0.

bool isPowerOfTwo(int n) {
if(n <= 0)
return false;

if((n & (n-1)) == 0)
return true;

return false;
}
bool isPowerOfTwo(int n) {
return n > 0 && !(n & (n-1));
}


标签:false,Power,int,Two,two,bool,isPowerOfTwo,return
From: https://blog.51cto.com/u_15911341/5934356

相关文章