> 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/useful-knowledge-about-python/bit-operation.md).

# Bit operation

* n << 1 向左移位，低位变高位，低位0补齐
* n >> 1 向右移位，高位变低位
* n & 1 得到最低位
* (n >> 31) & 1 得到最高位
* (n & 1) << 31 最低位和最高位互换

{% hint style="danger" %}
一个数最多左移或右移31位，然后留下最后一个bit，再移一定是0
{% endhint %}

unsigned integer range: 0 \~ (2^32-1) = 0 \~ (1 << 32 - 1)

signed integer range: -2^31 \~ (2^31 - 1) = (1 << 32) \~ (1 << 31 - 1)

if n < (1 << 31), n is positive; else n is negative, convert unsigned n to signed n:

`unsigned_integer =` `signed_integer+(1` `<< 32)`
