To find the number of binary 1 in an integral type

Source: Internet
Author: User

1. Determine the number of binary 1:

&->x-1&x, die and dynamic generation, dynamic generation method: bitssettable256[i] = (i &1) + BITSSETTABLE256[I/2]; Parallel bit operation: int BitCount4 (unsigned int n)

{
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);

return n;
}

The algorithm:     writes the binary representation of N, and then divides each 3bit into a group, finding out the number of 1 in each group and then representing it as a binary form. For example, n = 50, the second binary is expressed as 110010, after grouping is 110 and 010, the number of the two groups of 1 is 2 and 3. 2 corresponds to 010, 3 corresponds to 011, so the first line of code after the end, TMP = 010011, specifically how to achieve it? Because each group of 3bit, so this 3bit corresponding decimal number can be expressed as 2^2 * A + 2^1 * b + C form, that is, 4a + 2b + C form, where the value of A,b,c is 0 or 1, if 0 means that the corresponding bits is 0, if 1 means that the corresponding bits is 1, so a The value of + B + C is the number of 1 in the binary number of 4a + 2b + C. For example, the decimal number 6 (0110) = 4 * 1 + 2 * 1 + 0, where a = 1, b = 1, c = 0, A + b + c = 2, so 6 of the binary representation has two 1. Now the question is, how do I get a + B + c? Note The bitwise operation, the right shift is equivalent to 2, the use of this property!


4a + 2b + C shift right one equals 2a + b
4a + 2b + C right shift bit equals a
Then subtract.

4a + 2b + C – (2a + b) –a = a + B + C,
That's what the first line of code does, you see.

②. On the basis of the first line, the TMP in two adjacent groups in the number of 1 cumulative, because the accumulation into the process of some groups are repeated added once, so to discard these extra parts, this is the role of &030707070707, and because the final result may be greater than 63, so to take the mold.

It is important to note that, after the first line of code, from the right side, each adjacent 3bit only four possible, namely 000, 001, 010, 011, why? Because the number of 1 per 3bit is up to 3. Therefore, the following addition does not have a rounding problem, because 3 + 3 = 6, less than 8, does not produce a carry.

TMP + (TMP >> 3)-This is the addition of adjacent groups, note that there will be repeated additions to the part, such as TMP = 659 = 001 010 010 011, tmp >> 3 = 000 001 010 010, added

001 010 010 011
000 001 010 010
---------------------
001 011 100 101
001 + 101 = 1 + 5 = 6, so 659 of the binary representation has 6 1

Note that all we want is the second and last group (the green part), and the first and third (red parts) are part of the repeating addition, to eliminate, this is the task that &030707070707 completed (every three bits delete three bits), and finally why%63 it? Because it is equivalent to the number of 1 in 6bit per calculation, the maximum is 111111 = 77 (octal) = 63 (decimal), so the last 63 modulo.

To find the number of binary 1 in an integral type

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.