当前位置:网站首页>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) )
边栏推荐
- MySQL can connect locally through localhost or 127, but cannot connect through intranet IP (for example, Navicat connection reports an error of 1045 access denied for use...)
- CSDN salary increase technology - learn about the use of several common logic controllers of JMeter
- First issue of JS reverse tutorial
- Garbage disposal method based on the separation of smart city and storage and living digital home mode
- **Grafana installation**
- 基于智慧城市与储住分离数字家居模式垃圾处理方法
- Qualifying 3
- 剑指 Offer II 107. 矩阵中的距离
- Liunx command
- 2020CCPC威海 J - Steins;Game (sg函数、线性基)
猜你喜欢
What development models did you know during the interview? Just read this one
Web3.0 series distributed storage IPFs
Elaborate on MySQL mvcc multi version control
Win10 installation vs2015
JS reverse tutorial second issue - Ape anthropology first question
Switching value signal anti shake FB of PLC signal processing series
ORM模型--关联字段,抽象模型类
web3.0系列之分布式存储IPFS
First issue of JS reverse tutorial
How will fashion brands enter the meta universe?
随机推荐
网上可以开炒股账户吗安全吗
Pit encountered by vs2015 under win7 (successful)
Why are social portals rarely provided in real estate o2o applications?
Database multi table Association query problem
How will fashion brands enter the meta universe?
Delete a record in the table in pl/sql by mistake, and the recovery method
Parameter sniffing (1/2)
第十四次试验
The industrial chain of consumer Internet is actually very short. It only undertakes the role of docking and matchmaking between upstream and downstream platforms
First issue of JS reverse tutorial
Write VBA in Excel, connect to Oracle and query the contents in the database
Bean operation domain and life cycle
为什么安装mysql时starting service报错?(操作系统-windows)
2020ccpc Weihai J - Steins; Game (SG function, linear basis)
MySQL can connect locally through localhost or 127, but cannot connect through intranet IP (for example, Navicat connection reports an error of 1045 access denied for use...)
Software modeling and analysis
Diffusion模型详解
Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
位操作==c语言2
[4g/5g/6g topic foundation-146]: Interpretation of white paper on 6G overall vision and potential key technologies-1-overall vision