当前位置:网站首页>C language operators
C language operators
2022-07-06 20:32:00 【farewell12345】
Catalog
5、 ... and 、 Monocular operators
6、 ... and 、 Relational operator
7、 ... and 、 Logical operators
8、 ... and 、 Conditional operators
Ten 、 Subscript reference 、 Function calls and structure members
One 、 Improve the overall shape
The properties of the operator
Operator classification
One 、 arithmetic operator
+ - * / %
/: The result of dividing two integers is rounded , for example 3/2 = 1, To get two numbers divided by decimals, at least one of them must be a decimal
%: The two operands of the modulo operator must be integers
Two 、 Shift operator
<<: Shift left operator , Move the binary bit of the operand one bit to the left , The left value is discarded , Right end patch 0
>>: Shift right operator , Move the binary bit of the operand one bit to the right , There are two moving rules
1. Arithmetic shift right : The value on the right is discarded , Fill in the original sign bit on the left , Commonly used arithmetic shift right
2. Logical shift right : The value on the right is discarded , Left complement 0
3、 ... and 、 Bit operators
&: Bitwise AND , According to the binary phase of the operand and , If it's all true, it's true
|: Press bit or , By the binary phase of the operand or , One is true is true
^: Bitwise XOR , XOR by the binary bit of the operand , True and false are different from each other
Bit operators must operate on integers
Bit operators can achieve some magical functions :
1. Calculate the integer corresponding to binary 1 The number of
// Calculate the integer corresponding to binary 1 The number of
int main()
{
int a = 0;// Integer to calculate
int i = 0;// Count the number of digits
int b = 0;// It means that this person is 0 still 1
int ret = 0;// Store results
scanf("%d", &a);
for (i = 0; i < 32; i++)
{
b = a & 1;
a = a >> 1;
ret += b;
}
printf("%d\n", ret);
return 0;
}
2. Change the value of a bit in a binary number
// Will be the last 5 Binary bits are composed of 0 Turn into 1
int main()
{
int a = 10;
//a Binary system :00000000000000000000000000001010
a = a | (1 << 4);
//00000000000000000000000000001010
//00000000000000000000000000010000
// Phase or
//00000000000000000000000000011010
printf("%d\n", a);// Output 26
return 0;
}
// Will be the last 5 Binary bits are composed of 0 Turn into 1
int main()
{
int a = 10;
//a Binary system :00000000000000000000000000001010
a = a & ~(1 << 3);
//00000000000000000000000000001010
//00000000000000000000000000001000
// According to the not 11111111111111111111111111110111
// Meet each other
//00000000000000000000000000000010
printf("%d\n", a);// Output 2
return 0;
}
3. Exchange the value of two integers , The third variable... Cannot be used
a^0 = a,a^a = 0, therefore a^b^b = a
int main()
{
int a = 3;
int b = 5;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("%d %d\n", a, b);
return 0;
}
there a^b It is equivalent to a string of keys ,a^(a^b) = b, b^(a^b) = a
Four 、 Assignment operator
Direct assignment :=
Compound assignment :+=、 -=、 *=、 /=、 <<=、 >>= 、%=、&=、|=、^=
5、 ... and 、 Monocular operators
!: Logical anti operation
-: negative
+: Comes at a time
&: Address fetch
sizeof: The type length of the operands , The unit is byte
~: According to the not
--: In front of : Calculate first and then call , After : Call first and then calculate
++: ditto
*: Dereference operator
( type ): Cast
6、 ... and 、 Relational operator
>、 >=、 < 、<= 、!=、 ==
Be careful := It's assignment ,== It's the judgment of equality
7、 ... and 、 Logical operators
&&: Logic and , Yes and , It must be true to be true
||: Logic or , Yes or relationship , If it is true, it is true
Logical and and logical or judge from left to right , When an expression is judged, there is a result , The following statements are no longer executed :
example :
int main()
{
int i = 0;
int a = 0;
int b = 2;
int c = 3;
int d = 4;
i = a++ && ++b && d++;
printf("a = %d\nb = %d\nc = %d\nd = %d\n", a, b, c ,d);
return 0;
}
The output is 1 2 3 4, because a++ After ,a by 0, 0 It is false to sum with any number , The result has been judged , hinder ++b and d++ No more execution
8、 ... and 、 Conditional operators
That is, the trinocular operator , The format is exp1 ? exp2 : exp3
The execution process is judged first exp1 Is it true , If it is true , perform exp2, If it's false , perform exp3
Nine 、 Comma expression
Is multiple expressions separated by commas , From left to right , The result of the entire expression is the result of the last expression
Ten 、 Subscript reference 、 Function calls and structure members
Subscript reference operator []: Arrays are commonly used , The subscripts of the elements in the array are from 0 Start , Such as arr[4] Represents the second in the array 5 Elements .
Function call operator ()
Structure member access operator : The first way is ., The format is : Structure . Member name . The second way is ->, The format is structure pointer -> Member name
Implicit type conversion
One 、 Improve the overall shape
C Integer arithmetic operations in languages 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 transformation is called Improve the overall shape .
Usually found in char and short In the operation of type data , Before operation, put char or short Convert to int Type then participates in the operation , Finally, according to the type of output results, we can see whether to truncate .
Integer promotion is promoted according to the sign bit of the data type of the variable .
Two 、 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 . The following hierarchy is called ordinary arithmetic conversion .
// From top to bottom, from high to low
long double
double
float
unsigned long int
long int
unsigned int
int
Such as 5/2.0f, When calculating, first set the integer 5 Convert to float Type then participates in the operation
The properties of the operator
There are three factors that affect the evaluation of complex expressions
1. The priority of the operator
2. The associativity of operators
3. Whether to control the order of evaluation
Let's look at the priority first , If the priority is the same , Look at their combination .
边栏推荐
- 小孩子学什么编程?
- 01 basic introduction - concept nouns
- 永磁同步电机转子位置估算专题 —— 基波模型类位置估算概要
- Recyclerview not call any Adapter method :onCreateViewHolder,onBindViewHolder,
- Common doubts about the introduction of APS by enterprises
- 使用ssh连接被拒
- Leetcode question 283 Move zero
- PowerPivot - DAX (first time)
- How does kubernetes support stateful applications through statefulset? (07)
- How to select several hard coded SQL rows- How to select several hardcoded SQL rows?
猜你喜欢
5. 無線體內納米網:十大“可行嗎?”問題
Anaconda安装后Jupyter launch 没反应&网页打开运行没执行
【DSP】【第一篇】开始DSP学习
Gui Gui programming (XIII) - event handling
"Penalty kick" games
Web security - payload
Cesium Click to draw a circle (dynamically draw a circle)
【Yann LeCun点赞B站UP主使用Minecraft制作的红石神经网络】
看过很多教程,却依然写不好一个程序,怎么破?
Ideas and methods of system and application monitoring
随机推荐
【计网】第三章 数据链路层(4)局域网、以太网、无线局域网、VLAN
Rhcsa Road
Introduction of Xia Zhigang
Learn to punch in Web
2022 portal crane driver registration examination and portal crane driver examination materials
[diy] how to make a personalized radio
Groovy基础语法整理
Utilisation de l'écran OLED
02 基础入门-数据包拓展
Crawler (14) - scrape redis distributed crawler (1) | detailed explanation
Jupyter launch didn't respond after Anaconda was installed & the web page was opened and ran without execution
Continuous test (CT) practical experience sharing
Anaconda安装后Jupyter launch 没反应&网页打开运行没执行
SQL injection 2
22-07-05 upload of qiniu cloud storage pictures and user avatars
Common doubts about the introduction of APS by enterprises
Special topic of rotor position estimation of permanent magnet synchronous motor -- fundamental wave model and rotor position angle
Le lancement du jupyter ne répond pas après l'installation d'Anaconda
Extraction rules and test objectives of performance test points
Digital triangle model acwing 1018 Minimum toll