> For the complete documentation index, see [llms.txt](https://dfine.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dfine.gitbook.io/leetcode/231.power_of_two.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
