The beauty of programming---Find the number of 1 in binary number __ The beauty of programming

Source: Internet
Author: User

Problem Description:

For an unsigned integer variable of one byte (8bit), the number of "1" in the binary representation is obtained, and the execution efficiency of the algorithm is required to be as high as possible.

Problem Solving:

 #include <iostream> using namespace std;
#define BYTE unsigned char/* solution 1: Using integer data division characteristics, by dividing and judging the value of the remainder of the analysis.
    Every time except for two, see if it is odd, yes add one, the final result is the binary representation of 1 of the number. * * int Count1 (BYTE v) {int sum=0;
        while (v) {if (v%2==1) {sum++;
    } V=V/2;
return sum;
    }/* Solution 2:o (LOG2V) use bit operations to shift right to the last one directly discard the "and" Operation of V and 00000001 can determine whether the last one is 1*/int Count2 (BYTE v) {int sum=0;
        while (v) {sum = v & 0x01;
    V >>=1;
return sum;
}/* Solution 3:o (m) m is the number of 1 in V.
    V & (V-1) Every time you can eliminate the last 1 of the binary representation, using this technique, the complexity of the algorithm is only related to the number of 1./int Count3 (BYTE v) {int sum = 0;
       while (v) {v &= V-1;
    sum++;
return sum;
    int main () {BYTE v=10100010;
    cout << Count1 (v) << Endl;
    cout << Count2 (v) << Endl;
    cout << Count3 (v) << Endl;
return 0; }

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.