# knowledge base

```
A number is a power of 2 if it's greater than 0 and its binary representation has only one '1'
```

```
def is_power_of_2(n):
    return n > 0 and (n & (n - 1)) == 0
```

This property is why the expression `n & (n - 1) == 0`&#x20;

* For n=21=2n = 2^1 = 2n=21=2, binary: `0010` → n−1=1n-1 = 1n−1=1, binary: `0001`
* For n=22=4n = 2^2 = 4n=22=4, binary: `0100` → n−1=3n-1 = 3n−1=3, binary: `0011`
* For n=23=8n = 2^3 = 8n=23=8, binary: `1000` → n−1=7n-1 = 7n−1=7, binary: `0111`



**数字转换**

在计算机领域，单位的换算分为**以 10 为底**和**以 2 为底**的两种常用方式。以下是常见的单位及其对应的换算关系，以及在十进制和二进制中的计算方法：

#### 1. 以 10 为底的单位（国际标准单位，SI 进制）

| 单位 | 英文单位     | 数值（以 10 为底）                                                | 对应的数量（英文）                       |
| -- | -------- | ---------------------------------------------------------- | ------------------------------- |
| KB | Kilobyte | (1 , KB = 10^3 , bytes = 1,000 , bytes)                    | One thousand (1 thousand)       |
| MB | Megabyte | (1 , MB = 10^6 , bytes = 1,000,000 , bytes)                | One million (1 million)         |
| GB | Gigabyte | (1 , GB = 10^9 , bytes = 1,000,000,000 , bytes)            | One billion (1 billion)         |
| TB | Terabyte | (1 , TB = 10^{12} , bytes = 1,000,000,000,000 , bytes)     | One trillion (1 trillion)       |
| PB | Petabyte | (1 , PB = 10^{15} , bytes = 1,000,000,000,000,000 , bytes) | One quadrillion (1 quadrillion) |

#### 2. 以 2 为底的单位（计算机传统，二进制进制）

| 单位  | 英文单位     | 数值（以 2 为底）                                                 | 对应的数量（英文）                                                                                                                                                            |
| --- | -------- | ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| KiB | Kibibyte | (1 , KiB = 2^{10} , bytes = 1,024 , bytes)                 | One thousand and twenty-four                                                                                                                                         |
| MiB | Mebibyte | (1 , MiB = 2^{20} , bytes = 1,048,576 , bytes)             | One million and forty-eight thousand five hundred seventy-six                                                                                                        |
| GiB | Gibibyte | (1 , GiB = 2^{30} , bytes = 1,073,741,824 , bytes)         | One billion and seventy-three million seven hundred forty-one thousand eight hundred twenty-four                                                                     |
| TiB | Tebibyte | (1 , TiB = 2^{40} , bytes = 1,099,511,627,776 , bytes)     | One trillion and ninety-nine billion five hundred eleven million six hundred twenty-seven thousand seven hundred seventy-six                                         |
| PiB | Pebibyte | (1 , PiB = 2^{50} , bytes = 1,125,899,906,842,624 , bytes) | One quadrillion one hundred twenty-five trillion eight hundred ninety-nine billion nine hundred six million eight hundred forty-two thousand six hundred twenty-four |

#### 对应的英文数字表达

* (10^3 = 1,000) - Thousand
* (10^6 = 1,000,000) - Million
* (10^9 = 1,000,000,000) - Billion
* (10^{12} = 1,000,000,000,000) - Trillion
* (10^{15} = 1,000,000,000,000,000) - Quadrillion

而在**二进制单位**中：

* (2^{10} = 1,024)
* (2^{20} = 1,048,576)
* (2^{30} = 1,073,741,824)
* (2^{40} = 1,099,511,627,776)
* (2^{50} = 1,125,899,906,842,624)

因此，**以 2 为底的单位**常用于表示更精确的内存容量和数据传输，而以 **10 为底的单位**常用于硬盘容量、网络流量等领域。



**成环问题**

数组扩充成2倍遍历，就模拟了成环遍历，结果只取数组原长度。

无需真的扩充数组，直接for 循环2倍数组长度, i = i mod (len(arr))
