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

边栏推荐
- Machine learning problem notes
- Do you know any formal part-time platforms?
- Jincang database kingbasees client programming interface guide ODBC (5. Development process)
- Deep analysis of integrated learning AdaBoost
- 多传感器融合定位(三)——惯性技术
- Is the declarative code of compose so concise?
- The failure rate is as high as 80%. How to correctly complete the strategic planning of digital transformation?
- [data mining engineer - written examination] Dahua shares in 2022
- After SAP Oracle replicates a new instance, the remote connection of the database reports an error ora-01031
- Arm-a53 data "recommended collection"
猜你喜欢

Multi sensor fusion positioning (II) -- map based positioning

迅为IMX6开发板QT系统创建AP热点基于RTL8723-交叉编译iptables

With the help of rpa+lcap, the enterprise treasurer management can be upgraded digitally

EN 1873屋面用装配附件.塑料单个屋面灯—CE认证

Powercli batch add esxi to vCenter

DevOps在物联网解决方案中的应用

Worthington -- Specification of Worthington trypsin inhibitor
![[detailed and super simple] how to use websocket links](/img/bf/5068f84e0d81a8330529cc4d7d5d27.png)
[detailed and super simple] how to use websocket links

VMware VCSA 7.0 Install

Deep analysis of integrated learning xgboost
随机推荐
台式机dp接口在哪(主机没有dp接口怎么办)
Blocking queue
PHP 海报二维码合成
SAP temporary tablespace error handling
Type 1-5 components
PowerCLi 批量添加esxi到vCenter
【C】替换空格,宏实现整数的二进制奇偶位交换
解决:Direct local .aar file dependencies are not supported when building an AAR.
PowerCLi VMware vCenter 通过自建的PXE Server一键批量部署常规New-VM
Classification and determination method of Worthington stemxyme
Zabbix 5.0 使用自带Redis模版监控
【C】atoi和offsetof的介绍和模拟实现
Machine learning problem notes
Equipped with a new generation of ultra safe cellular batteries, Sihao aipao is available from 139900 yuan
Jincang database kingbasees client programming interface guide ODBC (5. Development process)
脲酶丨Worthington杰克豆脲酶的特性及测定方案
1-6 state与绑定事件
实时数仓:滴滴的实时数仓落地实践
PowerCL 批量创建及管理虚拟交换机
Please briefly describe the respective characteristics of list, set and map type sets (briefly describe three different inheritance methods)
