当前位置:网站首页>[C language] deep understanding of symbols
[C language] deep understanding of symbols
2022-07-04 21:26:00 【Program ape teaches you to play basketball】
You wangt somenthing, go get it!
Catalog
1.1 Press bit or ( | ) and Bitwise AND ( & )
1.3 A question about integer upgrade
2.1 Move left << Move right >> The operator
3.2 Understand deeply from the perspective of assembly a++
1、 Bitwise operators
1.1 Press bit or ( | ) and Bitwise AND ( & )
Last time we talked about logic or and logic and , The results they get are true and false , But we must distinguish clearly , Bitwise operators "|" and "&" And logical operators "||" "&&" It's two completely different concepts .
Bitwise , Conciseness , Operate in binary digits of numeric values , Are based on data complement .
Press bit or "|" : The binary complements of two values operate on corresponding bits , Corresponding bit 1 Then for 1 , Otherwise 0.
Bitwise AND "&": The binary complements of two values operate on corresponding bits , The corresponding bits are 1 Then for 1, Otherwise 0.
Here we illustrate :
1 | 2 :
1 The binary complement of :0000 0000 ... 0000 0001
2 The binary complement of :0000 0000 ... 0000 0010
------ Press bit or result : 0000 0000 ... 0000 0011 -> Corresponding decimal system :3
1 & 2:
1 The binary complement of :0000 0000 ... 0000 0001
2 The binary complement of :0000 0000 ... 0000 0010
------ Bitwise AND result : 0000 0000 ... 0000 0000 -> Corresponding decimal system :0
In fact, there are many university teachers or books that may place or , Bitwise AND , And then we will talk about bitwise XOR , They will call the result of each binary operation really perhaps false , In fact, this statement is Not rigorous enough , True and false are logical judgments , The result of bitwise operation is numerical , And in C In language 0 Said the false , Not 0 It's true , So I don't recommend this statement .
1.2 Bitwise XOR ( ^ )
Press bit or "^" : The binary complements of two values operate on corresponding bits , Same as 0 , Different for 1.
Here we illustrate :
1 ^ 3 :
1 The binary complement of :0000 0000 ... 0000 0001
3 The binary complement of :0000 0000 ... 0000 0011
--- Bitwise XOR result : 0000 0000 ... 0000 0010 -> Corresponding decimal system :2
5 ^ 0 :
5 The binary complement of :0000 0000 ... 0000 0101
0 The binary complement of :0000 0000 ... 0000 0000
--- Bitwise XOR result : 0000 0000 ... 0000 0101 -> Corresponding decimal system :5
Conclusion : Any number exclusive or 0 All equal to itself
Here is a pen test question : Do not create temporary variables , Realize the exchange of two numbers .
// Many small partners directly think of the way :
int main()
{
int a = 10;
int b = 20;
printf("a = %d, b = %d\n", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("a = %d, b = %d\n", a, b);
return 0;
}
But let's study this code carefully , Does he have any hidden problems ?
An integer , Four bytes , That is to say 32 A bit , Add here , Will produce carry , What if we add two big numbers ? Their sum exceeds the maximum storage range of integer , Then truncation will occur in the computer ! To avoid this , We can adopt XOR method to realize this problem :
Finally, there is a very simple bitwise negation operator :~
purpose : To reverse the binary of a number ( Including its sign bit )
Be careful : The above bitwise operators , Their operands must be integers !
1.3 A question about integer upgrade
There is such a string of code , ask : Why one char The type size can be calculated as 4 byte ?
Regardless of any bitwise operator , Computers are required to calculate , And in the computer CPU Have computing ability , But the calculated data is placed in memory . therefore , Do any calculation , Must get the data from memory CPU In the register of . The default operand width of the register is 32 position , But ,char The type data is only 1 Bytes , That is to say 8 position , dissatisfaction 32 How to do , This requires integer improvement !( For detailed integer improvement, you can refer to the information )
If it is a signed number : High complement sign bit
If it is an unsigned number : High compensation 0
2、 Shift operator
2.1 Move left << Move right >> The operator
<< The shift left operator is a binocular operator , The function is to turn The operand on the left Each binary bit of Move to the left Specify the number of digits .
>> The shift right operator is a binocular operator , The function is to turn The operand on the right Each binary bit of To the right Specify the number of digits .
Be careful :
<< Move left : The lowest bit is discarded , Top zero padding
>> Move right :
- An unsigned number : The lowest bit is discarded , Top zero padding [ Logical shift right ]
- Signed number : The lowest bit is discarded , The highest complement sign [ Count right ]
The above is calculated in the complement
Warning : Shift Operators , Please do not move the negative digits , This is not defined in the standard !
Move left, we can say , It's mainly the right shift. We need to talk about it in detail :
Obviously see , This is a right shift in unsigned numbers , The first buddy won't be surprised , But the second one doesn't understand , Let's explain :
Here's a question , When -1 Ready to put variables b When we need to see -1 The type of ?
The answer is no need for ! What is put in memory is binary complement , It's essentially a -1 Put the complement of into the variable b among , second , The shift right operator belongs to calculation , Need to be in CPU In the middle of , So you need to put it in memory first -1 Get the complement of CPU Operation in register , According to our rules , Moving right , Unsigned numbers are discarded in the low order and zeroed in the high order , therefore -1 After the right shift is completed, it becomes 0111 1111 ... 1111 1111, Then we take %d Signed integer printing , He will be treated as a signed number , The highest position is 0 So it is considered to be a positive number , Convert to decimal, that is, the value printed above .
Second, let's look at the right shift of signed numbers :
I believe you will understand this very well , The first high complement sign bit is complement 0, To discard in low order , So the result is 0, The second high complement sign bit is complement 1, To discard in low order , The value remains unchanged , still -1.
Be careful :a>>1 It doesn't change a The value of the variable , Just like a + 1. Writing like this will change :a = a >> 1;
2.2 Exercises
I have learned the logical operators of the last period , And the shift operator in this period , Let's practice our hands :
Please design a macro that can specify the number of bits of data to be changed to 1 , And design a function to print out each bit .
// Reference resources
#define SETBIT(a, num) ((a) |= (1 << (num - 1)) )
void PrintBit(int a)
{
int num = 31;
while (num >= 0)
{
if ((a & (1 << num)))
printf("1");
else
printf("0");
--num;
}
printf("\n");
}
int main()
{
int a = 0;
SETBIT(a, 5);
PrintBit(a);
return 0;
}
3、++ and -- The operation of
3.1 Basic operation
In fact, this section of knowledge is very simple to understand , But there are always some schools that like to put forward some very cross cutting problems :
int i = 3; ask :(++i) + (++i) + (++i) What's the value of ?
My advice is , See this kind of question , Just leave it empty , You can also add a sentence below ,“ Are you polite ?”
This expression , The result calculated under any compiler is different !
There is no need to argue about who is right and who is wrong , If someone wants to fight you , Then tell him directly , You are really super high .
Okay , Get down to business , Let's talk about ++ and -- Basic understanding :
- In front of ++ -- : First of all, increase by yourself ( reduce ), Reuse
- After ++ -- : First use , Since the increase again ( reduce ) If no variable is received , So direct self increase .
Example :
There are so many basic uses , Next, let's have an in-depth understanding from the perspective of assembly :
3.2 Understand deeply from the perspective of assembly a++
Since we know , After ++ Use first and then ++, If we simply ++ Once? , Where is this value used ?
int main()
{
int a = 0xDD;
int b = a++; // Yes b receive , that a The first use is to a Value ( Content ), Put it in b in
int c = 0xEE;
c++; // No receiver , that " First use ", How to understand ?
return 0;
}
vs2019 Compiler disassembly :
Conclusion : After ++ The full meaning is to use , Increasing , If no variable is received , So direct self increase .
Be careful : Different compilers may have different processing procedures , But this is a basic research process , More rigorous than simple theoretical study .
Calm, mature and precipitated
边栏推荐
- 股票开户佣金最低多少,炒股开户佣金最低网上开户安全吗
- 华为模拟器ensp的路由配置以及连通测试
- 宝塔 7.9.2 宝塔控制面板绕过 手机绑定认证 绕过官方认证
- 【C语言】符号的深度理解
- Poster cover of glacier
- 搭建一个仪式感点满的网站,并内网穿透发布到公网 1/2
- Le module minidom écrit et analyse XML
- Jerry's ad series MIDI function description [chapter]
- Jerry's ad series MIDI function description [chapter]
- Daily question -leetcode1200- minimum absolute difference - array - sort
猜你喜欢
Huawei ENSP simulator configures ACL access control list
IIC (STM32)
Day24:文件系统
maya灯建模
PS vertical English and digital text how to change direction (vertical display)
torch. Tensor and torch The difference between tensor
Word文档中标题前面的黑点如何去掉
【C语言】符号的深度理解
WGCNA分析基本教程总结
The video sound of station B is very low - solution
随机推荐
【微信小程序】协同工作与发布
Actual combat simulation │ JWT login authentication
【微服务|SCG】Predicate的使用
LambdaQueryWrapper用法
Huawei ENSP simulator configures ACL access control list
In the release version, the random white screen does not display the content after opening the shutter
巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行
改善机器视觉系统的方法
2021 CCPC Harbin B. magical subsequence (thinking question)
[Shenbo introduction] VI How to contact your favorite doctoral tutor
numpy vstack 和 column_stack
Roast B station charges, is it because it has no money?
y56.第三章 Kubernetes从入门到精通 -- 业务镜像版本升级及回滚(二九)
Redis:Redis配置文件相关配置、Redis的持久化
Huawei ENSP simulator enables devices of multiple routers to access each other
华为ensp模拟器 配置ACL访问控制列表
网件r7000梅林系统5g不稳定 5g信号经常掉线解决方法
IIC (STM32)
Jerry's ad series MIDI function description [chapter]
IIC (STM32)