[C] Replace spaces, macros implement binary parity exchange of integers

Hits: 0

✨Blog Homepage: XIN-XIANG
Rong✨ Series column: [From 0 to 1, C language learning]
✨A short sentence: If you are in full bloom, butterflies will come spontaneously!
✨Blog description: Do your best to write every blog well, to help yourself familiarize yourself with the knowledge you have learned, and hope that your content can help some partners on the way of learning. If you find mistakes and deficiencies in the article, Also look forward to leaving a message in the comment area, we can communicate and improve together! 😊

Article directory


😽 Introduce two basic questions, which are implemented in C language. The title is not only a practice in the article, but just to record these ideas that are not easy for beginners to think of!

1. Replace spaces

1. Subject requirements

2. Problem solving ideas

First find out how many spaces there are in the string,

Each space needs to be replaced with %20, %20 is a space of three characters, and the original space only occupies a space of one character, that is to say, each space needs to add two characters to the space. space replacement;

Use end1 to record the position of the last character of the original string, and use end2 to record the position after adding space to replace the space at the end of the original string

Then assign the content of the end1 space to end2, and move the end1 and end2 one space forward. As long as the content of the space pointed to by end1 is not a space, assign the content of the end1 space to end2;

When the space pointed to by end1 is a space, then end1 moves forward to the position of the character before the space; end2 moves forward three times at this time, placing 0, 2, and % respectively; after placing %, end2 moves forward one place. to the position before %;

Repeat the above process until end1 and end, that is, when the positions pointed to by end1 and end2 coincide, all spaces are replaced.

3. Code implementation

class Solution {
    void replaceSpace(char* str, int length) {
        char* cur = str;
        int space_count = 0;
        while (*cur)
            if (*cur == ' ')
        int end1 = length - 1;
        int end2 = length - 1 + 2 * space_count;
        while (end1 != end2)
            if (str[end1] != ' ')
                str[end2--] = str[end1--];
                str[end2--] = '0';
                str[end2--] = '2';
                str[end2--] = '%';


2. The macro implements the [binary] parity exchange of integers

1. Subject requirements

Write a macro that swaps the odd and even bits of an integer’s binary bits.

2. Problem solving ideas

To swap parity bits, you need to take out the parity bits separately.

Since it is a macro, it is not very realistic to use the loop separately, so use bitwise AND (&) to do it.

If the odd-numbered digits are taken out, it is to & add 010101010101…; if the even-numbered digits are taken out, it is to & go up to 101010101010…, the corresponding hexadecimals are 555… and aaa…, generally we default to 32-bit integers, 4 bits correspond to one hexadecimal, which is 8 5 and 8 a.

Use & 0x55555555 to take out odd bits and & 0xaaaaaaa to take out even bits.

The odd-numbered bits are shifted left (<<) one bit to the even-numbered bits, the even-numbered bits are shifted right (>>) one bit to the odd-numbered bits, and the last two numbers are added to complete the swap.

※This macro can only complete the shaping within 32 bits. If you want to complete the 64 bits, you can double the number of 5 and a.

3. Code implementation

#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;


Dear friends, seeing this is fate, I hope my content can bring you a little help, if you can, support it for three consecutive times 😁! ! ! Thanks to everyone who came here, we can learn and communicate together and make progress together😉! ! ! Come on 🏃! ! !

You may also like...

Leave a Reply

Your email address will not be published.