Use the divide and conquer method to obtain the number of a median of 1.

Source: Internet
Author: User
 

How can we get the number 1 after this number is converted to binary given a number (assuming 32 bits?

 

Solution:

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

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

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

X = (X & 0x0000ffff) + (x>1) & 0x0000ffff)

 

 

Schematic:

The original problem (counting the number of 1 in 32 units) is divided into the number of 1 in 16 units, and the number of 1 in 8 units ......, Finally, we will count the number of 1 in two bits. Sum.

 

Analysis:

First, check the upper left corner:

 

 

Only one of the two elements is 1, so the result is filled in the following lattice. And so on.

Next, let's look at the second row and the third line:

 

 

Red indicates that only one of the above two elements is 1, and blue indicates that two elements on the top are 1, and the sum of the two numbers is 0011, it indicates that three of the four above fields are 1.

.

.

.

And so on, the final number is 0 b010111 = 23.

 

 

Now the question is, how to count the number of 1 and then add it together?

 

First line, 0x55555555 = 0b010101010101010101010101010101010101

The result of the sum of X takes out 1 of the even digits, and 1> 1 and 0x55555555 get the 1 of the odd digits. When the two numbers are added, the number of 1 in each two is obtained and placed in the corresponding bit of the next row.

Note that because only one digit is shifted to the right, only the number of 1 in each two digits is counted. You can use the 1111 & 0101 test to easily detect problems.

0x33333333 = 00110011001100110011001100110011

The result of the sum of X is all the numbers of 2nd ^ N and (2 ^ N)-1. And x> 2 is to extract the remaining number ., These numbers are the sum of 1 bits. You can also use 1111 & 0011 for testing.

.

.

.

.

And so on, the result is the number of bitwise 1.

 

 

 

There are many methods to calculate the number of elements with a statistical value of 1. Here is a simple method of division and control. There are still many amazing methods.

 

 

References:

<Hacker's delight> -- Mechanical Industry Press

Use the divide and conquer method to obtain the number of a median of 1.

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.