SoFunction
Updated on 2025-04-09

Example of segmented intercept constants in iOS algorithm tutorial

Preface

This article mainly introduces relevant content about segmented interception constants of iOS algorithms, and shares it for your reference and learning value. I won’t say much below, let’s take a look at the detailed introduction together.

1. The process of misaligned segmented addition and recursive merge

#include
intHamming_weight_3(intn )
{
n = (n&0x55555555) + ((n>>1)&0x55555555);
n = (n&0x33333333) + ((n>>2)&0x33333333);
n = (n&0x0f0f0f0f) + ((n>>4)&0x0f0f0f0f);
n = (n&0x00ff00ff) + ((n>>8)&0x00ff00ff);
n = (n&0x0000ffff) + ((n>>16)&0x0000ffff);
returnn;
}
intmain()
{
intn;
while(scanf("%d", &n) !=EOF)//Read the integer and print the number of 1{
printf("%d \n",Hamming_weight_3( n ));
}
return0;
}

2. Detail analysis:

1. What are the characteristics of numbers:

0x5555... After this is replaced by binary, it is 01010101010101010101...

0x3333... After this is replaced by binary, it is 0011001100110011...

0x0f0f………This is changed to binary and it is 0000111100001111…

2. If these binary sequences are regarded as a cycle sequence, then the period of the first sequence is 2, each cycle is 01, the period of the second sequence is 4, each cycle is 0011, the third cycle is 8, each is 00001111...

3. The result after these things are combined:

The entire number is divided into n segments according to the above period. The first half of each segment is cleared, and the second half retains the data. The difference is that the length of these number segments increases twice. So we can name them "segment interception constants".

Thanks!!!

Summarize

The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.