当前位置:网站首页>C traps and defects Chapter 3 semantic "traps" 3.8 operators &, |, and!
C traps and defects Chapter 3 semantic "traps" 3.8 operators &, |, and!
2022-07-29 03:03:00 【weixin_ Guest time】
Arithmetic &&、|| and !
C There are two kinds of logical operators in language , Sometimes it can be exchanged : Bitwise operators &、| and ~, And logical operators &&、|| and !.
If the programmer replaces the corresponding operator in the other class with an operator in one class , He might be surprised : After the exchange, the program seems to be able to “ normal ” Work , But in fact, this is just a coincidence .
Bitwise operators &、| and ~ Operands are treated as a binary sequence of bits , Align each bit separately for operation .
& The operator represents if and only if a bit of the binary representation of two operands is 1 when , In the binary representation of the final result, this bit is 1.
Logical operators &&、|| and ! The way to handle operands is : Or think of it as “ really ”, Or think of it as “ false ”, Instead of 0 Regard as “ really ”.
These operators when the result is “ really ” When to return to 1, When the result is “ false ” When to return to 0, They can only return 0 or 1. Except for the second ,
Operator && And operators || When the value of the left operand can determine the final result , The right operand is not evaluated at all .
Query a specific element in the table
i = 0;
while (i < tabsize && tab[i] != x) {
i++;
}
The intention is : If i be equal to tabsize Then the cycle ends , This indicates that the element to be found is not found in the table ; And in other cases , here i Is the index of the element you are looking for . Notice that asymmetric boundaries are used in this loop .
i = 0;
while (i < tabsize & tab[i] != x) {
i++;
}
This loop statement is also possible “ normal ” Work , But just because of two very lucky reasons .
first “ A fluke ” yes ,while The expression in & Both sides of the operator are comparison operations , The comparison result is “ really ” Is equal to 1, For “ false ” When is equal to the 0. as long as x and y All values of are limited to 0 or 1,x&y And x&&y Always get the same result . However , If either of the two comparison operators is divided 1 Other than 0 representative “ really ”, Then this cycle will not work properly .
the second “ A fluke ” yes , For the next element after the end of the array ( It doesn't really exist ), As long as the program does not change the value of this element , Just read its value , Generally, there will be no harm . Operator & And operators && Different , Operator & Both operands must be evaluated .
For the next element after the end of the array , It's legal to take its address . This is in the section , We try to actually read the value of this element , The result of this approach is undefined , And there are very few C The compiler can detect this problem .
边栏推荐
- C陷阱与缺陷 第3章 语义“陷阱” 3.8 运算符&&、||和!
- mysql大表联合查询优化,大事务优化,规避事务超时,锁等待超时与锁表
- Hangao database best practice configuration tool Hg_ BP log collection content
- HTB-Blue
- 【npm错误】- npm ERR code ERESOLVE 和 npm ERR ERESOLVE could not resolve 问题
- Flink kernel source code (VII) Flink SQL submission process
- C陷阱与缺陷 第3章 语义“陷阱” 3.6 边界计算与不对称边界
- C陷阱与缺陷 第3章 语义“陷阱” 3.4 避免“举偶法”
- 创客教育的起源和内涵的基本理念
- [QNX Hypervisor 2.2用户手册]9.11 ram(更新中)
猜你喜欢

12_ue4进阶_换一个更好看的人物模型

Li Shuo, vice president of Baidu: it's a good thing that China's labor costs rise with the support of digital technology

13_ue4进阶_蒙太奇动画实现一边走一边攻击

Chapter 2 VRP command line

Tp5.0 applet users do not need to log in and directly obtain the user's mobile number.
![[open the door to the new world] see how the old bird of testing plays API testing between applause](/img/79/1bc836cefef24d23e09d9865ff1fba.png)
[open the door to the new world] see how the old bird of testing plays API testing between applause
![[opencv] use OpenCV to call mobile camera](/img/66/6207bafbc9696e43da7a60a386a238.jpg)
[opencv] use OpenCV to call mobile camera

Verilog:阻塞赋值和非阻塞赋值

MySQL - count(字段)、count(主键)、count(1)、count(*)的区别

Confusion matrix learning notes
随机推荐
Redis configuration cache expiration listening event trigger
【C】数组
[NPM error] - NPM err code eresolve and NPM err eresolve could not resolve problems
多行文本省略
Day 5 experiment
Summary of classic problems in Flink production environment
cuda-gdb提示:/tmp/tmpxft_***.cudafe1.stub.c: No such file or directory.
C语言小项目 -- 通讯录(静态版+动态版+文件版)
MySQL 操作数据库数据报错:Fatal error encountered during command execution
Engineering boy: under 20 years old, ordinary but not mediocre
Linux下安装MySQL8.0的详细步骤
【FreeSwitch开发实践】media bug获取通话语音流
C陷阱与缺陷 第3章 语义“陷阱” 3.6 边界计算与不对称边界
Summary of common hooks
MySQL large table joint query optimization, large transaction optimization, avoiding transaction timeout, lock wait timeout and lock table
Verilog的时间系统任务----$time、$stime、$realtime
解读AI机器人养宠引领时尚潮流
MySQL - count(字段)、count(主键)、count(1)、count(*)的区别
Unable to start after idea installation
Hangao database best practice configuration tool Hg_ BP log collection content