当前位置:网站首页>【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. !!!

边栏推荐
- 多传感器融合定位(二)——基于地图的定位
- EN 1935 building hardware. Single axis hinge - CE certification
- [data mining engineer - written examination] Dahua shares in 2022
- 小程序editor富文本编辑使用及rich-text解析富文本
- leetcode 763. Partition Labels 划分字母区间(中等)
- Powercl batch creates and manages virtual switches
- Leetcode64. 最小路径和
- VirtualLab基础实验教程-8.傅里叶变换(1)
- Jincang database kingbasees client Programming Interface Guide - ODBC feature support constraints
- 数仓:Doris在美团的应用实践
猜你喜欢

PHP 海报二维码合成

GhostNets on Heterogeneous Devices via Cheap Operations

What is a driver signature and how does the driver get a digital signature?

Powercli VMware vCenter deploys conventional new VMS in batch through self built PXE server with one click

多传感器融合定位(一)——3D激光里程计

Connection pool - return connection details (Part 2)

连接池-归还连接详解(下)

The failure rate is as high as 80%. How to correctly complete the strategic planning of digital transformation?

【C】喝汽水,找单身狗问题

实时数仓:美团基于Flink的实时数仓建设实施
随机推荐
你知道有哪些正规的兼职平台吗?
什么是驱动程序签名,驱动程序如何获取数字签名?
1-8 props的基础使用
脲酶丨Worthington杰克豆脲酶的特性及测定方案
Oracle创建表空间和用户
Doip test development practice
ZABBIX 5.0 uses its own redis template for monitoring
Leetcode63. 不同路径 II
Hyperparametric optimization (grid search and Bayesian Optimization)
EN 1935 building hardware. Single axis hinge - CE certification
Wildcard ssl/tls certificate
PIP image download
Pycharm new project
Apple's official website is being updated to maintain the apple store. Products such as the iPhone 13 / pro of the Bank of China will enjoy a maximum discount of 600 yuan
SAP temporary tablespace error handling
数据中台的那些“经验与陷阱”
EN 1873 assembly accessories for roofing - plastic single roof lamps - CE certification
PMP考试倒计时,速看3A通关锦囊!
简述分组密码的加密分组链接模式的工作原理及其特点(密码学移位密码加密解密)
What is a driver signature and how does the driver get a digital signature?
