Skip to content

Commit

Permalink
Update 01.Bit-Operation.md
Browse files Browse the repository at this point in the history
  • Loading branch information
itcharge committed May 13, 2024
1 parent f052f6a commit cdcd526
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions Contents/09.Algorithm-Base/06.Bit-Operation/01.Bit-Operation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
在学习二进制数的位运算之前,我们先来了解一下什么叫做「二进制数」。

![](https://qcdn.itcharge.cn/images/20230225233101.png)
![二进制数](https://qcdn.itcharge.cn/images/202405132135165.png)

> **二进制数(Binary)**:由 $0$ 和 $1$ 两个数码来表示的数。二进制数中每一个 $0$ 或每一个 $1$ 都称为一个「位(Bit)」。
Expand All @@ -29,7 +29,7 @@

同理,在二进制数中,$01101010_{(2)}$ 可以看作为 $(0 \times 2^7) + (1 \times 2^6) + (1 \times 2^5) + (0 \times 2^4) + (1 \times 2^3) + (0 \times 2^2) + (1 \times 2^1) + (0 \times 2^0)$,即 $0 + 64 + 32 + 0 + 8 + 0 + 2 + 0 = 106_{(10)}$。

![](https://qcdn.itcharge.cn/images/20230225233152.png)
![二进制数转十进制数](https://qcdn.itcharge.cn/images/202405132136456.png)

我们可以通过这样的方式,将一个二进制数转为十进制数。

Expand Down Expand Up @@ -82,7 +82,7 @@ $\begin{aligned} 106 \div 2 = 53 & \text{(余 0)} \cr 53 \div 2 = 26 & \text

举个例子,对二进制数 $01111100_{(2)}$ 与 $00111110_{(2)}$ 进行按位与运算,结果为 $00111100_{(2)}$,如图所示:

![](https://qcdn.itcharge.cn/images/20230225233202.png)
![按位与运算](https://qcdn.itcharge.cn/images/202405132137023.png)

### 2.2 按位或运算

Expand All @@ -97,7 +97,7 @@ $\begin{aligned} 106 \div 2 = 53 & \text{(余 0)} \cr 53 \div 2 = 26 & \text

举个例子,对二进制数 $01001010_{(2)}$ 与 $01011011_{(2)}$ 进行按位或运算,结果为 $01011011_{(2)}$,如图所示:

![](https://qcdn.itcharge.cn/images/20230225233231.png)
![按位或运算](https://qcdn.itcharge.cn/images/202405132137593.png)

### 2.3 按位异或运算

Expand All @@ -115,7 +115,7 @@ $\begin{aligned} 106 \div 2 = 53 & \text{(余 0)} \cr 53 \div 2 = 26 & \text

举个例子,对二进制数 $01001010_{(2)}$ 与 $01000101_{(2)}$ 进行按位异或运算,结果为 $00001111_{(2)}$,如图所示:

![](https://qcdn.itcharge.cn/images/20230225233240.png)
![按位异或运算](https://qcdn.itcharge.cn/images/202405132137874.png)

### 2.4 取反运算

Expand All @@ -127,21 +127,21 @@ $\begin{aligned} 106 \div 2 = 53 & \text{(余 0)} \cr 53 \div 2 = 26 & \text

举个例子,对二进制数 $01101010_{(2)}$ 进行取反运算,结果如图所示:

![](https://qcdn.itcharge.cn/images/20230225233257.png)
![取反运算](https://qcdn.itcharge.cn/images/202405132138853.png)

### 2.5 左移运算和右移运算

> **左移运算(SHL)**: 左移运算符为 `<<`。其功能是对一个二进制数的各个二进位全部左移若干位(高位丢弃,低位补 $0$)。
举个例子,对二进制数 $01101010_{(2)}$ 进行左移 $1$ 位运算,结果为 $11010100_{(2)}$,如图所示:

![](https://qcdn.itcharge.cn/images/20230225233308.png)
![左移运算](https://qcdn.itcharge.cn/images/202405132138841.png)

> **右移运算(SHR)**: 右移运算符为 `>>`。其功能是对一个二进制数的各个二进位全部右移若干位(低位丢弃,高位补 $0$)。
举个例子,对二进制数 $01101010_{(2)}$ 进行右移 $1$ 位运算,结果为 $00110101_{(2)}$,如图所示:

![](https://qcdn.itcharge.cn/images/20230225233317.png)
![右移运算](https://qcdn.itcharge.cn/images/202405132138348.png)

## 3. 位运算的应用

Expand Down

0 comments on commit cdcd526

Please sign in to comment.