当前位置:网站首页>Bit operation ==c language 2
Bit operation ==c language 2
2022-07-07 10:04:00 【Brother Dong's cultivation diary】
One 、 Bit operators
1、 Bit and &
- Be careful : Bit and &, Logic &&.
give an example 0xAA&0xF0=0xA0,0xAA && 0xF0=1
2、 Bit or |
- Be careful : Bit or |, Logical or ||
3、 Bit inversion ~
- Be careful :C The inversion of language median is ~, Logical negation is !.
4、 Bit exclusive or ^
- 0 or 1 And 1 Bit exclusive or will be reversed ,0 or 1 And 0 Different or unchanged 、
5、 Bit shift left 、 Shift right
- For unsigned numbers :
- When you move to the left, you make up the right 0, It's equivalent to logical shift .
- When you move to the right, make up the left 0, It's equivalent to logical shift .
- For signed Numbers :
- When you move to the left, you make up the right 0, It's called arithmetic shift , It's equivalent to logical shift .
- When there is a right shift, the left side is filled with the sign bit , Positive complement 0 Negative numbers make up 1, It's called arithmetic shift .
- The shift used in embedded system is unsigned number shift .
Two 、&,|,^ Special function when operating registers
1、 Register operation requirements
- ARM It's memory and IO Uniformly prepared (x86 It is not ), Reading and writing registers is to manipulate hardware .
- How to set a special position without affecting other bits ?
The answer is : read - Change - Write Trilogy .
2、 Special positioning and zeroing &
3、 Specific location 1 use |
4、 Special positioning reverse use ^
3、 ... and 、 How to use bit operation to construct specific binary numbers
1、 Register operations often require a specific location to a specific value (“ Change ” In the process of )
- solution 1: Use tool software or your own brain to calculate , Give directly 32 Bit specific number
- solution 2: Write your own code and use bit operation symbols to build
2、 Use shift to get a specific location as 1 Binary number of
- For example, you need one bit3~bit7 by 1 Binary number of :(0x1f << 3).
- bit3 ~ bit7 by 1, meanwhile bit23 ~ 25 Also for the 1:((0x1f << 3) | (7 << 23)).
3、 Combining negation to obtain a specific location is 0 Binary number
- obtain bit4 ~ bit10 by 0, Others are 1 Number of numbers :~(0x7f << 4)
4、 summary :“ Change ” The process of using &、|、^ It can be completed by combining specific binary numbers
Four 、 Practice of bit operation
1、 Given an integer number a, Take out a Of bit3~bit8
- First step :a &= (0x3f << 3);
- The second step :a >>= 3;
2、 Given an integer number a, to a Of bit7~bit17 assignment 937
- First step :a &= ~(0x7ff << 7); // Specific location 0
- The second step :a |= (937 << 7); // Specific positioning assignment
3、 Given an integer number a, to a Of bit7~bit17 Add... To the value in 17
- First step :b = a & (0x7ff << 7); b >>= 7; // Take out the value
- The second step :b += 17; // add 17
- The third step :a &= ~(0x7ff << 7); // Specific bit clearance 0
- Step four :a |= (b << 7); // Specific positioning assignment
5、 ... and 、 Technology upgrade : Use macro definition to complete bit operation
1、 Set directly with macro definitions 、 Reset
- #define SET_NTH_BIT(x,n) ((x) |= ((1U)<<(n)))
- #define CLEAR_NTH_BIT(x,n) ((x) &=~((1U)<<(n)))
2、 Use macro definition to intercept partial continuous bits of variables ,
- For example, intercepting variables 0x88(10001000b) Of bit2~bit4
- #define GRTBITS(x,n,m) ( ( (x) & ~( ~(0U) << ((m) – (n) + 1)) ) << (n) ) >> (n) )
边栏推荐
- 基于智慧城市与储住分离数字家居模式垃圾处理方法
- Application of C # XML
- 视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」
- Lesson 1: finding the minimum of a matrix
- There is a problem using Chinese characters in SQL. Who has encountered it? Such as value & lt; & gt;` None`
- “十二星座女神降临”全新活动推出
- CodeForces - 1324D Pair of Topics(二分或双指针)
- CDZSC_2022寒假个人训练赛21级(1)
- 【原创】程序员团队管理的核心是什么?
- Applet popup half angle mask layer
猜你喜欢

Impression notes finally support the default markdown preview mode

Use 3 in data modeling σ Eliminate outliers for data cleaning

Pit using BigDecimal

Strategic cooperation subquery becomes the secret weapon of Octopus web browser

小程序滑动、点击切换简洁UI

ORM模型--关联字段,抽象模型类

【原创】程序员团队管理的核心是什么?
![[Frida practice]](/img/20/fc68bcf2f55b140d6754af6364896b.png)
[Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform

The new activity of "the arrival of twelve constellations and goddesses" was launched

Basic use of JMeter to proficiency (I) creation and testing of the first task thread from installation
随机推荐
Guys, have you ever encountered the case of losing data when Flink CDC reads mysqlbinlog? Every time the task restarts, there is a probability of losing data
Check the example of where the initialization is when C initializes the program
Liunx command
The difference between viewpager2 and viewpager and the implementation of viewpager2 in the rotation chart
20排位赛3
Write it into the SR table in the way of flinksql. It is found that the data to be deleted has not been deleted. Refer to the document https://do
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
14th test
[4g/5g/6g topic foundation-146]: Interpretation of white paper on 6G overall vision and potential key technologies-1-overall vision
哈夫曼编码压缩文件
ORM--逻辑关系与&或;排序操作,更新记录操作,删除记录操作
Agile course training
La différence entre viewpager 2 et viewpager et la mise en œuvre de la rotation viewpager 2
Official media attention! The list of top 100 domestic digital collection platforms was released, and the industry accelerated the healthy development of compliance
【frida实战】“一行”代码教你获取WeGame平台中所有的lua脚本
使用BigDecimal的坑
用flinksql的方式 写进 sr的表,发现需要删除的数据没有删除,参照文档https://do
Guys, how can mysql-cdc convert the upsert message to append only
Bean 作⽤域和⽣命周期
Sword finger offer II 107 Distance in matrix