当前位置:网站首页>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) )
边栏推荐
- 请教个问题,我用sql-client起了个同步任务,从MySQL同步到ADB,历史数据有正常同步过去
- The applet realizes multi-level page switching back and forth, and supports sliding and clicking operations
- Thinkphp3.2 information disclosure
- EXT2 file system
- Introduction to energy Router: Architecture and functions for energy Internet
- Pit encountered by vs2015 under win7 (successful)
- 【ORM框架】
- 小程序滑动、点击切换简洁UI
- 2016 CCPC Hangzhou Onsite
- Can flycdc use SqlClient to specify mysqlbinlog ID to execute tasks
猜你喜欢

The Himalaya web version will pop up after each pause. It is recommended to download the client solution
![[original] what is the core of programmer team management?](/img/11/d4b9929e8aadcaee019f656cb3b9fb.png)
[original] what is the core of programmer team management?

AI从感知走向智能认知

PLC信号处理系列之开关量信号防抖FB

CSDN salary increase technology - learn about the use of several common logic controllers of JMeter

Web3.0 series distributed storage IPFs

【无标题】
![[untitled]](/img/5b/61efbaded29250bc8d921b0cf087c8.png)
[untitled]

小程序实现页面多级来回切换支持滑动和点击操作

农牧业未来发展蓝图--垂直农业+人造肉
随机推荐
HCIP 第一天 笔记整理
Gym - 102219J Kitchen Plates(暴力或拓扑序列)
Lecture 1: stack containing min function
企业实战|复杂业务关系下的银行业运维指标体系建设
有没有大佬帮忙看看这个报错,有啥排查思路,oracle cdc 2.2.1 flink 1.14.4
[untitled]
How will fashion brands enter the meta universe?
终于可以一行代码也不用改了!ShardingSphere 原生驱动问世
flink. CDC sqlserver. You can write the DEM without connector in sqlserver again
Before joining the chain home, I made a competitive product analysis for myself
Use 3 in data modeling σ Eliminate outliers for data cleaning
csdn涨薪技术-浅学Jmeter的几个常用的逻辑控制器使用
第十四次试验
Win10 installation vs2015
Gym - 102219j kitchen plates (violent or topological sequence)
视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」
Software modeling and analysis
MongoDB创建一个隐式数据库用作练习
CDZSC_2022寒假个人训练赛21级(1)
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展