当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Verilog's time system tasks - $time, $stime, $realtime

金山云回港上市:中国TO B云厂商的港股奔袭

会议OA之反馈功能

看机器人教育引领素质教育主流

瀚高数据库最佳实践配置工具HG_BP日志采集内容

cuda-gdb提示:/tmp/tmpxft_***.cudafe1.stub.c: No such file or directory.

Hangao database best practice configuration tool Hg_ BP log collection content

工科男生:20岁未满,平凡但不平庸

Redis configuration cache expiration listening event trigger

VASP calculation task error: M_ divide:can not subdivide 8 nodes by 6
随机推荐
Alibaba Sentinel - 工作流程及原理解析
C和指针 第3章 语义“陷阱” 3.5 空指针并非字符串
《QA离业务代码能有多近?》通过codediff直接暴露缺陷
MySQL - count(字段)、count(主键)、count(1)、count(*)的区别
MySQL compound query (important)
navicat新建数据库
The basic concept of the origin and connotation of maker Education
Verilog的时间系统任务----$time、$stime、$realtime
会议OA之反馈功能
Trample --- discretization + tree array + difference
从零开始实现lmax-Disruptor队列(六)Disruptor 解决伪共享、消费者优雅停止实现原理解析
[QNX Hypervisor 2.2用户手册]9.11 ram(更新中)
Hangao database best practice configuration tool Hg_ BP log collection content
Look at robot education and lead the mainstream of quality education
Confusion matrix learning notes
mycat读写分离配置
金山云回港上市:中国TO B云厂商的港股奔袭
融云实时社区解决方案
多行文本省略
服务器运行管理制度