当前位置:网站首页>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 .
边栏推荐
- Map in JS (including leetcode examples)
- Knowledge arrangement about steam Education
- 7.1 simulation summary
- Disable access to external entities in XML parsing
- Dark horse notes -- Set Series Collection
- 2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
- Exercise notes 13 (effective letter ectopic words)
- National all Chinese Automatic Test Software apifox
- Gee: find the spatial distribution and corresponding time of the "greenest" in the Yellow River Basin in 2020 [pixel by pixel analysis]
- 线程池批量处理数据
猜你喜欢

Disable access to external entities in XML parsing

Johnson–Lindenstrauss Lemma(2)

Gee series: Unit 4 data import and export in Google Earth engine

How to configure PostgreSQL 12.9 to allow remote connections

Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster

Gee dataset: chirps pentad high resolution global grid rainfall dataset

Ansible installation and use

Video multiple effects production, fade in effect and border background are added at the same time
![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]

LeetCode 1175. 质数排列(质数判断+组合数学)
随机推荐
设置滚动条默认样式 谷歌浏览器
06 decorator mode
Summary of database problems
摆正元素(带过渡动画)
黑馬筆記---Set系列集合
Global and Chinese market of travel data recorder (VDR) 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of commercial fish tanks 2022-2028: Research Report on technology, participants, trends, market size and share
在{{}}中拼接字符
Set the default style of scroll bar Google browser
函数中使用sizeof(arr) / sizeof(arr[0])求数组长度不正确的原因
函数栈帧的创建和销毁
Application of intelligent robot in agricultural ecology
Johnson–Lindenstrauss Lemma(2)
6.30 year end summary, end of student age
Super detailed pycharm tutorial
國產全中文-自動化測試軟件Apifox
Mathematical knowledge -- understanding and examples of fast power
Summary of MySQL key challenges (2)
7.1模拟赛总结
Pyflink writes MySQL examples with JDBC