当前位置:网站首页>Thoroughly understand bit operations -- and (&), not (~), or (|), XOR (^)
Thoroughly understand bit operations -- and (&), not (~), or (|), XOR (^)
2022-07-28 19:39:00 【Programmer Li ha】
Catalog
And &:
2 Binary & operation , Only when 2 The values of all are 1 when , The result is 1. All others are returned 0;
The case is as follows :

Because only when 2 Both binary are 1 when , The result is 1, Others are 0, So the result is shown in the figure below .

Rules are as follows :
" Binary truncation ", Why binary truncation , Let's use the above example ,15 The binary of is low 4 All places are 1, and 5 The binary of is 0101. because & Operations must all be 1 The result is 1, So can we understand it as 5 hold 15 Other bits of 1 Cut it into 0 What about it ? Then take another example ,0xFFFF = 65535 = 1111 1111 1111 1111, We use it 0x1 = 1 = 0000 0000 0000 0001 Let's do one & operation . So the result is 1, That is the 0xFFFF The height of 31 All the bits are set as 0, So it's just using 0x1 hold 0xFFFF The height of 31 Bits are truncated to 0.
Practical cases :
- So in some low-level operations , For space and time efficiency , It's all used bit Bits are used as identification bits to represent some states , Or address space . So be a 32 The value of bits may contain the address space , There are many status identification bits . When we need to position one of the States as 0 When , We can go through & The operation of . If the second is higher 17 The position is 0, We can use 1111 1111 1111 1111 0111 1111 1111 1111 & Source operands , You can cut off the highest 17 position .
- The operation of taking remainder : I still remember when I first started as a programmer , Will do questions to practice , For example, traversal 1-100, Even numbers output something , Odd number output . In fact, it is %2 An operation of remainder . In fact, you can also use & And operation to complete . We find the law , When a decimal system is converted to binary , The last bit of binary is 0, Or 0 Either it's 2 Multiple ( even numbers ), Instead, if the last one is 1, So either 1, Or it's odd ( Of course 1 It's also odd ). So the rule is : Source operands & 1, If 0 Then it's even , If 1 It's an odd number . And the remainder of any number can , When we need to take 1024 The remainder of , We just need to use the source operand & 1024-1, The result is 1024 The remainder of , because 1023 add 1 Is all carry , therefore 1023 yes 1024 All low bits of bit are 1, So pass & Operation can be obtained 1024 The remainder of ( Here the author , Description ability is a little poor , You can test and demonstrate by yourself )
or |:
2 Binary | operation , It only needs 2 There is one binary 1, The result is 1(2 Both binary are 1 It's also 1), Well understood. , except 2 Both binary are 0, The other results are 1.
The case is as follows :
take 10 and 5 The binary of is displayed separately

Because only 2 There is one binary 1 The results for 1, therefore | After operation, it is shown in the following figure .

Rules are as follows :
" Binary combination ", It's easy to understand , Don't explain too much , That is the 2 Group binary aligned and combined ....
Practical cases :
- As described above & The first practical case in operation , It's about how to use & Operate a bit The status position of bit is 0, that | Whether the operation can place a bit at 1 Well ? After all, it is binary combination , We just need to write a set of binaries aligned with the source operand , Set the status bit to 1 Which one of them wrote 1, It's done .
- The first 2 This case is also based on the first case , We see Linux2.6 In the source code of the kernel ( This is a fork and clone when , Configuration items of parent-child processes , I don't know or affect this case ). We are in the project , Are there many configuration items , Finally, how to manage these configuration items gracefully and efficiently ? Yes, each configuration corresponds to a set of binaries ( Although the picture is 16 Base number , Just make a decimal conversion ), Finally through | Operations combine these configurations . When judging whether configuration items can be used & Operation? ( by 0 It means that some configuration items are not enabled , nudges , But in detail )?

Exclusive or ^:
2 It's binary ^ Exclusive or operation , as long as 2 The same result of binary is 0, The difference is 1.( Note that here is to judge whether it is the same , I didn't judge whether it was again 1)
The case is as follows :

Rules are as follows :
" Binary carry free addition ", It's easy to understand ,^ The XOR operation is the same as 0, The difference is 1. The same result is 11 perhaps 00, and 11 As the result of the 0,11 So it can be understood that binary adds but does not carry ( Added as 0, But go high 1 Of , But not carry ), and 00 The sum is still 0. And for the different only 10 perhaps 01. and 01 perhaps 10 The result of XOR operation is 1, And they add up for 1. So it can be explained that XOR is binary non carry addition .
Practical cases :
- Disturbance function ( Baidu can understand by itself )
- Two number exchange , Use ^ Exclusive or operation , You can not borrow temporary variables .
a = 0011 0010; b = 0010 1010; a = a ^ b; // After operation a = 0001 1000 b = 0010 1010 b = b ^ a; // After operation b = 0011 0010 , a = 0001 1000 a = a ^ b; // After operation a = 0010 1010 , b = 0011 0010
- 2 Equal numbers ^ Whether the result of XOR operation is 0, So it can be used to judge whether it is equal .
Not ~:
Reverse the whole binary ,0 become 1,1 become 0. This is too easy , Don't draw pictures to understand .
summary :
After finding the law , You can see the answer at a glance .
Last , If this post is helpful to you , Hope you can praise it + Focus on + Collection ! Your support is my greatest motivation , The use of various frameworks and the source code interpretation of the framework will be updated all the time ~!
边栏推荐
- The mystery of ID number
- 峰值速率超2Gbps!高通在中国首发通过5G毫米波MIMO OTA测试
- 远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作
- Design of library management database system
- 【笔记】《启示录》:产品经理的实践经验与反省清单
- BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the Applic
- VAE: understanding and implementation of variational self encoder
- Business visualization - let your flowchart "run" (4. Actual business scenario test)
- Avoidance Adjusted Climbrate
- 助力面板行业创新,FPGA将成屏厂TCON最佳选择?
猜你喜欢

Force buckle 1331. Array serial number conversion

NDK 系列(5):JNI 从入门到实践,爆肝万字详解!

远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作

WPF 实现带蒙版的 MessageBox 消息提示框

Rust Getting Started Guide (crite Management)

Convertible bond concept table x notation gives you a convenient and fast experience!

shared_ptr 和 make_shared 的使用

JS 批量添加事件监听onclick this 事件委托 target currentTarget onmouseenter onmouseover

业务可视化-让你的流程图“Run“起来(4.实际业务场景测试)

What parameters should be passed in calling integer or character array functions
随机推荐
MySQL8 Encrypting InnoDB Tablespaces
Transformer for anomaly detection - instra "painting transformer for anomaly detection"
使用SaltStack自动化部署LNMP
投资35.45亿元!格力集团参与小米产业基金
第一次写博客
MySQL性能测试工具sysbench学习
Pytoch: quickly find the main diagonal elements and non diagonal elements of NxN matrix
Serial port receiving application ring buffer
Saltstack system initialization
MySQL8 基于clone创建主从复制
[notes] Networking: Internet product managers change the world
The United States will provide $25billion in subsidies to encourage chip manufacturers such as Intel to move back to production lines
Rust 入门指南(rustup, cargo)
Doxygen文档生成工具
App自动化测试是怎么实现H5测试的
App自动化测试是怎么实现H5测试的
ES6 new - arrow function
【已解决】AC86U ML改版固件虚拟内存创建失败,提示USB磁盘读写速度不满足要求
初步学习函数(第3篇博客)
When CNN meets transformer cmt:revolutionary neural networks meet vision transformers