2021-01-11 10:03:37 +08:00
|
|
|
## 位操作扩展函数
|
2021-01-10 09:58:41 +08:00
|
|
|
|
2021-01-11 10:03:37 +08:00
|
|
|
### 1. &
|
2021-01-10 09:58:41 +08:00
|
|
|
|
|
|
|
> 位与运算。
|
|
|
|
----------------------------
|
|
|
|
|
2021-01-11 10:03:37 +08:00
|
|
|
#### 原型
|
2021-01-10 09:58:41 +08:00
|
|
|
|
|
|
|
```
|
2021-01-11 10:03:37 +08:00
|
|
|
n1 & n2
|
2021-01-10 09:58:41 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```
|
2021-01-11 10:03:37 +08:00
|
|
|
print(1 & 1)
|
2021-01-10 09:58:41 +08:00
|
|
|
```
|
|
|
|
|
2021-01-11 10:03:37 +08:00
|
|
|
### 2. |
|
|
|
|
|
|
|
|
> 位或运算。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
2021-01-10 09:58:41 +08:00
|
|
|
|
|
|
|
```
|
2021-01-11 10:03:37 +08:00
|
|
|
1|2
|
2021-01-10 09:58:41 +08:00
|
|
|
```
|
|
|
|
|
2021-01-11 10:03:37 +08:00
|
|
|
### 示例
|
2021-01-10 09:58:41 +08:00
|
|
|
|
|
|
|
```
|
2021-01-11 10:03:37 +08:00
|
|
|
print(|(1, 2))
|
2021-01-10 09:58:41 +08:00
|
|
|
```
|
|
|
|
|
2021-01-11 10:03:37 +08:00
|
|
|
### 3.~
|
2021-01-10 09:58:41 +08:00
|
|
|
|
2021-01-11 10:03:37 +08:00
|
|
|
> 按位取反运算。
|
2021-01-10 09:58:41 +08:00
|
|
|
----------------------------
|
|
|
|
|
2021-01-11 10:03:37 +08:00
|
|
|
#### 原型
|
2021-01-10 09:58:41 +08:00
|
|
|
|
|
|
|
```
|
2021-01-11 10:03:37 +08:00
|
|
|
~(n1)
|
2021-01-10 09:58:41 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```
|
2021-01-11 10:03:37 +08:00
|
|
|
print(~1)
|
2021-01-10 09:58:41 +08:00
|
|
|
```
|
|
|
|
|
2021-01-11 10:03:37 +08:00
|
|
|
### 4.^
|
|
|
|
|
|
|
|
> 异或运算。
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
2021-01-10 09:58:41 +08:00
|
|
|
|
|
|
|
```
|
2021-01-11 10:03:37 +08:00
|
|
|
n1^n2
|
2021-01-10 09:58:41 +08:00
|
|
|
```
|
|
|
|
|
2021-01-11 10:03:37 +08:00
|
|
|
#### 示例
|
2021-01-10 09:58:41 +08:00
|
|
|
|
|
|
|
```
|
2021-01-11 10:03:37 +08:00
|
|
|
print(n1^n2)
|
2021-01-10 09:58:41 +08:00
|
|
|
```
|
|
|
|
|
2021-01-11 10:03:37 +08:00
|
|
|
### 5. <<
|
2021-01-10 09:58:41 +08:00
|
|
|
|
2021-01-11 10:03:37 +08:00
|
|
|
> 左移
|
2021-01-10 09:58:41 +08:00
|
|
|
----------------------------
|
|
|
|
|
2021-01-11 10:03:37 +08:00
|
|
|
#### 原型
|
2021-01-10 09:58:41 +08:00
|
|
|
|
|
|
|
```
|
2021-01-11 10:03:37 +08:00
|
|
|
n1<<n2
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```
|
|
|
|
print(n1<<n2)
|
|
|
|
```
|
|
|
|
|
|
|
|
### 6. >>
|
|
|
|
|
|
|
|
> 右移
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```
|
|
|
|
n1>>n2
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```
|
|
|
|
print(n1>>n2)
|
|
|
|
```
|
|
|
|
|
|
|
|
### 7. bit_get
|
|
|
|
|
|
|
|
> 获取指定的位
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```
|
2021-01-13 13:48:59 +08:00
|
|
|
bit_get(n1, n2) -> bool
|
2021-01-10 09:58:41 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```
|
2021-01-11 10:03:37 +08:00
|
|
|
print(bit_get(0b1010, 1))
|
|
|
|
```
|
|
|
|
|
|
|
|
### 8. bit_set
|
|
|
|
|
|
|
|
> 设置指定的位
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```
|
|
|
|
bit_set(n1, n2)
|
2021-01-10 09:58:41 +08:00
|
|
|
```
|
|
|
|
|
2021-01-11 10:03:37 +08:00
|
|
|
#### 示例
|
2021-01-10 09:58:41 +08:00
|
|
|
|
|
|
|
```
|
2021-01-11 10:03:37 +08:00
|
|
|
print(bit_set(0b1010, 0))
|
2021-01-10 09:58:41 +08:00
|
|
|
```
|
|
|
|
|
2021-01-11 10:03:37 +08:00
|
|
|
### 9. bit_clear
|
2021-01-10 09:58:41 +08:00
|
|
|
|
2021-01-11 10:03:37 +08:00
|
|
|
> 清除指定的位
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```
|
|
|
|
bit_clear(n1, n2)
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```
|
|
|
|
print(bit_clear(0b1010, 1))
|
2021-01-10 09:58:41 +08:00
|
|
|
```
|
2021-01-11 10:03:37 +08:00
|
|
|
|
|
|
|
### 10. bit_toggle
|
|
|
|
|
|
|
|
> 取反指定的位
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
#### 原型
|
|
|
|
|
|
|
|
```
|
|
|
|
bit_toggle(n1, n2)
|
|
|
|
```
|
|
|
|
|
|
|
|
#### 示例
|
|
|
|
|
|
|
|
```
|
|
|
|
print(bit_toggle(0b1010, 1))
|
|
|
|
```
|
|
|
|
|
|
|
|
### 更多示例
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(bit_clear(0b0111, 1) == 0b0101)
|
|
|
|
assert(bit_clear(0b0111, 0) == 0b0110)
|
|
|
|
|
|
|
|
assert(bit_set(0b0, 0) == 0b1)
|
|
|
|
assert(bit_set(0b0, 1) == 0b10)
|
|
|
|
assert(bit_set(0b0, 2) == 0b100)
|
|
|
|
|
|
|
|
assert(bit_toggle(0b000, 0) == 0b1)
|
|
|
|
assert(bit_toggle(0b111, 1) == 0b101)
|
|
|
|
assert(bit_toggle(0b101, 2) == 0b1)
|
|
|
|
|
|
|
|
assert(bit_get(0b0111, 0))
|
|
|
|
assert(bit_get(0b0111, 1))
|
|
|
|
assert(!bit_get(0b0011, 2))
|
|
|
|
|
|
|
|
assert((1<<1) == 2)
|
|
|
|
assert((1<<2) == 4)
|
|
|
|
assert((1<<8) == 256)
|
|
|
|
assert(((0xf0112233 >> 24) & 0xff) == 0xf0)
|
2021-01-10 09:58:41 +08:00
|
|
|
```
|