当前位置:网站首页>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 .
边栏推荐
- 06 decorator mode
- Fabric.js 精简JSON
- Mapping settings in elk (8) es
- Express logistics quick query method, set the unsigned doc No. to refresh and query automatically
- Go implements leetcode rotation array
- Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Save the CDA from the disc to the computer
- Gee: remote sensing image composite and mosaic
- 摆正元素(带过渡动画)
- Feign realizes file uploading and downloading
猜你喜欢

06 decorator mode

Record my pytorch installation process and errors

C case of communication between server and client based on mqttnet

Gee data set: export the distribution and installed capacity of hydropower stations in the country to CSV table

Save the CDA from the disc to the computer

Practical problem solving ability of steam Education

Fabric.js 渐变
![Gee: use of common mask functions in remote sensing image processing [updatemask]](/img/55/bf4ef5fc923242e72caab71f1a4e4b.jpg)
Gee: use of common mask functions in remote sensing image processing [updatemask]

Creation and destruction of function stack frames

Gee: create a new feature and set corresponding attributes
随机推荐
Johnson–Lindenstrauss Lemma(2)
Fabric.js 居中元素
2022 Alibaba global mathematics competition, question 4, huhushengwei (blind box problem, truck problem) solution ideas
The El cascader echo only selects the questions that are not displayed
Black Horse Notes - - set Series Collection
Global and Chinese markets for marine selective catalytic reduction systems 2022-2028: Research Report on technology, participants, trends, market size and share
php/js cookie共享跨域的问题
A new attribute value must be added to the entity entity class in the code, but there is no corresponding column in the database table
Go implements leetcode rotation array
ansible安装与使用
指针使用详解
Lay the foundation for children's programming to become a basic discipline
Leetcode18题 【四数之和】递归解法
Video multiple effects production, fade in effect and border background are added at the same time
Draw a wave chart_ Digital IC
el form 表单validate成功后没有执行逻辑
Basic differences between Oracle and MySQL (entry level)
LeetCode 241. Design priorities for operational expressions (divide and conquer / mnemonic recursion / dynamic programming)
Case sharing | intelligent Western Airport
C # picture display occupancy problem