Evaluate the number of 1 in the binary number

Source: Internet
Author: User
Document directory
  • Solution 1: Division and remainder
  • Solution 2: Bit operations
  • Solution 3: Bit operation (modification)
  • Solution 4: branch operations
  • Solution 5: look-up table
Evaluate the number of 1 in the binary number

Most readers will have this reaction: This question is too simple, and the solution seems to be quite simple. There will not be too many twists and turns or peak turns. So what does this question evaluate us? In fact, the requirements for storage space and efficiency vary with actual applications during programming. For example, programming on PC is very different from programming on embedded devices. We can think carefully about how to make efficiency as "high" as possible ".

Solution 1: Division and remainder

An eight-bit binary example can be used for analysis. For binary operations, we know that dividing by 2 will reduce the original number by 0. If there are more than one division, it indicates that there is a 1 in the current position. Take 10 100 010 as an example; the first time divided by 2, the quotient is 1 010 001, and the remainder is 0. The second time divided by 2, the quotient is 101, and the remainder is 1. Therefore, we can analyze the division of integer data by Division and determining the remainder value. So we have the following code.

Code List 2-1

int Count(int v) {int num = 0; while(v) { if(v % 2 == 1) { num++; } v = v/ 2; } return num; }

Solution 2: Bit operations

The previous code looks complicated. We know that the right shift operation can also achieve the goal of division. The only difference is how to determine whether 1 exists after the shift. For this question, let's look at an eight-digit number: 10 100 001. During the shift to the right, we will discard the last bit directly. Therefore, you need to determine whether the last bit is 1, and the "and" operation can achieve the goal. You can "and" the eight-digit number and 00000001. If the result is 1, it indicates that the last digit of the current eight digits is 1; otherwise, it is 0. The Code is as follows:

Code List 2-2

int Count(int v) { int num = 0; While(v) { num += v &0x01; v >>= 1; } return num; } 


Solution 3: Bit operation (modification)

Bit operations are much more efficient than division and remainder operations. However, even if bit operations are used, the time complexity is still O (log2v), and log2v is the number of digits of the binary number. So can we reduce the complexity? If there is a way to make the complexity of the algorithm only related to the number of "1", can the complexity be further reduced? 10 100 001 is also used as an example. If we only consider the correlation between the number of values and the number of values, can we determine the number of values only with 1 in each decision? To simplify this problem, we have only one case. For example, 01 000 000. How can we determine that a given binary number contains only one 1? You can determine whether this number is an integer power of 2. In addition, how can we design operations if we only make judgments with this "1? We know that if you perform this operation, the result is
0 or 1. If you want the operation result to be 111 000, you can perform the "and" operation with 00 111. In this way, the operation is 01 000 000 & (01 000 000-00 000 001) = 01 000 000 & 00 111 111 = 0. Therefore, we have the code to understand method 3:
Code List 2-3

int Count(int v) { int num = 0; while(v) { v &= (v-1); num++; } return num; } 

Solution 4: branch operations

The complexity of solution 3 is reduced to O (M), where M is the number of 1 in V. Some people may already be satisfied. We only need to calculate the number of 1 digits, so it should be fast enough. However, since we only have eight-digit data, we simply put 0 ~ The answer is as follows:

Code List 2-4

int Count(int v) { int num = 0; switch (v) { case 0x0: num = 0; break; case 0x1: case 0x2: case 0x4: case 0x8: case 0x10: case 0x20: case 0x40: case 0x80: num = 1; break; case 0x3: case 0x6: case 0xc: case 0x18: case 0x30: case 0x60: case 0xc0: num = 2; break; //... } return num; } 

Solution 4 seems very direct, but the actual execution efficiency may be lower than solution 2 and solution 3, because the execution of the branch statement depends on the value of the specific byte. If a = 0, naturally, the answer is obtained in 1st cases, but if a = 255, the answer is obtained in the last case, that is, after 255 comparison operations! It seems that solution 4 is not advisable! However, solution 4 provides the idea of using the space-for-time method to list and directly provide values. If you need to get results quickly, you can use space or use known conclusions. This is like computing 1 + 2 +... + N formulas can be used to draw conclusions in program implementation. Finally, solution 5 is obtained: The answer can be directly returned without any comparison in the algorithm. This solution should be time-intensive.


Solution 5: look-up table

Code List 2-5

/* Pre-defined result table */INT counttable [256] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 6, 7, 1, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 4, 3, 4, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 5, 3, 4, 4, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8}; int count (INT v) {// check parameter return counttable [v];}

This is a typical Algorithm for changing the space time ~ In 255, the number of "1" is directly stored in the array. V is used as the subscript of the array, and counttable [v] is the number of "1" in v. The time complexity of the algorithm is only O (1 ). In an application that requires frequent use of this algorithm, it is a common method to obtain high time efficiency through "space for Time". The specific algorithm should also be optimized for different applications.

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.