# [leetcode] 231. Power of Two

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

Solution 1

Idea: divide n by 2, and check the residual.

class Solution {
public:
bool isPowerOfTwo(int n) {
while (n!=0 && n%2==0){
n = n/2;
}
return n==1;

}
};

Solution 2

Idea: 1 -- 1, 2 -- 10, 4 -- 100,... the highest bits are all 1. If minus 1, the left bit become 0 ,and others become 1s. Do bit logic and, should be 0;

class Solution {
public:
bool isPowerOfTwo(int n) {
return (n>0) && ((n&(n-1))==0);
}
};

• 本文已收录于以下专栏：

## LeetCode 231. Power of Two 解题报告

LeetCode 231. Power of Two 解题报告

## Leetcode 231. Power of Two & 326. Power of Three & 342. Power of Four

231. Power of Two   Total Accepted: 69528 Total Submissions: 190898 Difficulty: Easy Given an ...

## leetcode第231题：Power of Two

ProblemGiven an integer, write a function to determine if it is a power of two.题目给出一个整数，写一个函数判断它是否是2...

## leetcode- Number of 1 bits, power of two, power of three

leetcode- Number of 1 bits, power of two, power of three
• aaasy
• 2016-04-23 22:13
• 73

## Two dimensional power spectral density measurements of X-ray

• 2013-09-23 11:29
• 3.22MB
• 下载

## Round Up To Power Of Two

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)