# 231.Power of Two

**231.Power of Two**

难度:Easy

> 给定一个整数，编写一个函数来判断它是否是 2 的幂次方。

```
示例 1:

输入: 1
输出: true
解释: 20 = 1
示例 2:

输入: 16
输出: true
解释: 24 = 16
示例 3:

输入: 218
输出: false
```

直接位运算，二的整数次幂，必然只有一个位上是1，其他位为零。

```
class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n==0) return false;
        while((n&1) ==0)
        {
            n>>=1;
        }
     //   cout<<n<<endl;
        return n==1;
    }
};
```

> 执行用时 :8 ms, 在所有 C++ 提交中击败了60.55%的用户\
> 内存消耗 :8.1 MB, 在所有 C++ 提交中击败了27.77%的用户


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/231.power_of_two.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
