当前位置:网站首页>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 0
2. 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 .
边栏推荐
- Simple and practical accounting software, so that accounts can be checked
- Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
- Here comes the chicken soup! Keep this quick guide for data analysts
- Essence and physical meaning of convolution (deep and brief understanding)
- Gee series: Unit 2 explore datasets
- Fabric.js IText设置指定文字的颜色和背景色
- 7.TCP的十一种状态集
- Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
- LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
- Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
视差特效的原理和实现方法
Gee: create a new feature and set corresponding attributes
Practical problem solving ability of steam Education
Disable access to external entities in XML parsing
Go Chan's underlying principles
Gee series: Unit 4 data import and export in Google Earth engine
LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
Fabric.js 将本地图像上传到画布背景
黑馬筆記---Set系列集合
Collectors. Groupingby sort
随机推荐
Global and Chinese market of insulin pens 2022-2028: Research Report on technology, participants, trends, market size and share
Using Kube bench and Kube hunter to evaluate the risk of kubernetes cluster
Fabric.js 居中元素
[quick view opencv] familiar with CV matrix operation with image splicing examples (3)
LeetCode 1175. 质数排列(质数判断+组合数学)
ubuntu20.04安装mysql8
Global and Chinese markets of semiconductor laser therapeutics 2022-2028: Research Report on technology, participants, trends, market size and share
Gee series: Unit 5 remote sensing image preprocessing [GEE grid preprocessing]
go实现leetcode旋转数组
Differential identities (help find mean, variance, and other moments)
How to configure PostgreSQL 12.9 to allow remote connections
7.1模拟赛总结
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
视差特效的原理和实现方法
Case sharing | intelligent Western Airport
fastText文本分类
Gee series: Unit 4 data import and export in Google Earth engine
LS1046nfs挂载文件系统
Simple and practical accounting software, so that accounts can be checked
Gee: use of common mask functions in remote sensing image processing [updatemask]