当前位置:网站首页>Operator details
Operator details
2022-07-02 05:16:00 【Smart Knight】
Catalog
5、 ... and 、 Logical operators
6、 ... and 、 Expression evaluation
One 、 Operator classification
Operators can be divided into : arithmetic operator 、 Shift operator 、 Bit operators 、 Assignment operator 、 Monocular operators 、 Relational operator 、 Logical operators 、 Conditional operators 、 Comma expression 、 Subscripts refer to function calls and structure member operators
Two 、 arithmetic operator
1. Arithmetic operators mainly include :
+( Add ) -( reduce ) *( ride ) /( except ) %( modulus , Take the remainder )
2. matters needing attention
(1) except % Besides the operator , Several other operators can act on integers and floating point numbers .
(2) about / Operator if both operands are integers , Perform integer division . As long as there are floating-point numbers, it is floating-point division , for example 4/1.0 or 4.0/1 Perform floating-point division .
(3)% The two operands of an operator must be integers . What is returned is the remainder after integral division .
(4) The modular calculation of negative numbers is related to the compiler , for example -7%3 Can be equal to -1 It could also be equal to -2
3、 ... and 、 Shift operator
1. Original code 、 Inverse and complement
First , The original inverse complement of a positive number is exactly the same , The original inverse complement of negative numbers follows the following rules :
Original code : Binary code of data , Represents the value attribute of the variable
Inverse code : Except for sign bits, all bits are reversed
Complement code : One plus one , Change the complement as long as the data is stored in memory and used by the program
2. Shift left operator :
(1) Shift rules : Abandon on the left 、 The right to repair 0
3. Shift right operator :>>
(1) Shift rules : Including arithmetic right shift and logical right shift , The choice of the two methods depends on the compiler
(2) Arithmetic shift right : Right abandon , Fill up the sign bit on the left
(3) Logical shift right : Right abandon , Fill zero directly on the left

Be careful : The operand of a shift operator can only be a non negative integer .
Four 、 Bit operators
1. Bit operators mainly include :
& ( Bitwise AND )| ( Press bit or )^ ( Bitwise XOR )
2. The rules
(1)& Bit and rule : The corresponding bits of binary complement are 1 when , The newly generated binary code is 1 Otherwise 0
(2)| Rules of bitwise OR : The corresponding bit of binary complement is 1 when , The newly generated binary code is 1 Otherwise 0
(3)^ The rule of bitwise XOR : When the corresponding bits of binary complement are different , The newly generated binary code is 1 Otherwise 0
(4) For two variables a and b, There is a^0=a,a^a=0,.
(5) Multiple ^ The calculated value of the expression is independent of the calculation order and also satisfies the commutative law , such as :a^b^b=a^(b^b)=a^0=a

3. Interview questions
Cannot create temporary variable ( The third variable ), Realize the exchange of two numbers .
#include<stdio.h>
int main()
{
int a = 0;
int b = 0;
scanf("%d %d", &a, &b);
printf(" Exchange before :a=%d,b=%d", a, b);
a = a ^ b;
b = a ^ b;// At this time a Equivalent to the original a^b
a = a ^ b;// At this time b=a,a Still equivalent to the original a^b
printf(" After exchanging :a=%d,b=%d", a, b);
return 0;
}5、 ... and 、 Logical operators
&&( Logic and ) ||( Logic or )
Logic and is that the whole expression is true when the previous and subsequent expressions are true , Logic or the whole expression is true when one of the previous and subsequent expressions is true
Distinguish between logical and bitwise and :1&2----->0 、1&&2---->1
Distinguish between logical or and bitwise OR :1|2----->3 、1||2---->1
C In language 0 For false , Non zero is true .
6、 ... and 、 Monocular operators and other operators
! Logical anti operation
- negative
+ Comes at a time
& Address fetch
sizeof The type length of the operands ( In bytes )
~ To reverse the binary of a number
-- In front of 、 After --
++ In front of 、 After ++
* Indirect access operators ( Dereference operator , For pointer )
( type ) Cast
There are other operators , Check out my previous blog ( lazy ): Refuse to go from entry to burial : First time to know C Language _ Smart Knight blog -CSDN Blog
https://blog.csdn.net/qq_65285898/article/details/124021746?spm=1001.2014.3001.5502
6、 ... and 、 Expression evaluation
1. Integer lifting and data truncation
C Integer arithmetic operations are always performed at least with the precision of the default integer type .
To get this accuracy , Characters and short operands in expressions are converted to normal integers before use , This conversion is called integer promotion .
For example, adding two character variables requires converting these two variables into int type ( Improve the overall shape ), And then add up , Finally, convert the result to character type ( truncation ).
// The shaping and lifting of negative numbers
char c1 = -1;
Variable c1 Binary bit of ( Complement code ) There are only 8 A bit :
1111111
because char For signed char
So when shaping and improving , High supplementary sign bit , That is to say 1
The result of ascension is :
11111111111111111111111111111111
// Positive integer lifting
char c2 = 1;
Variable c2 Binary bit of ( Complement code ) There are only 8 A bit :
00000001
because char For signed char
So when shaping and improving , High supplementary sign bit , That is to say 0
The result of ascension is :
00000000000000000000000000000001
// No sign plastic lift , High compensation 02. Arithmetic conversion
If the operands of an operator belong to different types , Then unless one of the operands is converted to the type of the other operand , Otherwise, the operation cannot be carried out . If the type of an operand is lower in the list above , Then first convert to the type of another operand and perform the operation .
such as : One int Variable of type with a double Add variables of type , The result is double type . in other words , Different types of data operations , In its calculation, the size of the variable is at least an integer size , The result defaults to the data type with high precision .
3. The properties of the operator
(1) There are three factors that affect the evaluation of complex expressions .
- The priority of the operator : Specifies the execution order of adjacent operators
- for example :a+b*c Due to * Has a higher priority than +, So first execute * After execution +
- The associativity of operators : When two adjacent operators have the same priority , Determine the combining direction of the expression , Some need to make the expression evaluate from left to right (L-R), Some need to be calculated from right to left (R-L), Others don't apply (N/A)
- for example :a=b=c Because the before and after operators are the same , That is to say, the priority is the same , and = The combination of R-L, That is, the operator executes from right to left , amount to a=(b=c) hold c Assign a value to b, then a=b hold b Assign a value to a, This is the associativity of operators .
- Whether to control the order of evaluation : The most representative is the logical operator .
- For example, for exp1 && exp2 Conditions ,exp1 When it is false exp2 It won't be calculated , For the same exp1 || exp2 Conditions ,exp1 To true exp2 It will not be calculated .
(2) Schedule :

(3) Because the priority of operators applies to adjacent operators , Therefore, the value of complex expressions is different under different compilers .
such as :
#include<stdio.h>
int main()
{
int i = 10;
i = i-- - --i * ( i = -3 ) * i++ + ++i;
printf("i = %d\n", i);
return 0;
}In different circumstances i = i-- - --i * ( i = -3 ) * i++ + ++i Calculated results of :
Tandy 6000 Xenix 3.2 | -128 |
Think C 5.02(Macintosh) | -95 |
IBM PowerPC AIX 3.2.5 | -86 |
Sun Sparc cc(K&C compiler ) | -85 |
gcc,HP_UX 9.0,Power C 2.0.0 | -63 |
therefore , When we write code, we should pay attention to avoid overly complex expressions .
边栏推荐
- Fabric.js 精简JSON
- ansible安装与使用
- Fasttext text text classification
- Basic differences between Oracle and MySQL (entry level)
- LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
- 2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
- Fabric.js 圆形笔刷
- Gee series: Unit 3 raster remote sensing image band characteristics and rendering visualization
- Fabric.js 将本地图像上传到画布背景
- el form 表单validate成功后没有执行逻辑
猜你喜欢
![Gee: explore the change of water area in the North Canal basin over the past 30 years [year by year]](/img/7b/b9ef76cee8b32204331a9c3c21b5c2.jpg)
Gee: explore the change of water area in the North Canal basin over the past 30 years [year by year]

Fabric.js 居中元素

Simple and practical accounting software, so that accounts can be checked

黑馬筆記---Set系列集合

Go Chan's underlying principles

Mysql基础---查询(1天学会mysql基础)

黑马笔记---Set系列集合

数据的储存

操作符详解

4. Flask cooperates with a tag to link internal routes
随机推荐
ansible安装与使用
Fabric.js 居中元素
【pyinstaller】_get_sysconfigdata_name() missing 1 required positional argument: ‘check_exists‘
How to make an RPM file
LeetCode 1175. Prime number arrangement (prime number judgment + Combinatorial Mathematics)
paddle: ValueError:quality setting only supported for ‘jpeg‘ compression
Practical problem solving ability of steam Education
Mapping settings in elk (8) es
Feign realizes file uploading and downloading
Global and Chinese market of commercial fish tanks 2022-2028: Research Report on technology, participants, trends, market size and share
Basic differences between Oracle and MySQL (entry level)
指针使用详解
2022阿里巴巴全球数学竞赛 第4题 虎虎生威(盲盒问题、集卡问题)解决思路
Feign realizes file uploading and downloading
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
Global and Chinese market of travel data recorder (VDR) 2022-2028: Research Report on technology, participants, trends, market size and share
Black Horse Notes - - set Series Collection
创新永不止步——nVisual网络可视化平台针对Excel导入的创新历程
Splice characters in {{}}
js面试收藏试题1