awtk/docs/fscript_bits.md
2022-08-23 10:12:36 +08:00

196 lines
1.9 KiB
Markdown

## 位操作扩展函数
### 1. &
> 位与运算。
----------------------------
#### 原型
```js
n1 & n2
```
#### 示例
```js
print(1 & 1)
```
### 2. |
> 位或运算。
----------------------------
#### 原型
```js
n1 | n2
```
#### 示例
```js
print(|(1, 2))
```
### 3.~
> 按位取反运算。
----------------------------
#### 原型
```js
~(n1)
```
#### 示例
```js
print(~1)
```
### 4.^
> 异或运算。
----------------------------
#### 原型
```js
n1^n2
```
#### 示例
```js
print(n1^n2)
```
### 5. <<
> 左移
----------------------------
#### 原型
```js
n1<<n2
```
#### 示例
```js
print(n1<<n2)
```
### 6. >>
> 右移
----------------------------
#### 原型
```js
n1>>n2
```
#### 示例
```js
print(n1>>n2)
```
### 7. bit_get
> 获取指定的位
----------------------------
#### 原型
```js
bit_get(n1, n2) => bool
```
#### 示例
```js
print(bit_get(0b1010, 1))
```
### 8. bit_set
> 设置指定的位
----------------------------
#### 原型
```js
bit_set(n1, n2) => value
```
#### 示例
```js
print(bit_set(0b1010, 0))
```
### 9. bit_clear
> 清除指定的位
----------------------------
#### 原型
```js
bit_clear(n1, n2) => value
```
#### 示例
```js
print(bit_clear(0b1010, 1))
```
### 10. bit_toggle
> 取反指定的位
----------------------------
#### 原型
```js
bit_toggle(n1, n2) => value
```
#### 示例
```js
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)
```