Skip to content

Commit

Permalink
doc: update c_operators.md.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Oct 12, 2022
1 parent 1e08fb7 commit 8cfc328
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .idoc/.filesStat.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@
"birthtime": "2022-05-01T15:32:10.059Z"
},
"docs/c_operators.md": {
"atime": "2022-05-03T17:18:57.538Z",
"mtime": "2022-05-03T17:18:54.939Z",
"ctime": "2022-05-03T17:18:54.939Z",
"atime": "2022-10-12T16:15:42.747Z",
"mtime": "2022-10-12T16:15:42.714Z",
"ctime": "2022-10-12T16:15:42.714Z",
"birthtime": "2022-05-01T15:32:10.058Z"
},
"docs/c_output.md": {
Expand Down
55 changes: 54 additions & 1 deletion docs/c_operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ C 将运算符分为以下几组:
* [赋值运算符](#赋值运算符) (Assignment operators)
* [比较运算符](#比较运算符) (Comparison operators)
* [逻辑运算符](#逻辑运算符) (Logical operators)
* 位运算符 (Bitwise operators)
* [位运算符](#位运算符) (Bitwise operators)

## 算术运算符

Expand Down Expand Up @@ -108,6 +108,59 @@ printf("%d", x > y); // 返回 1(真),因为 5 大于 3
| \|\| | 或逻辑 | 如果其中一个语句为真,则返回真 | x < 5 \|\| x < 4 |
| ! | 非逻辑 | 反转结果,如果结果为真则返回假 | !(x < 5 && x < 10) |
## 位运算符
运算符 | 描述 | 实例
:- |:- |:-
`&` | 按位与操作,按二进制位进行"与"运算 | `(A & B)` 将得到 `12` 即为 0000 1100
`\|` | 按位或运算符,按二进制位进行"或"运算 | `(A \| B)` 将得到 `61` 即为 0011 1101
`^` | 异或运算符,按二进制位进行"异或"运算 | `(A ^ B)` 将得到 `49` 即为 0011 0001
`~` | 取反运算符,按二进制位进行"取反"运算 | `(~A)` 将得到 `-61` 即为 1100 0011
`<<` | 二进制左移运算符 | `A << 2` 将得到 `240` 即为 1111 0000
`>>` | 二进制右移运算符 | `A >> 2` 将得到 `15` 即为 0000 1111
下面的实例,了解 C 语言中所有可用的位运算符
```c
#include <stdio.h>
int main()
{
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - c 的值是 %d\n", c );
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - c 的值是 %d\n", c );
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - c 的值是 %d\n", c );
c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - c 的值是 %d\n", c );
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - c 的值是 %d\n", c );
c = a >> 2; /* 15 = 0000 1111 */
printf("Line 6 - c 的值是 %d\n", c );
}
```

当上面的代码被编译和执行时,它会产生下列结果:

```c
Line 1 - c 的值是 12
Line 2 - c 的值是 61
Line 3 - c 的值是 49
Line 4 - c 的值是 -61
Line 5 - c 的值是 240
Line 6 - c 的值是 15
```

## sizeof 运算符

可以使用 `sizeof` 运算符找到数据类型或变量的内存大小(以字节为单位):
Expand Down

0 comments on commit 8cfc328

Please sign in to comment.