当前位置:网站首页>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) )
边栏推荐
- Sqlplus garbled code problem, find the solution
- 企业实战|复杂业务关系下的银行业运维指标体系建设
- Please ask me a question. I started a synchronization task with SQL client. From Mysql to ADB, the historical data has been synchronized normally
- Impression notes finally support the default markdown preview mode
- Thinkphp3.2 information disclosure
- 【原创】程序员团队管理的核心是什么?
- [bw16 application] Anxin can realize mqtt communication with bw16 module / development board at instruction
- What development models did you know during the interview? Just read this one
- 大佬们,有没有遇到过flink cdc读MySQLbinlog丢数据的情况,每次任务重启就有概率丢数
- How will fashion brands enter the meta universe?
猜你喜欢
![[Frida practice]](/img/20/fc68bcf2f55b140d6754af6364896b.png)
[Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform

China's first electronic audio category "Yamano electronic audio" digital collection is on sale!

【frida实战】“一行”代码教你获取WeGame平台中所有的lua脚本

Natapp intranet penetration

Bean operation domain and life cycle

The applet realizes multi-level page switching back and forth, and supports sliding and clicking operations

Software modeling and analysis

Internship log - day04

Elaborate on MySQL mvcc multi version control

request对象对请求体,请求头参数的解析
随机推荐
2020CCPC威海 J - Steins;Game (sg函数、线性基)
为什么安装mysql时starting service报错?(操作系统-windows)
request对象对请求体,请求头参数的解析
Liunx command
Use 3 in data modeling σ Eliminate outliers for data cleaning
[4g/5g/6g topic foundation -147]: Interpretation of the white paper on 6G's overall vision and potential key technologies -2-6g's macro driving force for development
Basic use of JMeter to proficiency (I) creation and testing of the first task thread from installation
基础篇:带你从头到尾玩转注解
使用BigDecimal的坑
Arcgis操作: 批量修改属性表
How to become a senior digital IC Design Engineer (5-2) theory: ULP low power design technology (Part 1)
Introduction to energy Router: Architecture and functions for energy Internet
Why are social portals rarely provided in real estate o2o applications?
Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
The Himalaya web version will pop up after each pause. It is recommended to download the client solution
Deep understanding of UDP, TCP
**Grafana installation**
Horizontal split of database
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
Can flycdc use SqlClient to specify mysqlbinlog ID to execute tasks