# 数字范围按位与

题目链接: <https://leetcode.cn/problems/bitwise-and-of-numbers-range>

## 解题思路：

对于每个数字，将其与 `right` 进行按位与运算，将结果存储到 `right` 中。然后，将 `right` 减 `1`，再次进行循环。这样可以保证 `right` 中存储的是给定范围内所有数字的按位与

```go
func rangeBitwiseAnd(left int, right int) int {
	for left < right {
		right &= (right - 1)
	}

	return right
}
```

## 复杂度分析

* **时间复杂度：** 时间复杂度是 $$O(\log n)$$，其中 $$n$$ 是 `left` 和 `right` 的二进制表示中的位数
* **空间复杂度：** 空间复杂度是 $$O(1)$$


---

# 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://leetcodebook-1.gitbook.io/top-interview-150/bitwise/bitwise-and-of-numbers-range.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.
