当前位置:网站首页>[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
边栏推荐
- 【optimtool.unconstrain】无约束优化工具箱
- 巅峰不止,继续奋斗!城链科技数字峰会于重庆隆重举行
- 杰理之AD 系列 MIDI 功能说明【篇】
- 数十亿公民信息遭泄漏!公有云上的数据安全还有“救”吗?
- UTF encoding and character set in golang
- Huawei ENSP simulator enables devices of multiple routers to access each other
- torch. Tensor and torch The difference between tensor
- WGCNA分析基本教程总结
- 华为ensp模拟器 三层交换机
- Word文档中标题前面的黑点如何去掉
猜你喜欢

admas零件名重复

Advantages of RFID warehouse management system solution

Huawei ENSP simulator layer 3 switch

华为ensp模拟器实现通信安全(交换机)

TweenMax表情按钮js特效

Day24: file system

PS vertical English and digital text how to change direction (vertical display)

NetWare r7000 Merlin system virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements. Solution, is it necessary to create virtual memory??

杰理之AD 系列 MIDI 功能说明【篇】

Solution of 5g unstable 5g signal often dropped in NetWare r7000 Merlin system
随机推荐
UTF encoding and character set in golang
测试用例 (TC)
Flutter在 release版本,打开后随机白屏不显示内容
What are the functional modules of RFID warehouse management system solution
刘锦程荣获2022年度中国电商行业创新人物奖
【活动早知道】LiveVideoStack近期活动一览
WGCNA分析基本教程总结
[observation] Lenovo: 3x (1+n) smart office solution, releasing the "multiplier effect" of office productivity
Kubeadm初始化报错:[ERROR CRI]: container runtime is not running
Numpy vstack and column_ stack
华为ensp模拟器 DNS服务器的配置
Detailed explanation of multi-mode input event distribution mechanism
Use of redis publish subscription
华为ensp模拟器实现通信安全(交换机)
网件r7000梅林系统5g不稳定 5g信号经常掉线解决方法
杰理之AD 系列 MIDI 功能说明【篇】
Stealing others' vulnerability reports and selling them into sidelines, and the vulnerability reward platform gives rise to "insiders"
Jerry's ad series MIDI function description [chapter]
Routing configuration and connectivity test of Huawei simulator ENSP
Day24: file system