0/1 mysteries: algorithms that directly extract the number of 1 in binary

Source: Internet
Author: User

[Problem description]: calculates the number of 1 in a binary number with N digits.

[Algorithm overview]: groups binary numbers by two digits, calculates the number of 1 in each two digits, and stores it in a binary number. Then, the above process is iterated to obtain the number of 1 in every 4 bits, the number of 1 in every 8 bits, and then the number of 1 in N bits is obtained.

[Algorithm complexity]: log is based on N

[Algorithm description]: Excerpted from http://www.loveunix.net/html/200405/29967.html. Author: wingc Release Date:

Unsigned long countbit (unsigned long X)

{

X = (X & 0x55555555) + (x> 1 & 0x55555555 );

X = (X & 0x33333333) + (x> 2 & 0x33333333 );

X = (X & 0x0f0f0f) + (x> 4 & 0x0f0f0f );

X = (X & 0x00ff00ff) + (x> 8 & 0x00ff00ff );

X = (X & 0x0000ffff) + (x> 16 & 0x0000ffff );

Return (X );

}
/*
0x55555555: 01010101010101010101010101010101
0x33333333: 00110011001100110011001100110011
0x0f0f0f: 00001111000011110000111100001111
0x00ff00ff: 00000000111111110000000011111111
0x0000ffff: 00000000000000001111111111111111
*/

Author: wingc released on: 2004-5-22

Haha, after some guidance, I understand it. In fact, I have been thinking that the conversion process of the decimal number should have a mathematical formula derivation, which leads to the constant conversion of the decimal number, if you focus on the binary number, it's hard to understand. (There may be a mathematical formula, but instead of looking for a formula 15-> 10-> 4, it is easier to understand the process of directly observing the changes of binary strings)

Let's look at the number (8 digits) of a long point. I don't know what it means.

10101100 (172)
| First iteration (with 01010101 operations)
V
00000100 + 01010100 = 01011000

In this step, the number of numbers 1 (01), 1 (01), 2 (10), and 0 (00) on each two digits of 10101100 are calculated, however, due to the problem of "weight", this value is the number of digits where the binary number is located. Except for the last two 00 values, the first three values cannot express their true meanings. Therefore, iteration is required.

01011000
| Second iteration (with 00110011 operations)
V
00010000 + 00010010 = 00100010
In this step, the numbers 2 (10101100) and 2 (0010) of 1 on every four digits are calculated. In fact, the values of the two colors in the previous operation are added in pairs, at the same time, the "weight" is adjusted, and the "weight" of the first 01 and the third 10 in the previous step is both decreased by two, in this way, the "weight" of 0010 of the last four digits is "normal", which can express the meaning of actual 2. However, the first four digits are still at a high level and cannot be actually expressed. iteration is required.

00100010
| The third iteration (with 00001111 operations)
V
0010 + 0010 = 0100 (4)
This step gets the actual meaning value of 1. The first 0010 in the previous step cannot be actually expressed due to the "weight" problem. This time, the first 0010 "weight" dropped by four places, which can express the actual meaning. This adds up to the actual number of 1.

In summary, it is actually to first calculate the number of 1 values for each two. However, due to the problem of the number of binary digits, the last two values have the meaning of counting 1, others cannot be actually expressed, so in the second iteration, this iteration calculates the number of 1 for every four bits, but for the same reason, in addition to the true meaning of counting the last four bits, the first four bits cannot be actually expressed, and the third iteration calculates the number of 1 bits per eight bits, that is, the number of one of the eight-bit binary numbers ~~
 

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.