当前位置:网站首页>【C】 Replace spaces and realize binary parity bit exchange of integers by macros
【C】 Replace spaces and realize binary parity bit exchange of integers by macros
2022-07-29 00:00:00 【Xin-Xiang Rong】
Blog home page : XIN-XIANG Rong
Series column :【 from 0 To 1,C Language learning 】
A short sentence : If you are in full bloom , Butterflies come !
Blog description : Do what you can , Write every blog , Help yourself familiarize yourself with what you have learned , I also hope these contents can help some partners on the way of learning , If errors and deficiencies are found in the article , Also hope to leave a message in the comment area , We communicate progress together !
List of articles
Preface
Introduce two basic questions , Use C Language implementation , The title is not just a practice in the article , Just record these ideas that are not easy for beginners !
One . Replace blank space
1. Subject requirements
2. Their thinking
First, find out how many spaces there are in the string ,
Each space needs to use %20 To replace ,%20 Is a space of three characters , The original space only takes up one character , in other words , For each space, you need to add two characters to replace the space ;
use end1 Record the position of the last character of the original string , use end2 Record the position after adding space to replace spaces at the end of the original string
And then end1 The content of the space is assigned to end2,end1 and end2 Move forward one more space , as long as end1 The content of the space pointed to is not a space , take end1 The content of the space is assigned to end2;
When end1 When the space pointed to is a space , here end1 Then move forward to the position of the character in front of the space ;end2 Now move forward three times , Place separately 0、2、%; After placing % after end2 Move forward one bit to % Front position ;
Repeat the process until end1 and end, That is to say end1 and end2 When the pointing positions coincide , All spaces have been replaced .
3. Code implementation
class Solution {
public:
void replaceSpace(char* str, int length) {
char* cur = str;
int space_count = 0;
while (*cur)
{
if (*cur == ' ')
{
space_count++;
}
cur++;
}
int end1 = length - 1;
int end2 = length - 1 + 2 * space_count;
while (end1 != end2)
{
if (str[end1] != ' ')
{
str[end2--] = str[end1--];
}
else
{
end1--;
str[end2--] = '0';
str[end2--] = '2';
str[end2--] = '%';
}
}
}
};
Two . Macros realize binary parity bit exchange of integers
1. Subject requirements
Write a macro , The odd and even bits of the binary bits of an integer can be exchanged .
2. Their thinking
Swap parity bits , You need to take out the parity bits first .
Since it's macro , It's not very realistic to take out the cycle separately , Then use ** Bitwise AND (&)** These bits are the way to do .
Odd digits are taken out , That is to & On 010101010101……; Even bits , Is to & On 101010101010……, The corresponding hexadecimals are 555…… and aaa……, Generally, we default to 32 An integer ,4 Bit corresponds to a bit 16 Hexadecimal is 8 individual 5,8 individual a.
adopt & 0x55555555 The way to get odd numbers and & 0xaaaaaaa Take out even digits in the same way .
Odd number Move left (<<) One is on the even digit , Even digit Move right (>>) One goes to the odd digit , The last two numbers Add get up , The exchange is complete .
※ This macro can only complete 32 Shaping within bits , To finish 64 Bit , Then we will 5 and a Just double the number of .
3. Code implementation
#include<stdio.h>
#define SWAP_BIT(num) num = (((num&0x55555555)<<1) + ((num&0xaaaaaaaa)>>1))
int main()
{
int a = 0;
scanf("%d", &a);
printf("%d\n", SWAP_BIT(a));
return 0;
}
Conclusion
Dear friends , It's fate to see here , I hope my content can bring you a little help , If you can, support it for three times !!! Thank you for coming here , We can learn and communicate together , Progress together !!! come on. !!!
边栏推荐
- sql 左连接,内连接 的写法「建议收藏」
- 商家对积分体系运营的两个误解
- 2022-07-28:以下go语言代码输出什么?A:AA;B:AB;C:BA;D:BB。 package main import ( “fmt“ ) func
- Compatibility description between kingbasees and Oracle (5. Pl/sql)
- 以JSP为视图解析器搭建SSM项目
- 连接池-归还连接详解(下)
- Compatibility description between kingbasees and Oracle (4. SQL)
- Doip test development practice
- 【TA-霜狼_may-《百人计划》】美术2.2 模型基础
- Is the declarative code of compose so concise?
猜你喜欢
PHP poster QR code synthesis
Yolov5 learning notes (I) -- principle overview
Multi sensor fusion positioning (II) -- map based positioning
基于 FPGA 实现数字时钟详细原理讲解及验证结果
GhostNets on Heterogeneous Devices via Cheap Operations
Doip communication of canoe application case
Genomic DNA isolation Worthington ribonuclease A
E-commerce data model design
ISO 13400(DoIP)标准解读
考过HCIP入职心仪公司,分享华为认证学习经历及心得
随机推荐
Worthington丨STEMxyme的分类和测定方法
实时数仓:滴滴的实时数仓落地实践
Leetcode60. 排列序列
JS advanced ES6 ~ es13 new features
Classification and determination method of Worthington stemxyme
Do you know any formal part-time platforms?
Worthington -- Specification of Worthington trypsin inhibitor
leetcode 763. Partition Labels 划分字母区间(中等)
1-7 solve the problem of this pointing of methods in classes
EN 1935建筑五金.单轴铰链—CE认证
Yolov5 learning notes (I) -- principle overview
GhostNets on Heterogeneous Devices via Cheap Operations
pycharm配置运行环境
Working principle of fastdfs (technical principle)
基因组 DNA 分离丨Worthington核糖核酸酶A
多传感器融合定位(一)——3D激光里程计
VirtualLab基础实验教程-8.傅里叶变换(1)
迅为IMX6开发板QT系统创建AP热点基于RTL8723-交叉编译iptables
Genomic DNA isolation Worthington ribonuclease A
尿酸酶丨Worthington猪肝尿酸酶的特征: