Beautiful programming Reading Notes (1) Number of 1 in binary numbers

Source: Internet
Author: User
Tags bitwise

Problem:

For an 8-bit variable, calculate the number of 1 in the binary, and the algorithm execution efficiency is as high as possible.

For example, 9 is expressed as 1001 in binary format, and two digits are 1. Therefore, if 9 is input, the number of 1 is 2.

Solution 1:

An example of 8-bit binary is provided. For binary operations, we divide it by 2, and the original number will be reduced by 0 (shifted to the right by one ). If there is more than one division, a long time indicates that there is a 1 in the current position.

Take 10100010 as an example:

For the first time divided by 2, the quotient is 1010001, and the remainder is 0.

The second time divided by 2, the quotient is 101000, and the remainder is 1

Therefore, the division of integer data is used to analyze the division and the remainder value.

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

Solution 2: Bit operations

The bitwise operation achieves the same goal.

Use and operation (&) to determine whether the last digit is 1, move the last digit to the right, and continue to judge.

You can perform operations on the octal digits and 00000001. If the result is 1, The Last octal digit is 1, otherwise it is 0.

int Count(int a){    int count = 0;    while(a)    {         count += a & 0x01;         a >>= 1;    }    return count;}

Bitwise operations are much more efficient than Division operations.

Solution 3:

The author uses a clever method to subtract Phase 1 from himself (for example, 10100010 & 10100001 = 10100000) to eliminate the last digit 1, this method reduces the number of cycles by counting the number of cycles to 1.

For details, see the original article.

int Count(int a){    int count = 0;    while(a)    {a = a & (a-1);        count++;    }    return count;}

Solution 4: branch operations

The complexity of solution 3 is reduced to O (M), where M is 1. This efficiency is high enough.

If not, there is another method. Since it is an 8-bit data (0 ~ 255), can be directly 0 ~ 255 cases are listed, and branch operations are used to obtain the answer.

This method seems straightforward, but it may be less efficient than other methods. Analyze the specific situation. If a = 0 is compared once, the answer is obtained, but a = 255 is compared 255 times.

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

Solution 5: look-up table

Directly convert 0 ~ 255 the number of corresponding 1 values directly exists in the array and the space is used for time.

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, 3, 4, 4, 5,         1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,         1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,         3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,         1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,         3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,         2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,         3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 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 a){    return CountTable[a];}

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.