当前位置:网站首页>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 .
边栏推荐
- Idea replaces the contents of all files
- cuda-gdb提示:/tmp/tmpxft_***.cudafe1.stub.c: No such file or directory.
- CentOS install mysql8
- Summary of common hooks
- Analysis of Project-based Learning Creativity in steam Education
- Groupby method
- Li Shuo, vice president of Baidu: it's a good thing that China's labor costs rise with the support of digital technology
- 4000 多字学懂弄通 js 中 this 指向问题,顺便手写实现 call、apply 和 bind
- 数据截断及估计
- SOA(面向服务架构)是什么?
猜你喜欢

sqlilabs less-32~less-33
![[opencv] use OpenCV to call mobile camera](/img/66/6207bafbc9696e43da7a60a386a238.jpg)
[opencv] use OpenCV to call mobile camera

Algorithm --- paint the house (kotlin)

从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析

Notes on the seventh day

Apache文件管理自学笔记——映射文件夹和基于单ip多域名配置apache虚拟机

爆肝整理JVM十大模块知识点总结,不信你还不懂

2. Nodejs -- path (\dirname, \filname), URL URL, querystring module, mime module, various paths (relative paths), web page loading (interview questions *)

单例模式(饿汉式 懒汉式)

Summary of classic problems in Flink production environment
随机推荐
爆肝整理JVM十大模块知识点总结,不信你还不懂
.NET 序列化枚举为字符串
2. Nodejs -- path (\dirname, \filname), URL URL, querystring module, mime module, various paths (relative paths), web page loading (interview questions *)
【机器人学习】机械臂抓手matlab运动学与admas动力学分析
vasp计算任务报错:M_divide:can not subdivide 8 nodes by 6
服务器运行管理制度
Weekly recommended short videos: how to make product development more effective?
[open the door to the new world] see how the old bird of testing plays API testing between applause
万字详解 Google Play 上架应用标准包格式 AAB
4000 多字学懂弄通 js 中 this 指向问题,顺便手写实现 call、apply 和 bind
2022-07-28 顾宇佳 学习笔记
Multithreading realizes concurrent reading and execution of multi use case files +selenium grid4 realizes distributed deployment of test framework
【luogu P8352】小 N 的独立集(DP套DP)(性质)
mysql大表联合查询优化,大事务优化,规避事务超时,锁等待超时与锁表
Add a row to a specific location in the dataframe
C语言基础知识点汇总
IDEA安装后无法启动
C陷阱与缺陷 第3章 语义“陷阱” 3.1 指针与数组
[NPM error] - NPM err code eresolve and NPM err eresolve could not resolve problems
盘点国内外项目协同管理软件:SaaS和定制化成趋势