The beauty of programming 2.1 -- calculate the number of 1 in the binary tree

Source: Internet
Author: User

Problem:
For a 4-byte unsigned integer variable, the binary value indicates the number of "1" and the algorithm execution efficiency is as high as possible.
 
Solution 1 (bitwise traversal): Use the bitwise operation to determine whether 1 exists after the shift, and use v & 0x01 and v> = 1.
 
Solution 2 (1 Traversal method): In each judgment only with the number of 1 to judge, using v & = V-1.
 
Solution 3 (Binary accumulate): Add two consecutive places, add four consecutive places, and add eight consecutive places. The preceding operation returns four octal digits. The value of each octal digit indicates the sum of the eight digits (that is, the number of the first digits in the eight digits), and then adds each octal digit to the preceding octal digits, still get 4 octal digits, but the value of each octal digit represents the sum of 16 digits (that is, the number of 1 in the 16 bits), and then add each octal digit to the first octal digit, finally, take the value of the first six digits in the first eight digits (this value can represent 63 at most, and the maximum number greater than 1 is 32 ).
1: int Count (unsigned x ){
2: x = x-(x> 1) & 0x55555555); // you can change it to (x + (x> 1) & 0x55555555;
3: x = (x + (x> 2) & amp; 0x33333333;
4: x = (x + (x> 4) & 0x0f0f0f;
5: x = x + (x> 8 );
6: x = x + (x> 16 );
7: return x & 0x0000003F;
8 :}
Consider x in the first row as 16 consecutive binary digits AB. a and B represent one binary digit respectively. 0x55555555 is 01 for each two consecutive binary digits, then AB-(AB> 1) & 01 = AB-a = 2 * a + B-a = a + B Indicates the sum of two consecutive binary digits of x.
Consider x in the first row as eight consecutive four binary digits AB. a and B represent two binary digits respectively, and their values are the sum of the two binary digits corresponding to x, 0x33333333 is 0011 for each four consecutive binary digits, then AB & 0011 + (AB> 2) & amp; 0011 = B + a = a + B Indicates the sum of four consecutive binary digits of x.
Consider x in the first row as four consecutive eight binary digits AB. a and B represent four binary digits respectively, and their values are the sum of the four binary digits corresponding to x, 0x0F0F0F0F is 00001111 for every eight consecutive binary digits, then AB & 00001111 + (AB> 2) & amp; 00001111 = B + a = a + B Indicates the sum of eight consecutive binary digits of x.
Consider x in the first row as four consecutive 8 binary digits: abcd, a, B, c, and d, which represent 8 binary numbers, respectively, and the values are the sum of 8 binary digits corresponding to x, x + (x> 8) = abcd + abc = a (a + B) (B + c) (c + d ). Continue with row 3, x + (x> 16) = a (a + B) (B + c) (c + d) + a (a + B) = a (a + B) (a + B + c + d ). The last eight binary digits (a + B + c + d) are the sum of the 32 binary digits of x. Www.2cto.com
 
Solution 4 (accumulate remainder): first, add three consecutive digits, and then add six consecutive digits to obtain five numbers (each number is the sum of six consecutive binary digits) + 1 (the sum of the remaining two binary digits). Finally, the remainder is used to obtain the sum of the 32 binary digits.

1: int BitCount (unsigned int u)
{
2: unsigned int uCount;
3: uCount = u-(u> 1) & 033333333333)-(u> 2) & 011111111111 );
4: return (uCount + (uCount> 3) & 030707070707) % 63;
}
Think of the 2nd rows of u as 11 consecutive three binary digits abc, a, B, and c, respectively, representing one binary number, 033333333333 is 011 for each three consecutive binary digits, and it removes the highest bit of the three binary digits. 011111111111 is 001 for each three consecutive binary digits, & it only retains the bitwise of the three binary digits. Here, it mainly ensures that the first digit does not affect the addition or subtraction of the three binary digits after the right shift, then, the operation of uCount on three consecutive binary numbers is abc-AB-a = 4a + 2b + c-2a-b-a = a + B + c, indicating the sum of the three binary digits of u.
Consider the uCount of the first row as 6 consecutive 6 binary digits AB. a and B represent the three binary digits respectively, and the values are the sum of the three binary digits corresponding to u, 030707070707 is 000111 for each of the six consecutive binary digits, then AB & 000111 + (AB> 3) & 000111 = B +. The result can be considered as 6 consecutive 6 binary digits abcdef, a, B, c, d, e and f represent six consecutive binary numbers, respectively, and their values are the sum of the six binary digits of the u pair, result = f + 64e + 64 ^ 2d + 64 ^ 3c + 64 ^ 4b + 64 ^ 5a, after obtaining the remainder 63 (multiplication has a allocation law for the remainder operation (64*64 * d) % 63 = (64% 63) * (64% 63) * (d % 63 )) % 63 = d % 63), Get a + B + c + d + f, that is, the sum of 32 binary digits of u, and a + B + c + d + f <= 32 <63, the remainder operation has no effect on the final result, but if it is a 64-bit data, it will have an impact.
 

Author: linyunzju

Related Article

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.