当前位置:网站首页>[dish of learning notes dog learning C] evaluation expression
[dish of learning notes dog learning C] evaluation expression
2022-07-24 10:35:00 【Jiang Junzhu】
List of articles
Expression evaluation
The order in which expressions are evaluated is partly determined by the operators priority and associativity decision . The operands of some expressions may need to be converted to other types in the evaluation process .
Implicit type conversion
Improve the overall shape
significance : The integer operation of the expression is in CPU Executed in the corresponding computing device of ,CPU Inner integer arithmetic unit (ALU) The byte length of the operands of It's usually int Byte length of , It's also CPU The length of a general-purpose register . therefore , Even two char Type addition , stay CPU When executing, it should be converted to CPU The standard length of the inner operands . Universal CPU It is difficult to realize two directly 8 Bits and bytes are directly added ( Although there may be such byte addition instructions in machine instructions ). So all kinds of expressions The length is less than int The integer value of the length , Must be converted to int perhaps unsigned int, Then it can be sent in CPU To perform the operation .
How to improve : according to Sign bit of variable data type To improve .
int main() {
char a = 3;
//00000011
char b = 127;
//01111111
char c = a + b;
// Improve the overall shape
//a - 00000000 00000000 00000000 00000011
//b - 00000000 00000000 00000000 01111111
//c - 00000000 00000000 00000000 10000010
//char Type only 8 A bit ,c To truncate
//c - 10000010
printf("%d", c);
//%d What is printed is an integer , therefore c To do integer lifting
// Integer promotion is promoted according to the sign bit of the data type of the variable
//10000010 The sign bit of is 1, So make up 1
//11111111 11111111 11111111 10000010 - Complement code
//11111111 11111111 11111111 10000001 - Inverse code
//10000000 00000000 00000000 01111110 - -126 - Original code
return 0;
}
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 low in the list , Then first convert to the type of another operand and perform the operation .
long double
double
float
unsigned long int
long int
unsigned int
int
Arithmetic conversions are To types with higher accuracy convert .
Arithmetic conversion must be reasonable , Otherwise there will be problems , If accuracy is lost .
int main() {
int a = 4;
float f = 4.5f;
a + f;
//a Will send to float transformation
return 0;
}
Operator properties
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
int main() {
int a = 1;
int b = 2;
int c = a + b * 7;// Priority determines the order of calculation
int d = a + b + 7;// Priority does not work , Associativity determines order
return 0;
}
The priority and associativity of operators are to make the code run more logically and accurately , Not for people to show off their skills , Write a bunch of operators to show how well you master priority and associativity ;
If an expression cannot determine the unique calculation path through the attributes of the operator , Then this expression is problematic .
attach :C List of priorities and associativity of language operators
边栏推荐
- The method modified by private can be accessed through reflection. What is the meaning of private?
- Protocol Bible - talk about ports and quads
- Figure model 2-2022-5-13
- Array element removal problem
- Erlang学习02
- The paper of gaojingjian center was selected into the ACL 2022 of the international summit to further expand the privacy computing capacity of Chang'an chain
- 每日三题 7.22
- Problem solving -- question 283 of leetcode question bank
- NLP introduction + practice: Chapter 2: introduction to pytorch
- Sentinel three flow control modes
猜你喜欢

CMS vulnerability recurrence - foreground arbitrary user password modification vulnerability

分布式事务处理方案大 PK!

ZOJ 2770 differential restraint system -- 2 -- May 20, 2022

Sentinel three flow control modes

Programmers can't JVM? Ashes Engineer: all waiting to be eliminated! This is a must skill!

Qt创建应用程序托盘及相关功能

实时天气API

Intranet remote control tool under Windows

Sentinel 流量控制快速入门

图像处理:RGB565转RGB888
随机推荐
MySQL - normal index
机器学习小试(10)使用Qt与Tensorflow创建CNN/FNN测试环境
Sentinel implements the persistence of pull pattern rules
Chapter V Modification implementation (impl) class
MySQL - unique index
二分查找法
Zoj1137+ operation 1 -- May 28, 2022
分布式事务处理方案大 PK!
Kotlin domain specific language (DSL)
Binlog and iptables prevent nmap scanning, xtrabackup full + incremental backup, and the relationship between redlog and binlog
图像处理:RGB565转RGB888
Binary original code, inverse code, complement code
Erlang学习01
Qt创建应用程序托盘及相关功能
The role of glpushmatrix and glpopmatrix
Will not be rejected! Learn the distributed architecture notes sorted out by Alibaba Daniel in 35 days, with a salary increase of 20K
Sentinel flow control quick start
Intranet remote control tool under Windows
Common Unicode encoding range
ZOJ 2770 differential restraint system -- 2 -- May 20, 2022