当前位置:网站首页>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) )
边栏推荐
- In addition to the objective reasons for overtime, what else is worth thinking about?
- MongoDB创建一个隐式数据库用作练习
- [Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform
- “十二星座女神降临”全新活动推出
- Web3.0 series distributed storage IPFs
- Internship log - day07
- flink. CDC sqlserver. 可以再次写入sqlserver中么 有连接器的 dem
- Become a "founder" and make reading a habit
- ORM--逻辑关系与&或;排序操作,更新记录操作,删除记录操作
- Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
猜你喜欢
How will fashion brands enter the meta universe?
Garbage disposal method based on the separation of smart city and storage and living digital home mode
[Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform
喜马拉雅网页版每次暂停后弹窗推荐下载客户端解决办法
20排位赛3
Use 3 in data modeling σ Eliminate outliers for data cleaning
Official media attention! The list of top 100 domestic digital collection platforms was released, and the industry accelerated the healthy development of compliance
First issue of JS reverse tutorial
Flex flexible layout
Basic chapter: take you through notes
随机推荐
2020 Zhejiang Provincial Games
CDZSC_2022寒假个人训练赛21级(2)
Can flycdc use SqlClient to specify mysqlbinlog ID to execute tasks
小程序滑动、点击切换简洁UI
Gym - 102219j kitchen plates (violent or topological sequence)
2020浙江省赛
ORM模型--数据记录的创建操作,查询操作
Win10安装VS2015
Internship log - day07
Codeforces - 1324d pair of topics
Garbage disposal method based on the separation of smart city and storage and living digital home mode
PostgreSQL创建触发器的时候报错,
Scratch crawler mysql, Django, etc
Internship log - day04
Deep understanding of UDP, TCP
[original] what is the core of programmer team management?
能源路由器入门必读:面向能源互联网的架构和功能
csdn涨薪技术-浅学Jmeter的几个常用的逻辑控制器使用
How to become a senior digital IC Design Engineer (1-6) Verilog coding Grammar: Classic Digital IC Design
根据热门面试题分析Android事件分发机制(二)---事件冲突分析处理