"The beauty of programming" reading notes-"Finding the number of 1 in binary numbers"

Source: Internet
Author: User
Tags integer division
Find the number of 1 in binary. For a variable of one byte (8bit), the number of "1" in the second binary representation requires the algorithm to perform as efficiently as possible.

Let's take a look at some of the algorithms given in the sample chapters:

Solution One, each time except two, see if it is an odd number, yes, the cumulative add one, the final result is a binary representation of 1.

Solution Two, also used in a cycle, but the inside of the operation is simplified with the displacement operation.

1:int Count (int v)
2: {
3:int num = 0;
4:while (v) {
5:num + = v & 0x01;
6:v >>= 1;
7:}
8:return num;
9:}

Solution Three, use a clever and operation, V & (V-1) Each can eliminate the binary representation of the last 1, using this technique can reduce a certain number of cycles.

Solution Four, look up the table method, because only the data 8bit, directly build a table, containing the number of 1 of the numbers, and then check the table on the line. Complexity O (1).

1:int counttable[256] = {0, 1, 1, 2, 1, ..., 7, 7, 8};
2:
3:int Count (int v) {
4:return Counttable[v];
5:}

Well, here are the four kinds of options given in the sample, and here's what I think.

The first is the measurement of the algorithm, the complexity is really the only standard. Especially for this data size given, and very small cases, complexity is actually a relatively minor factor.

The complexity of the tabular method is O (1), I use solution one, loop eight times fixed, the complexity is also O (1). As for the size of the data to become 32-bit integer, it is not appropriate to look at the table method.

Secondly, I think that since it is such a small operation, the scale of measurement must also be small, CPU clock cycle can be used as a reference.

The solution has several times integer addition, several times integer division (the general compiler can optimize it to displacement), there are several cyclic branch judgment, a few parity judgment (this time, according to the data on the Csapp, generally a branch penalty to consume about 14 cycles), Add up to about dozens of cycle.

Look at the solution four, look up the table method seems to be the address calculation can be solved, but in fact, there is a visit operation, and the first time it is possible that the array is not in the cache, so a cache miss result may be consumed dozens of or even hundreds of cycles (because to access memory). So for this "small operation", the performance of this algorithm is actually very poor.

Here I would recommend several algorithms to solve this problem, take the 32-bit unsigned integer as an example.

1:int Count (unsigned x) {
2:x = X-((x >> 1) & 0x55555555);
3:x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
4:x = (x + (x >> 4)) & 0x0f0f0f0f;
5:x = x + (x >> 8);
6:x = x + (x >> 16);
7:return x & 0x0000003F;
8:}

Here is the dichotomy, 221 groups Add, then four four a group add, then eight eight, and finally get everyone's sum.

And a more ingenious hakmem algorithm.

1:int Count (unsigned x) {
2:unsigned N;
3:
4:n = (x >> 1) & 033333333333;
5:x = X-n;
6:n = (n >> 1) & 033333333333;
7:x = X-n;
8:x = (x + (x >> 3)) & 030707070707;
9:x = Modu (x, 63);
10:return x;
11:}

The first is the binary you three a group, to find out the number of 1 in each group, and then merge adjacent two groups, get six a group of 1 of the number, and finally very clever with the addition of 63 to obtain the results.

Because 2^6 = 64, that is, X_0 + x_1 * + x_2 * * + = X_0 + x_1 + x_2 (mod 63), here the equals sign indicates congruence.

This procedure requires only 10 or so instructions, and does not visit, fast.

Thus, the actual effect of measuring an algorithm is not only to look at the complexity, but also in conjunction with other circumstances specific analysis.

On the following two extensions, the question is how to deal with the 32-bit integer, as already mentioned.

Question two is given two integers a and B, and how many bits of a and B are different.

This problem is actually a number of 1 problems more than one step, as long as a and B to calculate the difference or result, and then ask the value of 1 of the number of the line.

Overall it seems that the book is still very good, more like the inside of a problem to propose different algorithms and constantly improve the style. Here is a little personal understanding, I hope you correct;-)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.