> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/bu-chong-6180-dao/190.-reverse-bits-bit.md).

# 190. Reverse Bits(bit)

\# Easy

{% hint style="info" %}
输入是数字，没有bit这种类型，所以只能左移和右移来操作

让最低位和最高位互换，就是先获取低位然后通过左移至高位，需要遍历所有的位
{% endhint %}

```python
class Solution:
    def reverseBits(self, n: int) -> int:
        res = 0
        power = 31
        while n:
            res += (n & 1) << power
            n = n >> 1
            power -= 1
        return res
```

Time = $$O(1)$$ , space = $$O(1)$$ .
